From 3408bdc8121755283e0148ad65b15e300fb1bf6e Mon Sep 17 00:00:00 2001 From: alyssadev Date: Mon, 18 Sep 2023 16:53:27 +1000 Subject: [PATCH] fix bug with browsers not respecting valid text/ types other than plain --- src/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 57008de..1ad6119 100644 --- a/src/index.js +++ b/src/index.js @@ -74,7 +74,12 @@ async function add(request,host,path) { } catch (e) { if (e instanceof TypeError) { if (!dest) return create_response(request, "No file provided", {status:400}) - await FILES.put(path, dest, { httpMetadata: {contentType: dest.type}}) + await FILES.put(path, dest, { + httpMetadata: { + contentType: dest.type, + contentDisposition: `inline; filename="${dest.name}"` + } + }) await KV.delete(path) return create_response(request, `https://${host}/${path}`, {status:201}) } @@ -110,6 +115,12 @@ async function get(request,host,path) { if (dest_file) { const headers = new Headers() dest_file.writeHttpMetadata(headers) + const md = dest_file.httpMetadata + // bug with browsers not displaying valid text/ documents inline + if (request.headers.get("user-agent").startsWith("Mozilla/") && md.contentType.startsWith("text/")) { + headers.set("Content-Type", "text/plain") + headers.set("Content-Disposition", `${md.contentDisposition}; type=${md.contentType}`) + } headers.set("etag", dest_file.httpEtag) return create_response(request, dest_file.body, { headers, } ) }