Skip to content

Commit b4d12f1

Browse files
committed
Adding content type mapping
1 parent 08f490f commit b4d12f1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "labelzoom-cf-api-proxy",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"private": true,
55
"scripts": {
66
"deploy": "wrangler deploy",

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313

1414
import { handleOptions, responseWithAllowOrigin } from "./cors";
1515

16+
function getContentType(fileExtension: string): string | undefined {
17+
switch (fileExtension) {
18+
case "json": return "application/json";
19+
case "xml": return "application/xml";
20+
case "zpl": return "text/plain";
21+
case "png": return "image/png";
22+
case "jpg":
23+
case "jpeg":
24+
return "image/jpeg";
25+
case "gif": return "image/gif";
26+
case "bmp": return "image/bmp";
27+
case "pdf": return "application/pdf";
28+
}
29+
return undefined;
30+
}
31+
1632
/**
1733
* Log conversion request and response data to Cloudflare R2
1834
* @param request
@@ -35,7 +51,7 @@ async function handleConversionLog(request: Request<unknown, IncomingRequestCfPr
3551

3652
// Clone and log request asynchronously
3753
if (loggingEnabled) ctx.waitUntil(Promise.all([
38-
env.LZ_R2_BUCKET.put(requestID + `/in.${sourceFormat}`, request.clone().body), // TODO: Set content type
54+
env.LZ_R2_BUCKET.put(requestID + `/in.${sourceFormat}`, request.clone().body, {httpMetadata:{contentType:getContentType(sourceFormat)}}),
3955
env.LZ_R2_BUCKET.put(requestID + '/params.json', url.searchParams.get('params'), {httpMetadata:{contentType:'application/json'}})
4056
]));
4157

@@ -44,7 +60,7 @@ async function handleConversionLog(request: Request<unknown, IncomingRequestCfPr
4460

4561
// Clone and log response asynchronously
4662
if (loggingEnabled) ctx.waitUntil(
47-
env.LZ_R2_BUCKET.put(requestID + `/out.${targetFormat}`, response.clone().body) // TODO: Set content type
63+
env.LZ_R2_BUCKET.put(requestID + `/out.${targetFormat}`, response.clone().body, {httpMetadata:{contentType:getContentType(targetFormat)}})
4864
);
4965

5066
// Return response to client

0 commit comments

Comments
 (0)