merge: Check for truncated uploads in Mastodon API (!998)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/998

Approved-by: Marie <github@yuugi.dev>
Approved-by: dakkar <dakkar@thenautilus.net>
This commit is contained in:
Marie 2025-05-09 09:27:42 +00:00
commit 093f67ae55
2 changed files with 10 additions and 3 deletions

View file

@ -30,13 +30,20 @@ export function createTempDir(): Promise<[string, () => void]> {
}); });
} }
export async function saveToTempFile(stream: NodeJS.ReadableStream): Promise<string> { export async function saveToTempFile(stream: NodeJS.ReadableStream & { truncated?: boolean }): Promise<[string, () => void]> {
const [filepath, cleanup] = await createTemp(); const [filepath, cleanup] = await createTemp();
try { try {
await pipeline(stream, fs.createWriteStream(filepath)); await pipeline(stream, fs.createWriteStream(filepath));
return filepath;
} catch (e) { } catch (e) {
cleanup(); cleanup();
throw e; throw e;
} }
if (stream.truncated) {
cleanup();
throw new Error('Read failed: input stream truncated');
}
return [filepath, cleanup];
} }

View file

@ -54,7 +54,7 @@ export class ServerUtilityService {
} }
} else { // Otherwise it's a file } else { // Otherwise it's a file
try { try {
const filepath = await saveToTempFile(part.file); const [filepath] = await saveToTempFile(part.file);
const tmpUploads = (request.tmpUploads ??= []); const tmpUploads = (request.tmpUploads ??= []);
tmpUploads.push(filepath); tmpUploads.push(filepath);