Skip to content

Commit

Permalink
fix: Improve Content-Disposition parsing to support Unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
otomir23 committed Aug 20, 2024
1 parent 291adab commit a5fd8b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"devDependencies": {
"@stylistic/eslint-plugin": "^1.5.1",
"@types/better-sqlite3": "^7.6.8",
"@types/content-disposition": "^0.5.8",
"@types/mime-types": "^2.1.4",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.16.0",
Expand All @@ -33,6 +34,7 @@
"@mtcute/node": "^0.15.1",
"@t3-oss/env-core": "^0.7.1",
"better-sqlite3": "^9.3.0",
"content-disposition": "^0.5.4",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.29.3",
"mime-types": "^2.1.35",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/core/data/cobalt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from "zod"
import { Text, literal, translatable } from "#core/utils/text"
import { error, ok, Result } from "#core/utils/result"
import { parse as parseContentDisposition } from "content-disposition"

const genericErrorSchema = z.object({
status: z.literal("error"),
Expand Down Expand Up @@ -70,6 +71,14 @@ export const fetchMedia = async (

// Stream

const fileName = (header: string): string | undefined => {
try {
const contentDisposition = parseContentDisposition(header)
return contentDisposition.parameters.filename
} catch {
return undefined
}
}
export const fetchStream = async (url: string) => {
const data = await fetch(url, {
headers: [
Expand All @@ -87,8 +96,7 @@ export const fetchStream = async (url: string) => {
}

const contentDisposition = data.headers.get("Content-Disposition")
const match = contentDisposition && /.*filename="([^"]+)".*/.exec(contentDisposition)
const filename = (match && match[1]) ?? undefined
const filename = contentDisposition ? fileName(contentDisposition) : undefined

const buffer = Buffer.from(await data.arrayBuffer())
if (!buffer.length)
Expand Down

0 comments on commit a5fd8b7

Please sign in to comment.