File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,32 @@ import { fileTypeFromBuffer } from '@sgtpooki/file-type'
4
4
// default from verified-fetch is application/octect-stream, which forces a download. This is not what we want for MANY file types.
5
5
export const defaultMimeType = 'text/html'
6
6
7
+ function checkForSvg ( bytes : Uint8Array ) : boolean {
8
+ return / ^ ( < \? x m l [ ^ > ] + > ) ? [ ^ < ^ \w ] + < s v g / ig. test ( new TextDecoder ( ) . decode ( bytes . slice ( 0 , 64 ) ) )
9
+ }
10
+
11
+ async function checkForJson ( bytes : Uint8Array ) : Promise < boolean > {
12
+ try {
13
+ JSON . parse ( new TextDecoder ( ) . decode ( bytes ) )
14
+ return true
15
+ } catch ( err ) {
16
+ return false
17
+ }
18
+ }
19
+
7
20
export const contentTypeParser : ContentTypeParser = async ( bytes , fileName ) => {
8
21
const detectedType = ( await fileTypeFromBuffer ( bytes ) ) ?. mime
9
22
if ( detectedType != null ) {
10
23
return detectedType
11
24
}
25
+
12
26
if ( fileName == null ) {
13
27
// no other way to determine file-type.
28
+ if ( checkForSvg ( bytes ) ) {
29
+ return 'image/svg+xml'
30
+ } else if ( await checkForJson ( bytes ) ) {
31
+ return 'application/json'
32
+ }
14
33
return defaultMimeType
15
34
}
16
35
You can’t perform that action at this time.
0 commit comments