add forgotten img api

This commit is contained in:
Alexander Bocken 2023-06-27 19:27:00 +02:00
parent 6f4cf0a13c
commit 71fdcf9ffd
Signed by: Alexander
GPG Key ID: 1D237BE83F9B05E8
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import { writeFileSync } from 'fs';
import path from 'path'
import type { RequestHandler } from '@sveltejs/kit';
import { BEARER_TOKEN } from '$env/static/private'
import { error } from '@sveltejs/kit';
export const POST = (async ({ request }) => {
const data = await request.json();
console.log(data)
const filePath = path.join(
process.cwd(),
"static",
"images",
data.filename as string
);
const file = data.image;
console.log(data.headers)
if(data.bearer === BEARER_TOKEN){
console.log("PASSWORD CORRECT")
writeFileSync(filePath, file, 'base64');
}
else{
console.log("PASSWORD INCORRECT")
throw error(403, "Password incorrect")
}
}) satisfies RequestHandler;

View File

@ -0,0 +1,20 @@
import path from 'path'
import fs from 'fs/promises'
import { fail, redirect } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const POST = (async ({ request, url}) => {
try {
const data = Object.fromEntries(await request.formData())
const filePath = path.join(
process.cwd(),
"static",
"images",
data.filename as string
);
await fs.writeFile(filePath, Buffer.from(await (data.image as Blob).arrayBuffer()))
return new Response(String({status: 200}))
} catch (err) {
throw fail(500, { err: err })
}
}) satisfies RequestHandler;