denoland / denoland/std

consider setting content-disposition header for serveFile

Open
#6,539 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

This will allow the downloaded file to have the name of the file instead of a random string

```diff
--- a/vendor/jsr.io/@std/http/0.224.5/file_server.ts
+++ b/vendor/jsr.io/@std/http/0.224.5/file_server.ts
@@ -38,6 +38,7 @@ import { join } from "jsr:/@std/path@1.0.0-rc.2/join";
import { relative } from "jsr:/@std/path@1.0.0-rc.2/relative";
import { resolve } from "jsr:/@std/path@1.0.0-rc.2/resolve";
import { SEPARATOR_PATTERN } from "jsr:/@std/path@1.0.0-rc.2/constants";
+import { basename } from "jsr:/@std/path@1.0.0-rc.2/basename";
import { contentType } from "jsr:/@std/media-types@^1.0.0-rc.1/content-type";
import { calculate, ifNoneMatch } from "./etag.ts";
import {
@@ -232,6 +233,14 @@ export async function serveFile(
headers.set("content-type", contentTypeValue);
}

+ // Suggest original filename for downloads
+ const filename = basename(filePath);
+ const encodedFilename = encodeURIComponent(filename);
+ headers.set(
+ "content-disposition",
+ `attachment; filename*=UTF-8''${encodedFilename}`,
+ );
+
const fileSize = fileInfo.size;

const rangeValue = req.headers.get("range");
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.