11import { Req , Res } from '../../../deps.ts'
2- import { isAbsolute , join , extname } from 'https://deno.land/std@0.99 .0/path/mod.ts'
2+ import { isAbsolute , join , extname } from 'https://deno.land/std@0.100 .0/path/mod.ts'
33import { contentType } from '../../../deps.ts'
44import { createETag } from '../utils.ts'
55import { send } from './send.ts'
@@ -22,59 +22,56 @@ export type SendFileOptions = Partial<{
2222 *
2323 * @param res Response
2424 */
25- export const sendFile = < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) => (
26- path : string ,
27- opts : SendFileOptions = { }
28- ) => {
29- const { root, headers = { } , encoding = 'utf-8' , ...options } = opts
25+ export const sendFile =
26+ < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) =>
27+ ( path : string , opts : SendFileOptions = { } ) => {
28+ const { root, headers = { } , encoding = 'utf-8' , ...options } = opts
3029
31- if ( ! isAbsolute ( path ) && ! root ) throw new TypeError ( 'path must be absolute' )
30+ if ( ! isAbsolute ( path ) && ! root ) throw new TypeError ( 'path must be absolute' )
3231
33- const filePath = root ? join ( root , path ) : path
32+ const filePath = root ? join ( root , path ) : path
3433
35- let stats : Deno . FileInfo
34+ const stats = Deno . statSync ( filePath )
3635
37- stats = Deno . statSync ( filePath )
36+ headers [ 'Content-Encoding' ] = encoding
3837
39- headers [ 'Content-Encoding ' ] = encoding
38+ headers [ 'Last-Modified ' ] = stats . mtime ! . toUTCString ( )
4039
41- headers [ 'Last-Modified ' ] = stats . mtime ! . toUTCString ( )
40+ headers [ 'Content-Type ' ] = contentType ( extname ( path ) ) || 'text/html'
4241
43- headers [ 'Content-Type ' ] = contentType ( extname ( path ) ) || 'text/html'
42+ headers [ 'ETag ' ] = createETag ( stats )
4443
45- headers [ 'ETag ' ] = createETag ( stats )
44+ headers [ 'Content-Length ' ] = ` ${ stats . size } `
4645
47- headers [ 'Content-Length' ] = `${ stats . size } `
46+ headers [ 'Content-Security-Policy' ] = "default-src 'none'"
47+ headers [ 'X-Content-Type-Options' ] = 'nosniff'
4848
49- headers [ 'Content-Security-Policy' ] = "default-src 'none'"
50- headers [ 'X-Content-Type-Options' ] = 'nosniff'
49+ let status = 200
5150
52- let status = 200
51+ if ( req . headers . get ( 'range' ) ) {
52+ status = 206
53+ const [ x , y ] = req . headers ?. get ( 'range' ) ?. replace ( 'bytes=' , '' ) . split ( '-' ) as [ string , string ]
54+ const end = ( options . end = parseInt ( y , 10 ) || stats . size - 1 )
55+ const start = ( options . start = parseInt ( x , 10 ) || 0 )
5356
54- if ( req . headers . get ( 'range' ) ) {
55- status = 206
56- const [ x , y ] = req . headers ?. get ( 'range' ) ?. replace ( 'bytes=' , '' ) . split ( '-' ) as [ string , string ]
57- const end = ( options . end = parseInt ( y , 10 ) || stats . size - 1 )
58- const start = ( options . start = parseInt ( x , 10 ) || 0 )
59-
60- if ( start >= stats . size || end >= stats . size ) {
61- res . status = 416
62- res . headers ?. set ( 'Content-Range' , `bytes */${ stats . size } ` )
63- req . respond ( { } )
64- return res
57+ if ( start >= stats . size || end >= stats . size ) {
58+ res . status = 416
59+ res . headers ?. set ( 'Content-Range' , `bytes */${ stats . size } ` )
60+ req . respond ( { } )
61+ return res
62+ }
63+ headers [ 'Content-Range' ] = `bytes ${ start } -${ end } /${ stats . size } `
64+ headers [ 'Content-Length' ] = `${ end - start + 1 } `
65+ headers [ 'Accept-Ranges' ] = 'bytes'
6566 }
66- headers [ 'Content-Range' ] = `bytes ${ start } -${ end } /${ stats . size } `
67- headers [ 'Content-Length' ] = `${ end - start + 1 } `
68- headers [ 'Accept-Ranges' ] = 'bytes'
69- }
7067
71- for ( const [ k , v ] of Object . entries ( headers ) ) res . headers ?. set ( k , v )
68+ for ( const [ k , v ] of Object . entries ( headers ) ) res . headers ?. set ( k , v )
7269
73- res . status = status
70+ res . status = status
7471
75- const file = Deno . openSync ( filePath , { read : true , ...options } )
72+ const file = Deno . openSync ( filePath , { read : true , ...options } )
7673
77- send ( req , res ) ( file )
74+ send ( req , res ) ( file )
7875
79- return res
80- }
76+ return res
77+ }
0 commit comments