-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path+handler.ts
72 lines (66 loc) · 1.96 KB
/
+handler.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import fs from 'node:fs';
import path from 'node:path';
import { pipeline } from 'node:stream/promises';
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
export default (async (app) => {
/*
$ curl --request POST \
--header "Content-Type: multipart/form-data" \
--form "image=@/Users/path/to/name.png" \
--url http://127.0.0.1:3000/api/file-uploads
*/
app.post(
'',
{
schema: {
response: {
'2xx': {
message: Type.String(),
url: Type.String(),
},
},
},
},
async (req, reply) => {
const data = await req.file();
if (!data) return reply.badRequest();
const public_id = path.basename(data.filename, path.extname(data.filename));
const folder = data.mimetype.includes('image') ? 'images' : undefined;
if (process.env.NODE_ENV === 'production') {
await pipeline(data.file, app.cloudinary.uploader.upload_stream({ public_id, folder }));
} else {
const dir = path.resolve(import.meta.dirname, '../../../dist');
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
await pipeline(data.file, fs.createWriteStream(path.resolve(dir, data.filename)));
}
return reply.send({
message: 'OK',
url: app.cloudinary.url(folder ? `${folder}/${public_id}` : public_id),
});
},
);
/*
$ curl --request GET \
--url http://127.0.0.1:3000/api/file-uploads?publicId=images/avatar
*/
app.get(
'',
{
schema: {
querystring: Type.Object({
publicId: Type.String(),
}),
response: {
'2xx': {
message: Type.String(),
url: Type.String(),
},
},
},
},
async (req, reply) => {
return reply.send({ message: 'OK', url: app.cloudinary.url(req.query.publicId) });
},
);
}) as FastifyPluginAsyncTypebox;