Support HEAD query on fs endpoint

Some CPEs query the FS endpoint with a `HEAD` request first before downloading
the image using a `GET`.
This commit is contained in:
Clément Hampaï authored and Zaid Humaimidi committed 2023-08-30 18:14:59 -07:00
1 parent 39bb937f9d
commit 493f1d2f37
1 file changed
+15 -9
+15 -9
View File
@@ -62,8 +62,8 @@ export async function listener(
request: IncomingMessage, request: IncomingMessage,
response: ServerResponse response: ServerResponse
): Promise<void> { ): Promise<void> {
if (request.method !== "GET") { if (request.method !== "GET" && request.method !== "HEAD") {
response.writeHead(405, { Allow: "GET" }); response.writeHead(405, { Allow: "GET, HEAD" });
response.end("405 Method Not Allowed"); response.end("405 Method Not Allowed");
return; return;
} }
@@ -75,6 +75,7 @@ export async function listener(
message: "Fetch file", message: "Fetch file",
filename: filename, filename: filename,
remoteAddress: getRequestOrigin(request).remoteAddress, remoteAddress: getRequestOrigin(request).remoteAddress,
method: request.method,
}; };
const file = await collections.files.findOne({ _id: filename }); const file = await collections.files.findOne({ _id: filename });
@@ -87,20 +88,25 @@ export async function listener(
return; return;
} }
response.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Length": file.length,
});
logger.accessInfo(log);
if (request.method === "HEAD") {
response.end();
return;
}
const chunks = await getFile( const chunks = await getFile(
file["uploadDate"].getTime(), file["uploadDate"].getTime(),
file.length, file.length,
filename filename
); );
response.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Length": file.length,
});
pipeline(Readable.from(chunks), response, () => { pipeline(Readable.from(chunks), response, () => {
// Ignore errors resulting from client disconnecting // Ignore errors resulting from client disconnecting
}); });
logger.accessInfo(log);
} }