diff --git a/packages/backend/package.json b/packages/backend/package.json index 5b62ac83..c728f654 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -20,9 +20,7 @@ "nitropack": "^2.9.6", "octokit": "^3.2.1", "query-registry": "^3.0.1", - "wrangler": "^3.57.1" - }, - "dependencies": { + "wrangler": "^3.57.1", "@paralleldrive/cuid2": "^2.2.2" } } diff --git a/packages/backend/server/routes/multipart/complete.post.ts b/packages/backend/server/routes/multipart/complete.post.ts new file mode 100644 index 00000000..2ca768f3 --- /dev/null +++ b/packages/backend/server/routes/multipart/complete.post.ts @@ -0,0 +1,21 @@ +import type { R2UploadedPart } from "@cloudflare/workers-types"; + +export default eventHandler(async (event) => { + const { + key: key, + id: id, + "uploaded-parts": uploadedPartsHeader, + } = getHeaders(event); + + const binding = useBinding(event); + + const upload = binding.resumeMultipartUpload(key!, id!); + const uploadedParts: R2UploadedPart[] = JSON.parse(uploadedPartsHeader!); + + const object = await upload.complete(uploadedParts); + + return { + ok: true, + key: object.key, + }; +}); diff --git a/packages/backend/server/routes/multipart/upload.post.ts b/packages/backend/server/routes/multipart/upload.post.ts deleted file mode 100644 index 1db3b5ab..00000000 --- a/packages/backend/server/routes/multipart/upload.post.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { R2UploadedPart } from "@cloudflare/workers-types"; - -export default eventHandler(async (event) => { - const { - "key": key, - "id": id, - - "part-number": partNumberHeader, - - "uploaded-parts": uploadedPartsHeader - } = getHeaders(event); - - const partNumber = Number(partNumberHeader) - const binding = - event.context.cloudflare.env.ENV === "production" - ? event.context.cloudflare.env.PROD_CR_BUCKET - : event.context.cloudflare.env.CR_BUCKET; - - const upload = binding.resumeMultipartUpload(key!, id!) - if (uploadedPartsHeader) { - const uploadedParts: R2UploadedPart[] = JSON.parse(uploadedPartsHeader!) - - const object = await upload.complete(uploadedParts) - return { - ok: true, - key: object.key - } - } else { - const buffer = (await readRawBody(event, false))! - const part = await upload.uploadPart(partNumber, buffer) - - return { - ok: true, - part - } - } -}) - diff --git a/packages/backend/server/routes/multipart/upload.put.ts b/packages/backend/server/routes/multipart/upload.put.ts new file mode 100644 index 00000000..442686aa --- /dev/null +++ b/packages/backend/server/routes/multipart/upload.put.ts @@ -0,0 +1,19 @@ +export default eventHandler(async (event) => { + const { + key: key, + id: id, + "part-number": partNumberHeader, + } = getHeaders(event); + + const partNumber = Number(partNumberHeader); + const binding = useBinding(event); + + const upload = binding.resumeMultipartUpload(key!, id!); + const buffer = (await readRawBody(event, false))!; + const part = await upload.uploadPart(partNumber, buffer); + + return { + ok: true, + part, + }; +}); diff --git a/packages/cli/index.ts b/packages/cli/index.ts index b04d782f..5ec71015 100644 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -42,6 +42,7 @@ const apiUrl = process.env.API_URL ?? API_URL; const publishUrl = new URL("/publish", apiUrl); const createMultipart = new URL("/multipart/create", apiUrl); const uploadMultipart = new URL("/multipart/upload", apiUrl); +const completeMultipart = new URL("/multipart/complete", apiUrl); const main = defineCommand({ meta: { @@ -402,7 +403,7 @@ const main = defineCommand({ const chunk = file.slice(start, end); const uploadMultipartRes = await fetch(uploadMultipart, { - method: "POST", + method: "PUT", headers: { key: uploadKey, id: uploadId, @@ -420,8 +421,7 @@ const main = defineCommand({ const { part } = await uploadMultipartRes.json(); uploadedParts.push(part); } - console.log('uploaded-parts', uploadedParts) - const completeMultipartRes = await fetch(uploadMultipart, { + const completeMultipartRes = await fetch(completeMultipart, { method: "POST", headers: { key: uploadKey, @@ -437,7 +437,7 @@ const main = defineCommand({ } const { key: completionKey } = await completeMultipartRes.json(); - console.log(name, `object:${completionKey}`) + formData.set(name, `object:${completionKey}`); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1624442..f16ca0f9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,10 +67,6 @@ importers: version: 1.1.0 packages/backend: - dependencies: - '@paralleldrive/cuid2': - specifier: ^2.2.2 - version: 2.2.2 devDependencies: '@cloudflare/workers-types': specifier: ^4.20240512.0 @@ -81,6 +77,9 @@ importers: '@octokit/app': specifier: ^14.1.0 version: 14.1.0 + '@paralleldrive/cuid2': + specifier: ^2.2.2 + version: 2.2.2 '@pkg-pr-new/utils': specifier: workspace:^ version: link:../utils