Skip to content

Commit

Permalink
fix(nestjs-tools-fastify-upload): handle undefined filename by genera…
Browse files Browse the repository at this point in the history
…ting a random one
  • Loading branch information
getlarge committed Nov 6, 2024
1 parent 1d2ed24 commit b076fd9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/fastify-upload/src/lib/storage/stream-storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomBytes } from 'node:crypto';
import { createReadStream, createWriteStream } from 'node:fs';
import { rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
Expand All @@ -23,11 +24,13 @@ const temporaryFileOutput = (filename: string) => {
export class StreamStorage extends Storage<StreamStorageFile> {
async handleFile(file: MultipartFile): Promise<StreamStorageFile> {
const { encoding, mimetype, fieldname } = file;
// looks like the file.filename type is incorrect, file.filename could be undefined
const filename = file.filename ?? randomBytes(16).toString('hex');
/**
* force the stream to be consumed as required by Fastify and Busboy
* @see https://github.com/fastify/fastify-multipart?tab=readme-ov-file#usage
* */
const output = temporaryFileOutput(file.filename);
**/
const output = temporaryFileOutput(filename);
await pipeline(file.file, createWriteStream(output));
const stream = createReadStream(output);
return Promise.resolve({
Expand All @@ -36,7 +39,7 @@ export class StreamStorage extends Storage<StreamStorageFile> {
encoding,
mimetype,
fieldname,
originalFilename: file.filename,
originalFilename: filename,
});
}

Expand Down

0 comments on commit b076fd9

Please sign in to comment.