add forgotten img api
This commit is contained in:
parent
6f4cf0a13c
commit
71fdcf9ffd
27
src/routes/api/img/add/+server.ts
Normal file
27
src/routes/api/img/add/+server.ts
Normal 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;
|
20
src/routes/api/img/add/+server.ts_
Normal file
20
src/routes/api/img/add/+server.ts_
Normal 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;
|
Loading…
Reference in New Issue
Block a user