fix(telegram): warn on oversized media
This commit is contained in:
1 parent
aae5926db9
commit
9254f4b738
11 files changed
+149
-38
No files matched your search
@@ -52,6 +52,9 @@ describe("media store", () => {
|
||||
await expect(store.saveMediaBuffer(huge)).rejects.toThrow(
|
||||
"Media exceeds 5MB limit",
|
||||
);
|
||||
await expect(store.saveMediaBuffer(huge)).rejects.toBeInstanceOf(
|
||||
store.MediaTooLargeError,
|
||||
);
|
||||
});
|
||||
|
||||
it("copies local files and cleans old media", async () => {
|
||||
|
||||
+14
-4
@@ -9,6 +9,18 @@ import { detectMime, extensionForMime } from "./mime.js";
|
||||
|
||||
const MEDIA_DIR = path.join(CONFIG_DIR, "media");
|
||||
const MAX_BYTES = 5 * 1024 * 1024; // 5MB default
|
||||
|
||||
export class MediaTooLargeError extends Error {
|
||||
maxBytes: number;
|
||||
|
||||
constructor(maxBytes: number) {
|
||||
const limitMb = (maxBytes / (1024 * 1024)).toFixed(0);
|
||||
super(`Media exceeds ${limitMb}MB limit`);
|
||||
this.name = "MediaTooLargeError";
|
||||
this.maxBytes = maxBytes;
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULT_TTL_MS = 2 * 60 * 1000; // 2 minutes
|
||||
|
||||
export function getMediaDir() {
|
||||
@@ -144,7 +156,7 @@ export async function saveMediaSource(
|
||||
throw new Error("Media path is not a file");
|
||||
}
|
||||
if (stat.size > MAX_BYTES) {
|
||||
throw new Error("Media exceeds 5MB limit");
|
||||
throw new MediaTooLargeError(MAX_BYTES);
|
||||
}
|
||||
const buffer = await fs.readFile(source);
|
||||
const mime = await detectMime({ buffer, filePath: source });
|
||||
@@ -162,9 +174,7 @@ export async function saveMediaBuffer(
|
||||
maxBytes = MAX_BYTES,
|
||||
): Promise<SavedMedia> {
|
||||
if (buffer.byteLength > maxBytes) {
|
||||
throw new Error(
|
||||
`Media exceeds ${(maxBytes / (1024 * 1024)).toFixed(0)}MB limit`,
|
||||
);
|
||||
throw new MediaTooLargeError(maxBytes);
|
||||
}
|
||||
const dir = path.join(MEDIA_DIR, subdir);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
|
||||
Reference in new issue
Block a user