Spaces:
Runtime error
Runtime error
File size: 1,109 Bytes
c0dd699 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import { legacyTypeIntoZod, OpenAPIRoute } from 'chanfana';
import { Context } from 'hono';
import { z } from 'zod';
import { Bindings, FileBody } from '../types';
export class DownloadFile extends OpenAPIRoute {
schema = {
summary: '下载R2上的图片',
description: '',
request: {
params: z.object({
file_name: z.string(),
}),
},
response: {
200: {
description: '下载链接',
content: {
'image/*': {
schema: legacyTypeIntoZod({ file: FileBody({ format: 'binary' }) }),
},
},
},
},
};
async handle(c: Context<{ Bindings: Bindings }>) {
const data = await this.getValidatedData<typeof this.schema>();
const prefix = 'origin';
const pathname = `${prefix}/${data.params.file_name}`;
const object = await c.env.MY_BUCKET.get(pathname);
if (object === null) {
c.status(404);
return c.json({ error: 'not found' });
}
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.forEach((value, key) => {
c.header(key, value);
});
c.header('etag', object.httpEtag);
return c.body(object.body);
}
}
|