2022-08-27 19:25:14 +02:00
|
|
|
import fp from 'fastify-plugin';
|
|
|
|
import fileAdapter from '../services/file-adapter.js';
|
2021-07-01 17:51:08 +02:00
|
|
|
|
2022-08-27 19:25:14 +02:00
|
|
|
export default fp(async (fastify) => {
|
2022-08-25 11:16:52 +02:00
|
|
|
fastify.decorate('attachment', async (req, reply) => {
|
2023-04-21 13:51:32 +02:00
|
|
|
req.secret = {
|
|
|
|
files: [],
|
|
|
|
};
|
2021-07-01 17:51:08 +02:00
|
|
|
|
2024-01-27 15:33:08 +01:00
|
|
|
const { files, isPublic } = await req.body;
|
2022-08-22 15:18:58 +02:00
|
|
|
|
2024-01-27 15:33:08 +01:00
|
|
|
if (!isPublic && files?.length) {
|
2022-09-07 19:19:35 +02:00
|
|
|
for (const file of files) {
|
|
|
|
try {
|
|
|
|
const imageData = await fileAdapter.upload(file.content);
|
|
|
|
|
|
|
|
req.secret.files.push({
|
|
|
|
type: file.type,
|
|
|
|
ext: file.ext,
|
|
|
|
key: imageData.key,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message);
|
|
|
|
|
|
|
|
return reply.code(403).send({
|
|
|
|
type: 'files',
|
|
|
|
error: `Something went wrong uploading your files. Please try again.`,
|
|
|
|
});
|
|
|
|
}
|
2022-08-30 22:01:55 +02:00
|
|
|
}
|
2021-07-01 17:51:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|