Add additional logging to getFileStream failures (#9242)

This commit is contained in:
Tom Moor authored and GitHub committed 2025-05-18 19:24:50 -04:00
1 parent 201fbb56eb
commit b8fd239f2e
1 file changed
+28 -3
+28 -3
View File
@@ -8,7 +8,7 @@ import invariant from "invariant";
import JWT from "jsonwebtoken";
import safeResolvePath from "resolve-path";
import env from "@server/env";
import { ValidationError } from "@server/errors";
import { InternalError, ValidationError } from "@server/errors";
import Logger from "@server/logging/Logger";
import BaseStorage from "./BaseStorage";
@@ -132,8 +132,33 @@ export default class LocalStorage extends BaseStorage {
};
}
public getFileStream(key: string, range?: { start: number; end: number }) {
return Promise.resolve(fs.createReadStream(this.getFilePath(key), range));
public async getFileStream(
key: string,
range?: { start: number; end: number }
) {
const filePath = this.getFilePath(key);
const exists = await fs.pathExists(filePath);
if (!exists) {
throw InternalError(`File not found at ${key}`);
}
if (range) {
if (
typeof range.start !== "number" ||
typeof range.end !== "number" ||
range.start < 0 ||
range.end < range.start
) {
throw ValidationError("Invalid range specified");
}
}
try {
return fs.createReadStream(filePath, range);
} catch (err) {
Logger.error(`Failed to create read stream`, err, { filePath });
throw ValidationError("Unable to read file");
}
}
public stat(key: string) {