UNPKG

fkc

Version:

FKC application service framework.

29 lines 1.08 kB
'use strict'; const fs = require('fs'); module.exports = (req,res,obj) => { const stat = fs.statSync(obj.url); const fileSize = stat.size; const range = req.headers.range; if (range) { const parts = range.replace(/bytes=/,'').split('-'); const start = parseInt(parts[0], 10); const end = parts[1] ? parseInt(parts[1], 10):fileSize-1; const chunksize = (end - start) + 1; const head = { 'Accept-Ranges': 'bytes', 'Content-Type': obj.type, 'Content-Length': chunksize, 'Content-Range': `bytes ${start}-${end}/${fileSize}`, }; res.writeHead(206, head); fs.createReadStream(obj.url, { start, end }).pipe(res); } else { let head = { 'Content-Length': fileSize, 'Content-Type': obj.type, }; if(obj.name) head['Content-Disposition'] = `attachment; filename="${encodeURI(obj.name)}"`; res.writeHead(200, head); fs.createReadStream(obj.url).pipe(res); } }