diff --git a/apps/readest-app/src/app/api/opds/proxy/route.ts b/apps/readest-app/src/app/api/opds/proxy/route.ts index 799b502a..06e58f8e 100644 --- a/apps/readest-app/src/app/api/opds/proxy/route.ts +++ b/apps/readest-app/src/app/api/opds/proxy/route.ts @@ -113,7 +113,7 @@ async function handleRequest(request: NextRequest, method: 'GET' | 'HEAD') { }); } - if (stream === 'true' || (contentLength && parseInt(contentLength) > 1024 * 1024)) { + if (stream === 'true' && contentLength && parseInt(contentLength) > 1024 * 1024) { console.log(`[OPDS Proxy] Streaming: ${url}`); return new NextResponse(response.body, { @@ -129,13 +129,15 @@ async function handleRequest(request: NextRequest, method: 'GET' | 'HEAD') { }, }); } else { - const data = await response.text(); - console.log(`[OPDS Proxy] Success: ${url} (${data.length} bytes)`); + const buf = await response.arrayBuffer(); + const length = buf.byteLength; + console.log(`[OPDS Proxy] Success: ${url} (${length} bytes)`); - return new NextResponse(data, { + return new NextResponse(buf, { status: 200, headers: { 'Content-Type': contentType, + 'Content-Length': length.toString(), 'Cache-Control': 'public, max-age=300', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS', diff --git a/apps/readest-app/src/app/opds/page.tsx b/apps/readest-app/src/app/opds/page.tsx index e83018bf..280daf60 100644 --- a/apps/readest-app/src/app/opds/page.tsx +++ b/apps/readest-app/src/app/opds/page.tsx @@ -414,7 +414,7 @@ export default function BrowserPage() { } return; } else { - const pathname = new URL(url).pathname; + const pathname = decodeURIComponent(new URL(url).pathname); const ext = getFileExtFromMimeType(parsed?.mediaType) || getFileExtFromPath(pathname); const basename = pathname.replaceAll('/', '_'); const filename = ext ? `${basename}.${ext}` : basename; @@ -426,6 +426,7 @@ export default function BrowserPage() { let downloadUrl = useProxy ? getProxiedURL(url, '', true) : url; const headers: Record = { 'User-Agent': READEST_OPDS_USER_AGENT, + Accept: '*/*', }; if (username || password) { const authHeader = await probeAuth(url, username, password, useProxy); diff --git a/apps/readest-app/src/app/opds/utils/opdsReq.ts b/apps/readest-app/src/app/opds/utils/opdsReq.ts index 47a657f9..39eadfc4 100644 --- a/apps/readest-app/src/app/opds/utils/opdsReq.ts +++ b/apps/readest-app/src/app/opds/utils/opdsReq.ts @@ -207,7 +207,7 @@ export const probeAuth = async ( const fetchURL = useProxy ? getProxiedURL(cleanUrl) : cleanUrl; const headers: Record = { 'User-Agent': READEST_OPDS_USER_AGENT, - Accept: 'application/atom+xml, application/xml, text/xml', + Accept: 'application/atom+xml, application/xml, text/xml, */*', }; // Probe with HEAD request @@ -240,7 +240,8 @@ export const probeAuth = async ( } } - return null; + // Komga returns 200 even if requires auth, so we return Basic auth header in this case + return createBasicAuth(finalUsername, finalPassword); }; /** @@ -265,7 +266,7 @@ export const fetchWithAuth = async ( const fetchURL = useProxy ? getProxiedURL(cleanUrl) : cleanUrl; const headers: Record = { 'User-Agent': READEST_OPDS_USER_AGENT, - Accept: 'application/atom+xml, application/xml, text/xml', + Accept: 'application/atom+xml, application/xml, text/xml, */*', ...(options.headers as Record), };