1- import { contentDisposition } from 'https://esm.sh /@tinyhttp/content-disposition'
1+ import { contentDisposition } from 'https://cdn.skypack.dev /@tinyhttp/content-disposition@2.0.0 '
22import { SendFileOptions , sendFile } from './send/sendFile.ts'
3- import { extname } from 'https://deno.land/std@0.103 .0/path/mod.ts'
3+ import { extname } from 'https://deno.land/std@0.106 .0/path/mod.ts'
44import { setContentType , setHeader } from './headers.ts'
55import { Req , Res } from '../../deps.ts'
66
@@ -9,38 +9,38 @@ export type DownloadOptions = SendFileOptions &
99 headers : Record < string , unknown >
1010 } >
1111
12- export const download = < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) => (
13- path : string ,
14- filename ?: string ,
15- options : DownloadOptions = { }
16- ) : Response => {
17- const name : string | null = filename as string
18- let opts : DownloadOptions = options
19-
20- // set Content-Disposition when file is sent
21- const headers : Record < string , string > = {
22- 'Content-Disposition' : contentDisposition ( name || path )
23- }
12+ export const download =
13+ < Request extends Req = Req , Response extends Res = Res > ( req : Request , res : Response ) =>
14+ ( path : string , filename ?: string , options : DownloadOptions = { } ) : Response => {
15+ const name : string | null = filename as string
16+ let opts : DownloadOptions = options
2417
25- // merge user-provided headers
26- if ( opts . headers ) {
27- for ( const key of Object . keys ( opts . headers ) ) {
28- if ( key . toLowerCase ( ) !== 'content-disposition' ) headers [ key ] = opts . headers [ key ]
18+ // set Content-Disposition when file is sent
19+ const headers : Record < string , string > = {
20+ 'Content-Disposition' : contentDisposition ( name || path )
21+ }
22+
23+ // merge user-provided headers
24+ if ( opts . headers ) {
25+ for ( const key of Object . keys ( opts . headers ) ) {
26+ if ( key . toLowerCase ( ) !== 'content-disposition' ) headers [ key ] = opts . headers [ key ]
27+ }
2928 }
30- }
3129
32- // merge user-provided options
33- opts = { ...opts , headers }
30+ // merge user-provided options
31+ opts = { ...opts , headers }
3432
35- // send file
33+ // send file
3634
37- return sendFile < Request , Response > ( req , res ) ( path , opts ) as Response
38- }
35+ return sendFile < Request , Response > ( req , res ) ( path , opts ) as Response
36+ }
3937
40- export const attachment = < Response extends Res = Res > ( res : Response ) => ( filename ?: string ) : Response => {
41- if ( filename ) setContentType ( res ) ( extname ( filename ) )
38+ export const attachment =
39+ < Response extends Res = Res > ( res : Response ) =>
40+ ( filename ?: string ) : Response => {
41+ if ( filename ) setContentType ( res ) ( extname ( filename ) )
4242
43- setHeader ( res ) ( 'Content-Disposition' , contentDisposition ( filename ) )
43+ setHeader ( res ) ( 'Content-Disposition' , contentDisposition ( filename ) )
4444
45- return res
46- }
45+ return res
46+ }
0 commit comments