Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e03da99

Browse files
committedApr 21, 2025
feat: infer mimetype from paste filename
1 parent 8bd7733 commit e03da99

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed
 

‎doc/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Return the index page.
88

99
Fetch the paste with name `<name>`. By default, it will return the raw content of the paste.
1010

11-
The `Content-Type` header is set to `text/plain;charset=UTF-8`. If `<ext>` is given, the worker will infer mime-type from `<ext>` and change `Content-Type`. This method accepts the following query string parameters:
11+
The `Content-Type` header is set to `text/plain;charset=UTF-8`. If `<ext>` is given, the worker will infer mime-type from `<ext>` and change `Content-Type`. If the paste is uploaded with a filename, the worker will infer mime-type from the filename. This method accepts the following query string parameters:
1212

1313
The `Content-Disposition` header is set to `inline` by default. But can be overriden by `?a` query string. If the paste is uploaded with filename, or `<filename>` is set in given request URL, `Content-Disposition` is appended with `filename*` indicating the filename (with `<ext>` if it exists).
1414

‎src/handlers/handleRead.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export async function handleGet(request: Request, env: Env, _: ExecutionContext)
4444
})
4545
}
4646

47-
const inferred_mime = url.searchParams.get("mime") || (ext && mime.getType(ext)) || "text/plain"
48-
4947
const disp = url.searchParams.has("a") ? "attachment" : "inline"
5048

5149
const item = await getPaste(env, nameFromPath)
@@ -57,6 +55,12 @@ export async function handleGet(request: Request, env: Env, _: ExecutionContext)
5755

5856
// check `if-modified-since`
5957
const pasteLastModifiedUnix = item.metadata.lastModifiedAtUnix
58+
59+
const inferred_mime = url.searchParams.get("mime")
60+
|| (ext && mime.getType(ext))
61+
|| (item.metadata.filename && mime.getType(item.metadata.filename))
62+
|| "text/plain"
63+
6064
const headerModifiedSince = request.headers.get("if-modified-since")
6165
if (headerModifiedSince) {
6266
const headerModifiedSinceUnix = Date.parse(headerModifiedSince) / 1000

‎test/controlHeaders.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ test("mime type", async () => {
77
const ctx = createExecutionContext()
88
const url = (await upload(ctx, ({ "c": genRandomBlob(1024) })))["url"]
99

10+
const url_pic = (await upload(ctx, {
11+
"c": {content: genRandomBlob(1024), filename: "xx.jpg"}
12+
}))["url"]
13+
1014
async function testMime(accessUrl: string, mime: string) {
1115
const resp = await workerFetch(ctx, accessUrl)
1216
expect(resp.headers.get("Content-Type")).toStrictEqual(mime)
@@ -18,6 +22,9 @@ test("mime type", async () => {
1822
await testMime(`${url}?mime=random-mime`, "random-mime;charset=UTF-8")
1923
await testMime(`${url}.jpg?mime=random-mime`, "random-mime;charset=UTF-8")
2024
await testMime(`${url}/test.jpg?mime=random-mime`, "random-mime;charset=UTF-8")
25+
26+
await testMime(url_pic, "image/jpeg;charset=UTF-8")
27+
await testMime(`${url_pic}.png`, "image/png;charset=UTF-8")
2128
})
2229

2330
test("cache control", async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.