add initial img API endpoints

This commit is contained in:
2023-07-12 11:35:43 +02:00
parent c6b82865d4
commit 385af0401b
5 changed files with 137 additions and 5 deletions

View File

@ -1,4 +1,3 @@
import { writeFileSync } from 'fs';
import path from 'path'
import type { RequestHandler } from '@sveltejs/kit';
import { BEARER_TOKEN } from '$env/static/private'

View File

@ -0,0 +1,24 @@
import path from 'path'
import type { RequestHandler } from '@sveltejs/kit';
import { BEARER_TOKEN } from '$env/static/private'
import { IMAGE_DIR } from '$env/static/private'
import { unlink } from 'node:fs';
import { error } from '@sveltejs/kit';
export const POST = (async ({ request }) => {
const data = await request.json();
if(data.bearer === BEARER_TOKEN){
[ "full", "thumb", "placeholder"].forEach((folder) => {
unlink(path.join(IMAGE_DIR, folder, data.name + ".webp"), (e) => {
if(e) throw error(500, "could not delete: " + folder + "/" + data.name + ".webp")
})
})
return new Response(JSON.stringify({msg: "Deleted image successfully"}),{
status: 200,
});
}
else{
throw error(403, "Password incorrect")
}
}) satisfies RequestHandler;

View File

@ -0,0 +1,24 @@
import path from 'path'
import type { RequestHandler } from '@sveltejs/kit';
import { BEARER_TOKEN } from '$env/static/private'
import { IMAGE_DIR } from '$env/static/private'
import { rename } from 'node:fs';
import { error } from '@sveltejs/kit';
export const POST = (async ({ request }) => {
const data = await request.json();
if(data.bearer === BEARER_TOKEN){
[ "full", "thumb", "placeholder"].forEach((folder) => {
rename(path.join(IMAGE_DIR, folder, data.old_name + ".webp"), path.join(IMAGE_DIR, folder, data.new_name + ".webp"), (e) => {
if(e) throw error(500, "could not mv: " + folder + "/" + data.old_name + ".webp")
})
});
return new Response(JSON.stringify({msg: "Deleted image successfully"}),{
status: 200,
});
}
else{
throw error(403, "Password incorrect")
}
}) satisfies RequestHandler;