did some reformatting

This commit is contained in:
Andreas 2024-06-23 20:00:08 +02:00
parent cdfbadb805
commit dff64b31a4

View File

@ -8,8 +8,15 @@ import { UUID, randomUUID } from "crypto";
import { mkdir, mkdirSync, writeFile } from "fs";
import { where } from "@sequelize/core";
async function writeFilesToBucket(uuid: UUID, files: any[]) {
const fileArray:{name:string, content:Promise<ReadableStreamReadResult<Uint8Array>>|ReadableStreamReadResult<Uint8Array>}[] = await Promise.all( files.map((file:File) => {
type FileArrayEntry = {
name:string;
content: Promise<ReadableStreamReadResult<Uint8Array>> | ReadableStreamReadResult<Uint8Array>
}
const fileArray:FileArrayEntry[] = await Promise.all(files.map((file: File) => {
return { 'name': (() => file.name)(), 'content': file.stream().getReader().read() };
}));
@ -22,14 +29,20 @@ async function writeFilesToBucket(uuid: UUID, files:any[]) {
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 attachment = await Attachment.create({bucket_id:uuid, filename:finalFileArray[file].name}, {include: Attachment.associations.bucket});
writeFile(
`./bucket/${uuid}/${finalFileArray[file].name}`,
Buffer.from(finalFileArray[file].content.value as Uint8Array),
(e) => { console.log(e) }
);
const attachment = await Attachment.create(
{ bucket_id: uuid, filename: finalFileArray[file].name },
{ include: Attachment.associations.bucket }
);
console.log(attachment);
}
}
async function addToPost(postid:number):Promise<Bucket>
{
async function addToPost(postid: number): Promise<Bucket> {
Post.sync();
const post = await Post.findOne({ where: { id: postid } });
@ -38,7 +51,9 @@ async function addToPost(postid:number):Promise<Bucket>
return bucket;
}
async function addToBucket(bucketid: number): Promise<Bucket> {
const bucket = await Bucket.findOne({where: {id: bucketid}, include: {association: Bucket.associations.posts}});
const bucket = await Bucket.findOne({
where: { id: bucketid }, include: { association: Bucket.associations.posts }
});
if (!bucket) throw new APIError({ status: 500, responseText: "invalid bucketid" });
return bucket;
}
@ -79,7 +94,8 @@ async function tryCreateAttachment(request: Request) {
// Sanity check the auth and associated user for authorization
if (!auth || !auth.user) throw new APIError({ status: 401, responseText: "Authentication Error" });
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)}` });
if (!auth.user.perms || !auth.user.perms.isAdmin)
throw new APIError({ status: 401, responseText: `Unauthorized ${JSON.stringify(auth.user)}` });
// Handle incomplete data or other problems
if (!files) throw new APIError({ status: 500, responseText: "Missing file" });
if (!formData) throw new APIError({ status: 500, responseText: "Empty request body" });
@ -94,18 +110,18 @@ async function tryCreateAttachment(request: Request) {
writeFilesToBucket(bucket.id, files);
const bucket2 = await Bucket.findOne({where: {id: bucket.id}, include: [Bucket.associations.posts, Bucket.associations.attachments]})
const bucket2 = await Bucket.findOne({
where: { id: bucket.id },
include: [Bucket.associations.posts, Bucket.associations.attachments]
});
return new Response(JSON.stringify({
bucket: bucket2
}), { status: 200 });
}
export async function tryFetchAttachments(request: Request) {
await Post.sync();
@ -127,6 +143,7 @@ export async function tryFetchAttachments(request: Request) {
export async function GET(request: Request) {
return await attemptAPIAction(tryFetchAttachments, request);
}
export async function POST(request: Request) {
return await attemptAPIAction(tryCreateAttachment, request);
}