fix img APIs to working standard
This commit is contained in:
parent
385af0401b
commit
2ae789e6a6
@ -7,7 +7,6 @@ import { error } from '@sveltejs/kit';
|
||||
// header: use for bearer token for now
|
||||
// recipe json in body
|
||||
export const POST: RequestHandler = async ({request}) => {
|
||||
console.log("AT EDIT API")
|
||||
let message = await request.json()
|
||||
const recipe_json = message.recipe
|
||||
const bearer_token = message.headers.bearer
|
||||
|
@ -21,18 +21,21 @@ export const POST = (async ({ request }) => {
|
||||
await sharp(full_res)
|
||||
.toFormat('webp')
|
||||
.toFile(path.join(IMAGE_DIR,
|
||||
"rezepte",
|
||||
"full",
|
||||
data.name + ".webp"))
|
||||
await sharp(full_res)
|
||||
.resize({ width: 800})
|
||||
.toFormat('webp')
|
||||
.toFile(path.join(IMAGE_DIR,
|
||||
"rezepte",
|
||||
"thumb",
|
||||
data.name + ".webp"))
|
||||
await sharp(full_res)
|
||||
.resize({ width: 20})
|
||||
.toFormat('webp')
|
||||
.toFile(path.join(IMAGE_DIR,
|
||||
"rezepte",
|
||||
"placeholder",
|
||||
data.name + ".webp"))
|
||||
return new Response(JSON.stringify({msg: "Added image successfully"}),{
|
||||
|
@ -9,7 +9,7 @@ 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) => {
|
||||
unlink(path.join(IMAGE_DIR, "rezepte", folder, data.name + ".webp"), (e) => {
|
||||
if(e) throw error(500, "could not delete: " + folder + "/" + data.name + ".webp")
|
||||
})
|
||||
})
|
||||
|
@ -9,8 +9,10 @@ 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")
|
||||
const old_path = path.join(IMAGE_DIR, "rezepte", 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"}),{
|
||||
|
@ -99,17 +99,18 @@
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
name: old_short_name,
|
||||
bearer: password,}),
|
||||
headers : {
|
||||
'content-type': 'application/json',
|
||||
bearer: password
|
||||
}
|
||||
})
|
||||
})
|
||||
if(!res_img.ok){
|
||||
const item = await res_img.json();
|
||||
alert(item.message)
|
||||
return
|
||||
}
|
||||
return
|
||||
const res = await fetch('/api/delete', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
@ -142,16 +143,16 @@
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
name: old_short_name,
|
||||
bearer: password,
|
||||
}),
|
||||
headers : {
|
||||
'content-type': 'application/json',
|
||||
bearer: password
|
||||
}
|
||||
})
|
||||
})
|
||||
if(!res_img.ok){
|
||||
const item = await res_img.json();
|
||||
alert(item.message)
|
||||
return
|
||||
}
|
||||
}
|
||||
async function upload_img(){
|
||||
@ -172,7 +173,6 @@
|
||||
if(!res.ok){
|
||||
const item = await res_img.json();
|
||||
alert(item.message)
|
||||
return
|
||||
}
|
||||
}
|
||||
delete_img()
|
||||
@ -180,11 +180,19 @@
|
||||
}
|
||||
// case new short_name:
|
||||
else if(short_name != old_short_name){
|
||||
console.log("MOVING")
|
||||
console.log("PASSWORD:", password)
|
||||
const res_img = await fetch('/api/img/mv', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
bearer: password,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
old_name: old_short_name,
|
||||
new_name: short_name
|
||||
new_name: short_name,
|
||||
bearer: password,
|
||||
})
|
||||
})
|
||||
if(!res_img.ok){
|
||||
@ -219,7 +227,8 @@
|
||||
})
|
||||
if(res.ok){
|
||||
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('/'))
|
||||
}
|
||||
else{
|
||||
|
@ -1 +0,0 @@
|
||||
{"terminal": "nvimterm"}
|
@ -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}/>
|
Loading…
Reference in New Issue
Block a user