Skip to content

Commit

Permalink
content disposition helper
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Feb 6, 2024
1 parent 95ad0af commit 01d6685
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
13 changes: 13 additions & 0 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { lookup } from "@uploadthing/mime-types";

import type { AllowedFileType } from "./file-types";
import type {
ContentDisposition,
ExpandedRouteConfig,
FetchEsque,
FileData,
Expand Down Expand Up @@ -247,3 +248,15 @@ export function objectKeys<T extends Record<string, unknown>>(
export function isObject(obj: unknown): obj is Record<string, unknown> {
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
}

/** construct content-disposition header */
export function contentDisposition(
contentDisposition: ContentDisposition,
fileName: string,
) {
return [
contentDisposition,
`filename="${encodeURI(fileName)}"`,
`filename*=UTF-8''${encodeURI(fileName)}`,
].join("; ");
}
19 changes: 9 additions & 10 deletions packages/uploadthing/src/internal/multi-part.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { generateUploadThingURL, UploadThingError } from "@uploadthing/shared";
import {
contentDisposition,
generateUploadThingURL,
UploadThingError,
} from "@uploadthing/shared";
import type { ContentDisposition, FetchEsque } from "@uploadthing/shared";

import { maybeParseResponseXML } from "./s3-error-parser";
Expand Down Expand Up @@ -26,11 +30,10 @@ export async function uploadPart(
body: opts.chunk,
headers: {
"Content-Type": opts.contentType,
"Content-Disposition": [
"Content-Disposition": contentDisposition(
opts.contentDisposition,
`filename="${encodeURI(opts.fileName)}"`,
`filename*=UTF-8''${encodeURI(opts.fileName)}`,
].join("; "),
opts.fileName,
),
},
});

Expand Down Expand Up @@ -99,11 +102,7 @@ export async function uploadPartWithProgress(
xhr.setRequestHeader("Content-Type", opts.fileType);
xhr.setRequestHeader(
"Content-Disposition",
[
opts.contentDisposition,
`filename="${encodeURI(opts.fileName)}"`,
`filename*=UTF-8''${encodeURI(opts.fileName)}`,
].join("; "),
contentDisposition(opts.contentDisposition, opts.fileName),
);

xhr.onload = async () => {
Expand Down

0 comments on commit 01d6685

Please sign in to comment.