birdpack 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core.js +58 -32
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -387,46 +387,72 @@ module.exports = class{
|
|
|
387
387
|
this.end();
|
|
388
388
|
}
|
|
389
389
|
}else if(fileRange == 'file'){
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
390
|
+
if(options.parameter){
|
|
391
|
+
let data = fs.readFileSync(options.file, { encoding: 'utf8', flag: 'r' });
|
|
392
|
+
|
|
393
|
+
data = data.replace(/\{\{[a-zA-Z0-9\_]*\}\}/g, (a)=>{
|
|
394
|
+
let key = a.slice(2,-2);
|
|
395
|
+
if(key != ''){
|
|
396
|
+
if(options.parameter.hasOwnProperty(key)){
|
|
397
|
+
return options.parameter[key]
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return '';
|
|
401
|
+
});
|
|
394
402
|
|
|
395
|
-
|
|
396
|
-
const fileDescriptor = fs.openSync(options.file, "r");
|
|
403
|
+
data = Buffer.from(data, "utf8");
|
|
397
404
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
fs.closeSync(fileDescriptor);
|
|
401
|
-
return;
|
|
405
|
+
if(options.download === true){
|
|
406
|
+
this.set('content-disposition', `attachment; filename="${options.filename}"`);
|
|
402
407
|
}
|
|
403
408
|
|
|
404
|
-
|
|
405
|
-
|
|
409
|
+
this.code(200)
|
|
410
|
+
.length(data.length)
|
|
411
|
+
.type(options.type);
|
|
412
|
+
|
|
413
|
+
this.send(data);
|
|
414
|
+
}else{
|
|
415
|
+
const totalSize = fileStat.size;
|
|
406
416
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
if (err) {
|
|
410
|
-
fs.closeSync(fileDescriptor);
|
|
411
|
-
} else {
|
|
412
|
-
sendChunk(position + bytesRead);
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
}else{
|
|
416
|
-
fs.closeSync(fileDescriptor);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
417
|
+
const start = 0;
|
|
418
|
+
const end = totalSize - 1;
|
|
419
419
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
420
|
+
const chunkSize = end - start + 1;
|
|
421
|
+
const fileDescriptor = fs.openSync(options.file, "r");
|
|
422
|
+
|
|
423
|
+
const sendChunk = (position) => {
|
|
424
|
+
if(position > end){
|
|
425
|
+
fs.closeSync(fileDescriptor);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
423
428
|
|
|
424
|
-
|
|
425
|
-
|
|
429
|
+
const bytesToRead = Math.min(bufferSize, end - position + 1);
|
|
430
|
+
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, bytesToRead, position);
|
|
431
|
+
|
|
432
|
+
if(bytesRead > 0 && this.res.writable){
|
|
433
|
+
this[position + bytesRead > end ? 'end' : 'write'](buffer.slice(0, bytesRead), (err) => {
|
|
434
|
+
if (err) {
|
|
435
|
+
fs.closeSync(fileDescriptor);
|
|
436
|
+
} else {
|
|
437
|
+
sendChunk(position + bytesRead);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}else{
|
|
441
|
+
fs.closeSync(fileDescriptor);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
this.code(200)
|
|
446
|
+
.length(chunkSize)
|
|
447
|
+
.type(options.type);
|
|
448
|
+
|
|
449
|
+
if(options.download === true){
|
|
450
|
+
this.set('content-disposition', `attachment; filename="${options.filename}"`);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
this.writeHead();
|
|
454
|
+
sendChunk(start);
|
|
426
455
|
}
|
|
427
|
-
|
|
428
|
-
this.writeHead();
|
|
429
|
-
sendChunk(start);
|
|
430
456
|
}else{
|
|
431
457
|
options.error(400, 'This file not supported.');
|
|
432
458
|
}
|