Skip to content

Commit

Permalink
Fixed the GET Range
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Aug 1, 2017
1 parent 647a8c3 commit 3b2d11f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/server/v2/commands/Get.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ var RangedStream = (function (_super) {
}
RangedStream.prototype._transform = function (chunk, encoding, callback) {
if (this.nb < this.min) {
var lastNb = this.nb;
this.nb += chunk.length;
if (this.nb > this.min) {
chunk = chunk.slice(this.nb - this.min);
var start = this.min - lastNb;
chunk = chunk.slice(start, this.nb > this.max ? this.max - this.min + 1 + start : undefined);
callback(null, chunk);
}
else
Expand All @@ -38,7 +40,7 @@ var RangedStream = (function (_super) {
else {
this.nb += chunk.length;
if (this.nb > this.max)
chunk = chunk.slice(0, this.max - (this.nb - chunk.length));
chunk = chunk.slice(0, this.max - (this.nb - chunk.length) + 1);
callback(null, chunk);
}
};
Expand Down Expand Up @@ -90,7 +92,7 @@ var default_1 = (function () {
ctx.setCode(WebDAVRequest_1.HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes');
ctx.response.setHeader('Content-Type', mimeType);
ctx.response.setHeader('Content-Length', (max - min).toString());
ctx.response.setHeader('Content-Length', Math.min(size, max - min + 1).toString());
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*');
rstream.on('end', callback);
rstream.pipe(new RangedStream(min, max)).pipe(ctx.response);
Expand Down
10 changes: 6 additions & 4 deletions src/server/v2/commands/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class RangedStream extends Transform
this.nb = 0;
}

_transform(chunk: any, encoding: string, callback: Function)
_transform(chunk : string | Buffer, encoding : string, callback : Function)
{
if(this.nb < this.min)
{
const lastNb = this.nb;
this.nb += chunk.length;
if(this.nb > this.min)
{
chunk = chunk.slice(this.nb - this.min);
const start = this.min - lastNb;
chunk = chunk.slice(start, this.nb > this.max ? this.max - this.min + 1 + start : undefined);
callback(null, chunk);
}
else
Expand All @@ -36,7 +38,7 @@ class RangedStream extends Transform
{
this.nb += chunk.length;
if(this.nb > this.max)
chunk = chunk.slice(0, this.max - (this.nb - chunk.length));
chunk = chunk.slice(0, this.max - (this.nb - chunk.length) + 1);
callback(null, chunk);
}
}
Expand Down Expand Up @@ -100,7 +102,7 @@ export default class implements HTTPMethod
ctx.setCode(HTTPCodes.PartialContent);
ctx.response.setHeader('Accept-Ranges', 'bytes')
ctx.response.setHeader('Content-Type', mimeType)
ctx.response.setHeader('Content-Length', (max - min).toString())
ctx.response.setHeader('Content-Length', Math.min(size, max - min + 1).toString())
ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*')

rstream.on('end', callback);
Expand Down

0 comments on commit 3b2d11f

Please sign in to comment.