44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import Elysia, { error, t } from 'elysia';
|
|
import { authMiddleware } from '../middlewares/auth.ts';
|
|
import { s3client } from '../config/s3client.ts';
|
|
import { PutObjectCommand } from '@aws-sdk/client-s3';
|
|
import { randomUUIDv7 } from 'bun';
|
|
|
|
export const uploadController = new Elysia({ prefix: '/upload' })
|
|
.use(authMiddleware)
|
|
.post(
|
|
'/',
|
|
async ({ body: { dest, files } }) => {
|
|
if (!files.length) return error(422, { message: 'No files' });
|
|
try {
|
|
const filesPaths: string[] = [];
|
|
for (const file of files) {
|
|
const title = `${randomUUIDv7()}.${file.name.split('.')[1]}`;
|
|
await s3client.send(
|
|
new PutObjectCommand({
|
|
Bucket: process.env.S3_BUCKET,
|
|
Key: `${dest}/${title}`,
|
|
Body: Buffer.from(await file.arrayBuffer()),
|
|
})
|
|
);
|
|
filesPaths.push(`${dest}/${title}`);
|
|
}
|
|
return filesPaths;
|
|
} catch (err) {
|
|
console.log((err as Error).message);
|
|
return error(500, { error: 'Something went wrong (File uploading)' });
|
|
}
|
|
},
|
|
{
|
|
body: t.Object({
|
|
files: t.Files({ type: ['image', 'video'] }),
|
|
dest: t.String(),
|
|
}),
|
|
response: {
|
|
200: t.Array(t.String()),
|
|
422: t.ObjectString({}),
|
|
500: t.ObjectString({}),
|
|
},
|
|
}
|
|
);
|