@@ -190,24 +190,55 @@ export class ShareXServer {
190190 return res . status ( 400 ) . send ( "No filename provided." ) ;
191191 }
192192
193- this . #debug( `File ${ filename } requested by ${ req . ip } ` ) ;
194193 const exists = await this . #checkFileExists( filename ) ;
195194
196195 if ( ! exists ) {
197196 this . #debug( `The requested file ${ filename } does not exist` ) ;
198197 return res . status ( 404 ) . send ( "The requested file does not exist." ) ;
199198 }
200199
201- this . #debug( `Serving file ${ filename } to ${ req . ip } ` ) ;
202- const file = createReadStream ( `${ this . #fsPath} /${ filename } ` ) ;
203200 const fileStat = await stat ( `${ this . #fsPath} /${ filename } ` ) ;
204201
202+ let status = 200 ;
203+ let start = 0 ;
204+ let end = fileStat . size ;
205+ const additionalHeaders : {
206+ "Content-Range" ?: string ;
207+ "Accept-Ranges" ?: "bytes" ;
208+ } = { } ;
209+
210+ if ( req . header ( "Range" ) ) {
211+ const range = req . range ( fileStat . size ) ;
212+ if ( range && range !== - 1 && range !== - 2 && range [ 0 ] ) {
213+ status = 206 ;
214+ start = range [ 0 ] . start ;
215+ end = range [ 0 ] . end ;
216+ additionalHeaders [
217+ "Content-Range"
218+ ] = `bytes ${ start } -${ end } /${ fileStat . size } ` ;
219+ additionalHeaders [ "Accept-Ranges" ] = "bytes" ;
220+ }
221+ }
222+
205223 res . set ( {
206224 "Content-Length" : fileStat . size . toString ( ) ,
207- "Accept-Ranges" : "bytes" ,
208225 "Content-Type" : lookup ( filename ) || "application/octet-stream" ,
226+
227+ ...additionalHeaders ,
228+ } ) . status ( status ) ;
229+
230+ const file = createReadStream ( `${ this . #fsPath} /${ filename } ` , {
231+ start,
232+ end,
209233 } ) ;
210234
235+ if ( end == fileStat . size )
236+ this . #debug( `Serving file ${ filename } to ${ req . ip } ` ) ;
237+ else
238+ this . #debug(
239+ `Streaming file ${ filename } to ${ req . ip } (range: ${ start } -${ end } )`
240+ ) ;
241+
211242 return file . pipe ( res ) ;
212243 }
213244
0 commit comments