implemented file upload for attachments

This commit is contained in:
2024-06-11 02:23:22 +02:00
parent e841ba8938
commit 86eb422dd5
23 changed files with 332 additions and 147 deletions
+39 -18
View File
@@ -3,18 +3,20 @@
import { APIError, attemptAPIAction } from "@/util/api/error";
import { Auth, Post, PostTag, Tag, User } from "@/model/Models";
import { cookies } from "next/headers";
import { Attachment } from "@/model/Attachment";
import { randomUUID } from "crypto";
import { mkdir, mkdirSync, writeFile } from "fs";
async function tryCreateAttachment(request: Request) {
// Make sure the DB is ready
await PostTag.sync();
await Tag.sync();
await Attachment.sync();
await Post.sync();
// Prepare data
const requestBody = await request.json();
const formData = await request.formData();
const authCkie = await cookies().get("auth");
// Sanity check auth cookie
@@ -41,26 +43,45 @@ async function tryCreateAttachment(request: Request) {
if (!auth || !auth.user) throw new APIError({ status: 401, responseText: "Authentication Error" });
// Handle incomplete data or other problems
if (!requestBody) throw new APIError({ status: 500, responseText: "Empty request body" });
if (!requestBody.title) throw new APIError({ status: 500, responseText: "Missing post title" });
if (!requestBody.content) throw new APIError({ status: 500, responseText: "Missing post content" });
if (!formData) throw new APIError({ status: 500, responseText: "Empty request body" });
const files:any[] = formData.getAll('files')
if (!files) throw new APIError({ status: 500, responseText: "Missing file" });
if (!auth.user.id) throw new APIError({ status: 500, responseText: "Missing user id" });
if (!auth.user.perms || !auth.user.perms.isAdmin) throw new APIError({ status: 401, responseText: `Unauthorized ${JSON.stringify(auth.user)}` });
// Create a new Post in the database
const post = await Post.create(
{
content: requestBody.content,
user_id: auth.user.id,
title: requestBody.title,
},{
include: {
association: Post.associations.user
// peepee
const uuid = randomUUID()
const fileArray:{name:string, content:Promise<ReadableStreamReadResult<Uint8Array>>|ReadableStreamReadResult<Uint8Array>}[] = await Promise.all( files.map((file:File) => {
return {'name':( ()=>file.name)(), 'content':file.stream().getReader().read()};
}));
let finalFileArray:{name:string,content:ReadableStreamReadResult<Uint8Array>}[] = await (async () => {
for(let file in fileArray){
fileArray[file].content = await fileArray[file].content
}
}).then(post=>post.reload())
return [...fileArray as {name:string,content:ReadableStreamReadResult<Uint8Array>}[]]
})() ;
mkdirSync(`./bucket/${uuid}/`)
for(let file in finalFileArray){
writeFile(`./bucket/${uuid}/${finalFileArray[file].name}`,Buffer.from(finalFileArray[file].content.value as Uint8Array),(e)=>{console.log(e)})
}
// const kanker = files.map(parseFiles)
// console.log(await kanker[0]);
return new Response(JSON.stringify({
'files': fileArray,
'uuid': uuid,
}), { status: 200 });
// Return the response
return new Response(JSON.stringify(post), { status: 200 });
}