Skip to content

Commit

Permalink
fix(backend): FileServerServiceでレンジリクエストの場合に適切なレスポンスコードが返らない問題を修正 (#1…
Browse files Browse the repository at this point in the history
…3701)

* return 206 for every ranged response - fixes kokonect-link#494

(cherry picked from commit 92eec2178fd103e9ea2bcd646aacab1fb496a33b)

* detect size of remote files - fixes kokonect-link#494

without this, remote files are assumed to have size 0 (even if we just
downloaded them!) and the range-related code won't run

(cherry picked from commit 960f4fcff78a1f019c9a9377853fcd90dbfb7575)

---------

Co-authored-by: dakkar <dakkar@thenautilus.net>
  • Loading branch information
kakkokari-gtyih and dakkar authored Apr 14, 2024
1 parent 48a7679 commit 7cf0c18
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/backend/src/server/FileServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class FileServerService {
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
} else {
image = {
data: fs.createReadStream(file.path),
Expand Down Expand Up @@ -263,7 +264,6 @@ export class FileServerService {
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
console.log(end);
if (end > file.file.size) {
end = file.file.size - 1;
}
Expand Down Expand Up @@ -433,6 +433,7 @@ export class FileServerService {
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize);
reply.code(206);
} else {
image = {
data: fs.createReadStream(file.path),
Expand Down Expand Up @@ -529,6 +530,9 @@ export class FileServerService {
if (!file.storedInternal) {
if (!(file.isLink && file.uri)) return '204';
const result = await this.downloadAndDetectTypeFromUrl(file.uri);
if (!file.size) {
file.size = (await fs.promises.stat(result.path)).size;
}
return {
...result,
url: file.uri,
Expand Down

0 comments on commit 7cf0c18

Please sign in to comment.