fix(backend): FileServerServiceでレンジリクエストの場合に適切なレスポンスコードが返らない問題を修正 (#13701)

* return 206 for every ranged response - fixes #494

(cherry picked from commit 92eec2178f)

* detect size of remote files - fixes #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 960f4fcff7)

---------

Co-authored-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
かっこかり 2024-04-14 10:22:03 +09:00 committed by GitHub
parent 48a7679b8a
commit 7cf0c18f83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -194,6 +194,7 @@ export class FileServerService {
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`); reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes'); reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize); reply.header('Content-Length', chunksize);
reply.code(206);
} else { } else {
image = { image = {
data: fs.createReadStream(file.path), data: fs.createReadStream(file.path),
@ -263,7 +264,6 @@ export class FileServerService {
const parts = range.replace(/bytes=/, '').split('-'); const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10); const start = parseInt(parts[0], 10);
let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1; let end = parts[1] ? parseInt(parts[1], 10) : file.file.size - 1;
console.log(end);
if (end > file.file.size) { if (end > file.file.size) {
end = file.file.size - 1; end = file.file.size - 1;
} }
@ -433,6 +433,7 @@ export class FileServerService {
reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`); reply.header('Content-Range', `bytes ${start}-${end}/${file.file.size}`);
reply.header('Accept-Ranges', 'bytes'); reply.header('Accept-Ranges', 'bytes');
reply.header('Content-Length', chunksize); reply.header('Content-Length', chunksize);
reply.code(206);
} else { } else {
image = { image = {
data: fs.createReadStream(file.path), data: fs.createReadStream(file.path),
@ -529,6 +530,9 @@ export class FileServerService {
if (!file.storedInternal) { if (!file.storedInternal) {
if (!(file.isLink && file.uri)) return '204'; if (!(file.isLink && file.uri)) return '204';
const result = await this.downloadAndDetectTypeFromUrl(file.uri); const result = await this.downloadAndDetectTypeFromUrl(file.uri);
if (!file.size) {
file.size = (await fs.promises.stat(result.path)).size;
}
return { return {
...result, ...result,
url: file.uri, url: file.uri,