|
| 1 | +import { RuntimeError, ScriptContext } from "../_gen/scripts/complete.ts"; |
| 2 | +import { |
| 3 | + completeMultipartUpload, |
| 4 | + getMultipartUploadParts, |
| 5 | + keyExists, |
| 6 | +} from "../utils/bucket.ts"; |
| 7 | +import { getConfig } from "../utils/config_defaults.ts"; |
| 8 | +import { getKey, prismaToOutputWithFiles, Upload } from "../utils/types.ts"; |
| 9 | + |
| 10 | +export interface Request { |
| 11 | + uploadId: string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface Response { |
| 15 | + upload: Upload; |
| 16 | +} |
| 17 | + |
| 18 | +export async function run( |
| 19 | + ctx: ScriptContext, |
| 20 | + req: Request, |
| 21 | +): Promise<Response> { |
| 22 | + const config = getConfig(ctx.userConfig); |
| 23 | + |
| 24 | + const newUpload = await ctx.db.$transaction(async (db) => { |
| 25 | + // Find the upload by ID |
| 26 | + const upload = await db.upload.findFirst({ |
| 27 | + where: { |
| 28 | + id: req.uploadId, |
| 29 | + }, |
| 30 | + select: { |
| 31 | + id: true, |
| 32 | + userId: true, |
| 33 | + bucket: true, |
| 34 | + contentLength: true, |
| 35 | + files: true, |
| 36 | + createdAt: true, |
| 37 | + updatedAt: true, |
| 38 | + completedAt: true, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + // Error if the upload wasn't prepared |
| 43 | + if (!upload) { |
| 44 | + throw new RuntimeError( |
| 45 | + "upload_not_found", |
| 46 | + { |
| 47 | + meta: { uploadId: req.uploadId }, |
| 48 | + }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + // Check with S3 to see if the files were uploaded |
| 53 | + const fileExistencePromises = upload.files.map( |
| 54 | + async (file) => { |
| 55 | + // If the file was uploaded in parts, complete the multipart upload |
| 56 | + if (file.multipartUploadId) { |
| 57 | + try { |
| 58 | + const parts = await getMultipartUploadParts( |
| 59 | + config.s3, |
| 60 | + getKey(upload.id, file.path), |
| 61 | + file.multipartUploadId, |
| 62 | + ); |
| 63 | + if (parts.length === 0) return false; |
| 64 | + |
| 65 | + await completeMultipartUpload( |
| 66 | + config.s3, |
| 67 | + getKey(upload.id, file.path), |
| 68 | + file.multipartUploadId, |
| 69 | + parts, |
| 70 | + ); |
| 71 | + } catch (e) { |
| 72 | + throw new RuntimeError( |
| 73 | + "multipart_upload_completion_fail", |
| 74 | + { cause: e }, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } else { |
| 80 | + // Check if the file exists |
| 81 | + return await keyExists(config.s3, getKey(upload.id, file.path)); |
| 82 | + } |
| 83 | + }, |
| 84 | + ); |
| 85 | + const fileExistence = await Promise.all(fileExistencePromises); |
| 86 | + const filesAllExist = fileExistence.every(Boolean); |
| 87 | + if (!filesAllExist) { |
| 88 | + const missingFiles = upload.files.filter((_, i) => !fileExistence[i]); |
| 89 | + throw new RuntimeError( |
| 90 | + "files_not_uploaded", |
| 91 | + { |
| 92 | + meta: { |
| 93 | + uploadId: req.uploadId, |
| 94 | + missingFiles: missingFiles.map((file) => file.path), |
| 95 | + }, |
| 96 | + }, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // Error if `complete` was already called with this ID |
| 101 | + if (upload.completedAt !== null) { |
| 102 | + throw new RuntimeError( |
| 103 | + "upload_already_completed", |
| 104 | + { |
| 105 | + meta: { uploadId: req.uploadId }, |
| 106 | + }, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // Update the upload to mark it as completed |
| 111 | + const completedUpload = await db.upload.update({ |
| 112 | + where: { |
| 113 | + id: req.uploadId, |
| 114 | + }, |
| 115 | + data: { |
| 116 | + completedAt: new Date().toISOString(), |
| 117 | + }, |
| 118 | + select: { |
| 119 | + id: true, |
| 120 | + userId: true, |
| 121 | + bucket: true, |
| 122 | + contentLength: true, |
| 123 | + files: true, |
| 124 | + createdAt: true, |
| 125 | + updatedAt: true, |
| 126 | + completedAt: true, |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + return completedUpload; |
| 131 | + }); |
| 132 | + |
| 133 | + return { |
| 134 | + upload: prismaToOutputWithFiles(newUpload), |
| 135 | + }; |
| 136 | +} |
0 commit comments