fix img APIs to working standard

This commit is contained in:
Alexander Bocken 2023-07-12 12:23:35 +02:00
parent 385af0401b
commit 2ae789e6a6
7 changed files with 23 additions and 57 deletions

View File

@ -7,7 +7,6 @@ import { error } from '@sveltejs/kit';
// header: use for bearer token for now // header: use for bearer token for now
// recipe json in body // recipe json in body
export const POST: RequestHandler = async ({request}) => { export const POST: RequestHandler = async ({request}) => {
console.log("AT EDIT API")
let message = await request.json() let message = await request.json()
const recipe_json = message.recipe const recipe_json = message.recipe
const bearer_token = message.headers.bearer const bearer_token = message.headers.bearer

View File

@ -21,18 +21,21 @@ export const POST = (async ({ request }) => {
await sharp(full_res) await sharp(full_res)
.toFormat('webp') .toFormat('webp')
.toFile(path.join(IMAGE_DIR, .toFile(path.join(IMAGE_DIR,
"rezepte",
"full", "full",
data.name + ".webp")) data.name + ".webp"))
await sharp(full_res) await sharp(full_res)
.resize({ width: 800}) .resize({ width: 800})
.toFormat('webp') .toFormat('webp')
.toFile(path.join(IMAGE_DIR, .toFile(path.join(IMAGE_DIR,
"rezepte",
"thumb", "thumb",
data.name + ".webp")) data.name + ".webp"))
await sharp(full_res) await sharp(full_res)
.resize({ width: 20}) .resize({ width: 20})
.toFormat('webp') .toFormat('webp')
.toFile(path.join(IMAGE_DIR, .toFile(path.join(IMAGE_DIR,
"rezepte",
"placeholder", "placeholder",
data.name + ".webp")) data.name + ".webp"))
return new Response(JSON.stringify({msg: "Added image successfully"}),{ return new Response(JSON.stringify({msg: "Added image successfully"}),{

View File

@ -9,7 +9,7 @@ export const POST = (async ({ request }) => {
const data = await request.json(); const data = await request.json();
if(data.bearer === BEARER_TOKEN){ if(data.bearer === BEARER_TOKEN){
[ "full", "thumb", "placeholder"].forEach((folder) => { [ "full", "thumb", "placeholder"].forEach((folder) => {
unlink(path.join(IMAGE_DIR, folder, data.name + ".webp"), (e) => { unlink(path.join(IMAGE_DIR, "rezepte", folder, data.name + ".webp"), (e) => {
if(e) throw error(500, "could not delete: " + folder + "/" + data.name + ".webp") if(e) throw error(500, "could not delete: " + folder + "/" + data.name + ".webp")
}) })
}) })

View File

@ -9,8 +9,10 @@ export const POST = (async ({ request }) => {
const data = await request.json(); const data = await request.json();
if(data.bearer === BEARER_TOKEN){ if(data.bearer === BEARER_TOKEN){
[ "full", "thumb", "placeholder"].forEach((folder) => { [ "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) => { const old_path = path.join(IMAGE_DIR, "rezepte", folder, data.old_name + ".webp")
if(e) throw error(500, "could not mv: " + folder + "/" + data.old_name + ".webp") rename(old_path, path.join(IMAGE_DIR, "rezepte", folder, data.new_name + ".webp"), (e) => {
console.log(e)
if(e) throw error(500, "could not mv: " + old_path)
}) })
}); });
return new Response(JSON.stringify({msg: "Deleted image successfully"}),{ return new Response(JSON.stringify({msg: "Deleted image successfully"}),{

View File

@ -99,17 +99,18 @@
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
name: old_short_name, name: old_short_name,
bearer: password,}),
headers : { headers : {
'content-type': 'application/json', 'content-type': 'application/json',
bearer: password bearer: password
} }
}) })
})
if(!res_img.ok){ if(!res_img.ok){
const item = await res_img.json(); const item = await res_img.json();
alert(item.message) alert(item.message)
return return
} }
return
const res = await fetch('/api/delete', { const res = await fetch('/api/delete', {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
@ -142,16 +143,16 @@
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
name: old_short_name, name: old_short_name,
bearer: password,
}),
headers : { headers : {
'content-type': 'application/json', 'content-type': 'application/json',
bearer: password bearer: password
} }
}) })
})
if(!res_img.ok){ if(!res_img.ok){
const item = await res_img.json(); const item = await res_img.json();
alert(item.message) alert(item.message)
return
} }
} }
async function upload_img(){ async function upload_img(){
@ -172,7 +173,6 @@
if(!res.ok){ if(!res.ok){
const item = await res_img.json(); const item = await res_img.json();
alert(item.message) alert(item.message)
return
} }
} }
delete_img() delete_img()
@ -180,11 +180,19 @@
} }
// case new short_name: // case new short_name:
else if(short_name != old_short_name){ else if(short_name != old_short_name){
console.log("MOVING")
console.log("PASSWORD:", password)
const res_img = await fetch('/api/img/mv', { const res_img = await fetch('/api/img/mv', {
method: 'POST', method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
bearer: password,
},
body: JSON.stringify({ body: JSON.stringify({
old_name: old_short_name, old_name: old_short_name,
new_name: short_name new_name: short_name,
bearer: password,
}) })
}) })
if(!res_img.ok){ if(!res_img.ok){
@ -219,7 +227,8 @@
}) })
if(res.ok){ if(res.ok){
const url = location.href.split('/'); const url = location.href.split('/');
url.splice(url.length -2, 1); url.splice(url.length -2, 2);
url.push(short_name);
location.assign(url.join('/')) location.assign(url.join('/'))
} }
else{ else{

View File

@ -1 +0,0 @@
{"terminal": "nvimterm"}

View File

@ -1,46 +0,0 @@
<script lang=ts>
let src_full = ''
let src_thumbnail = ''
let src_placeholder = ''
let alt = 'Random Image'
let API = '/api/img/test1'
import { onMount } from 'svelte';
import Card from '$lib/components/Card.svelte';
onMount(async () => {
loadPlaceholder()
loadImage()
});
export async function loadImage() {
const res = await fetch(API)
let image = await res.json()
image = JSON.parse(image.img)
src_placeholder = "data:image/webp;base64, " + image.image
const img_el = document.querySelector("#img")
img_el?.classList.toggle("blur")
src_thumbnail = "data:image/webp;base64, " + image.thumbnail
}
export async function loadPlaceholder() {
const url = '/api/img/placeholder/test1'
const res = await fetch(url)
let image = await res.json()
image = JSON.parse(image.img)
src_placeholder = "data:image/webp;base64, " + image.placeholder
}
</script>
<style>
img{
width: 300px;
height: 300px;
object-fit: cover;
transition: 300ms;
}
.blur{
filter: blur(8px);
}
</style>
<img id=img src={src_placeholder} class=blur {alt}/>