Skip to content

Commit

Permalink
fix: apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
Teages committed Apr 12, 2024
1 parent 6be9735 commit b591e36
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 215 deletions.
55 changes: 30 additions & 25 deletions playground/pages/blob.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,51 @@ async function uploadFiles(files: File[]) {
const chunks = Math.ceil(file.size / chunkSize)
const uploaded: BlobUploadedPart[] = []
const { pathname, uploadId } = await $fetch<{pathname: string, uploadId: string}>('/api/blob/mpu', {
method: 'POST',
query: {
action: 'create',
pathname: file.name,
const { pathname, uploadId } = await $fetch<{pathname: string, uploadId: string}>(
`/api/blob/multipart/${file.name}`,
{
method: 'POST',
}
})
)
for (let i = 0; i < chunks; i += 1) {
const start = i * chunkSize
const end = Math.min(start + chunkSize, file.size)
const partNumber = i + 1
const chunk = file.slice(start, end)
const part = await $fetch<BlobUploadedPart>(`/api/blob/mpu/${pathname}`, {
params: {},
method: 'PUT',
body: await chunk.arrayBuffer(),
query: {
partNumber,
uploadId,
const part = await $fetch<BlobUploadedPart>(
`/api/blob/multipart/${pathname}`,
{
method: 'PUT',
query: {
uploadId,
partNumber,
},
body: chunk,
}
})
)
// optional: verify the etag and reupload if not match
uploaded.push(part)
}
const complete = await $fetch<SerializeObject<BlobObject>>('/api/blob/mpu', {
method: 'POST',
query: {
action: 'complete',
pathname,
uploadId,
},
body: {
parts: uploaded,
},
})
const complete = await $fetch<SerializeObject<BlobObject>>(
'/api/blob/multipart/complete',
{
method: 'POST',
query: {
pathname,
uploadId,
},
body: {
parts: uploaded,
},
}
)
console.log(complete)
uploadedFiles.push(complete)
}
Expand Down
55 changes: 0 additions & 55 deletions playground/server/api/blob/mpu/index.post.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { z } from 'zod'

export default eventHandler(async (event) => {
const hub = hubBlob()

const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)
Expand All @@ -11,12 +9,15 @@ export default eventHandler(async (event) => {
uploadId: z.string(),
}).parse)

const mpu = hub.resumeMultipartUpload(pathname, uploadId)
const { resumeMultipartUpload } = hubBlob()
const mpu = resumeMultipartUpload(pathname, uploadId)

try {
return await mpu.abort()
await mpu.abort()
}
catch (e: any) {
throw createError({ status: 400, message: e.message })
}

return sendNoContent(event)
})
23 changes: 23 additions & 0 deletions playground/server/api/blob/multipart/[...pathname].post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { z } from 'zod'

export default eventHandler(async (event) => {
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)

const options = await readValidatedBody(event, z.record(z.string(), z.any()).optional().parse)

const { createMultipartUpload } = hubBlob()
try {
const object = await createMultipartUpload(pathname, options)
return {
uploadId: object.uploadId,
pathname: object.pathname,
}
} catch (e: any) {
throw createError({
statusCode: 400,
message: e.message
})
}
})
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import { z } from 'zod'

async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) {
const result = new Uint8Array(streamSize)
let bytesRead = 0
const reader = stream.getReader()
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
result.set(value, bytesRead)
bytesRead += value.length
}
return result
}

export default eventHandler(async (event) => {
const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
Expand All @@ -32,8 +16,8 @@ export default eventHandler(async (event) => {
const body = await streamToArrayBuffer(stream, contentLength)


const hub = hubBlob()
const mpu = hub.resumeMultipartUpload(pathname, uploadId)
const { resumeMultipartUpload } = hubBlob()
const mpu = resumeMultipartUpload(pathname, uploadId)

try {
return await mpu.uploadPart(partNumber, body)
Expand Down
27 changes: 27 additions & 0 deletions playground/server/api/blob/multipart/complete.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod'

export default eventHandler(async (event) => {
const { pathname, uploadId } = await getValidatedQuery(event, z.object({
pathname: z.string().min(1),
uploadId: z.string().min(1),
}).parse)

const { parts } = await readValidatedBody(event,z.object({
parts: z.array(z.object({
partNumber: z.number(),
etag: z.string(),
}))
}).parse)

const { resumeMultipartUpload } = hubBlob()
const mpu = resumeMultipartUpload(pathname, uploadId)
try {
const object = await mpu.complete(parts)
return object
} catch (e: any) {
throw createError({
statusCode: 400,
message: e.message
})
}
})
15 changes: 15 additions & 0 deletions playground/server/utils/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) {
const result = new Uint8Array(streamSize)
let bytesRead = 0
const reader = stream.getReader()
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
result.set(value, bytesRead)
bytesRead += value.length
}
return result
}
19 changes: 2 additions & 17 deletions src/runtime/server/api/_hub/blob/[...pathname].put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ import { z } from 'zod'
import { hubBlob } from '../../../utils/blob'
import { requireNuxtHubAuthorization } from '../../../utils/auth'
import { requireNuxtHubFeature } from '../../../utils/features'

async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) {
const result = new Uint8Array(streamSize)
let bytesRead = 0
const reader = stream.getReader()
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
result.set(value, bytesRead)
bytesRead += value.length
}
return result
}
import { streamToArrayBuffer } from '../../../internal/utils/stream'

export default eventHandler(async (event) => {
await requireNuxtHubAuthorization(event)
requireNuxtHubFeature('blob')

const { pathname } = await getValidatedRouterParams(event, z.object({
pathname: z.string().min(1)
}).parse)
Expand Down
59 changes: 0 additions & 59 deletions src/runtime/server/api/_hub/blob/mpu/index.post.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createError, eventHandler } from 'h3'
import { createError, eventHandler, sendNoContent } from 'h3'
import { z } from 'zod'
import { hubBlob } from '../../../../utils/blob'
import { requireNuxtHubAuthorization } from '../../../../utils/auth'
Expand All @@ -17,15 +17,18 @@ export default eventHandler(async (event) => {
}).parse)


const blob = hubBlob()
const mpu = blob.resumeMultipartUpload(pathname, uploadId)
const { resumeMultipartUpload } = hubBlob()
const { abort } = resumeMultipartUpload(pathname, uploadId)

try {
await mpu.abort()
} catch (e: any) {
await abort()
}
catch (e: any) {
throw createError({
statusCode: 500,
message: `Storage error: ${e.message}`
})
}

return sendNoContent(event)
})
Loading

0 comments on commit b591e36

Please sign in to comment.