Skip to content

Commit 0c72e28

Browse files
committed
Allow uploadId to be skipped if file is provided
1 parent 3b6efc2 commit 0c72e28

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/components/file/dto/upload.dto.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ export abstract class RequestUploadOutput {
2323
@InputType()
2424
export abstract class CreateDefinedFileVersionInput {
2525
@IdField({
26-
description: 'The ID returned from the `requestFileUpload` mutation',
26+
description: stripIndent`
27+
The ID returned from the \`requestFileUpload\` mutation.
28+
This _can_ be skipped if \`file\` is provided.
29+
`,
30+
nullable: true,
2731
})
28-
readonly uploadId: ID;
32+
readonly uploadId?: ID;
2933

3034
@Field(() => UploadScalar, {
3135
description: stripIndent`

src/components/file/file.service.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,15 @@ export class FileService {
262262
const {
263263
parentId,
264264
file: uploadingFile,
265-
uploadId,
265+
uploadId: uploadIdInput,
266266
mimeType: mimeTypeOverride,
267267
media,
268268
} = input;
269+
if (!uploadIdInput && !uploadingFile) {
270+
throw new InputException('Upload ID is required', 'uploadId');
271+
}
272+
273+
const uploadId = uploadIdInput ?? (await generateId());
269274

270275
const parentType = await this.validateParentNode(
271276
parentId,
@@ -274,12 +279,14 @@ export class FileService {
274279
);
275280

276281
if (uploadingFile) {
277-
const prevExists = await this.bucket.headObject(uploadId).catch((e) => {
278-
if (e instanceof NotFoundException) {
279-
return false;
280-
}
281-
throw e;
282-
});
282+
const prevExists = uploadIdInput
283+
? await this.bucket.headObject(uploadId).catch((e) => {
284+
if (e instanceof NotFoundException) {
285+
return false;
286+
}
287+
throw e;
288+
})
289+
: false;
283290
if (prevExists) {
284291
throw new InputException(
285292
'A file with this ID already exists. Request an new upload ID.',

0 commit comments

Comments
 (0)