Skip to content

Commit 2aa4f2f

Browse files
committed
refactor(uploads): collapse getFileContentType into resolveFileType
The two helpers differed only in whether application/octet-stream falls back to the extension map. Add an option flag to resolveFileType and keep getFileContentType as a thin wrapper for direct-PUT callers that need to preserve the exact browser-reported content-type.
1 parent c448556 commit 2aa4f2f

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

apps/sim/lib/uploads/utils/file-utils.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,25 +272,32 @@ export function getMimeTypeFromExtension(extension: string): string {
272272
}
273273

274274
/**
275-
* Resolve a reliable MIME type from a file, falling back to extension
276-
* when the browser reports empty or generic `application/octet-stream`
277-
*/
278-
export function resolveFileType(file: { type: string; name: string }): string {
279-
return file.type && file.type !== 'application/octet-stream'
280-
? file.type
281-
: getMimeTypeFromExtension(getFileExtension(file.name))
275+
* Resolve a reliable MIME type from a file, falling back to the extension map
276+
* when the browser reports an empty type. By default treats
277+
* `application/octet-stream` as "unknown" and falls back to the extension —
278+
* pass `{ preserveOctetStream: true }` for direct PUT uploads where the
279+
* browser-supplied content-type must match the presigned handshake exactly.
280+
*/
281+
export function resolveFileType(
282+
file: { type: string; name: string },
283+
options?: { preserveOctetStream?: boolean }
284+
): string {
285+
const browserType = file.type?.trim()
286+
if (browserType) {
287+
if (options?.preserveOctetStream || browserType !== 'application/octet-stream') {
288+
return browserType
289+
}
290+
}
291+
return getMimeTypeFromExtension(getFileExtension(file.name))
282292
}
283293

284294
/**
285-
* Resolve the upload `Content-Type` for a `File`. Trusts the browser-reported
286-
* type when present, otherwise falls back to the extension map. Unlike
287-
* {@link resolveFileType}, this preserves `application/octet-stream` if the
288-
* browser explicitly set it — relevant for direct PUT uploads where we want
289-
* the exact content-type the user's browser handshakes with the bucket on.
295+
* Upload `Content-Type` for direct PUT — preserves the browser's reported type
296+
* verbatim (including `application/octet-stream`) so it matches the presigned
297+
* URL's signed Content-Type header.
290298
*/
291299
export function getFileContentType(file: File): string {
292-
if (file.type?.trim()) return file.type
293-
return getMimeTypeFromExtension(getFileExtension(file.name))
300+
return resolveFileType(file, { preserveOctetStream: true })
294301
}
295302

296303
/**

0 commit comments

Comments
 (0)