Skip to content

Commit a956284

Browse files
committed
chore(storage): attempt to serve file from route url
1 parent d3bf027 commit a956284

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

pages/storage.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const items = [[{
120120
}"
121121
class="overflow-hidden relative"
122122
>
123-
<img v-if="file.httpMetadata?.contentType?.startsWith('image/') && file.body" :src="`data:${file.httpMetadata.contentType};base64,${(file.body)}`" class="h-36 w-full object-cover">
123+
<!-- <img v-if="file.httpMetadata?.contentType?.startsWith('image/')" :src="`/api/storage/${file.key}`" class="h-36 w-full object-cover"> -->
124124
<div class="h-36 w-full flex items-center justify-center p-2 text-center">
125125
{{ file.key }}
126126
</div>

server/api/storage/[key].get.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { ReadableStreamDefaultReader } from '@cloudflare/workers-types/experimental'
2-
import { useValidatedParams, useValidatedQuery, z, zh } from 'h3-zod'
1+
import { useValidatedParams, z } from 'h3-zod'
32

43
export default eventHandler(async (event) => {
54
await requireUserSession(event)
65

7-
86
const { key } = await useValidatedParams(event, {
97
key: z.string().min(1).max(100)
108
})
11-
const { populate } = await useValidatedQuery(event, {
12-
populate: zh.boolAsString.optional().default('false')
13-
})
149

1510
// List files for the current user
1611

@@ -21,14 +16,8 @@ export default eventHandler(async (event) => {
2116
throw createError({ message: 'File not found', statusCode: 404 })
2217
}
2318

24-
if (populate) {
25-
const bodyReader: ReadableStreamDefaultReader<Uint8Array> = object.body.getReader()
26-
const body = await processStream(bodyReader)
27-
return {
28-
...object,
29-
body: Buffer.from(body).toString('base64')
30-
}
31-
}
19+
setHeader(event, 'Content-Type', object.httpMetadata!.contentType!)
20+
setHeader(event, 'Content-Length', object.size)
3221

33-
return object
22+
return object.body.getReader()
3423
})

0 commit comments

Comments
 (0)