mirror of
https://github.com/qwertyforce/scenery.git
synced 2025-05-02 13:39:40 +00:00
delete_image
This commit is contained in:
parent
2c160746a6
commit
6e9208a850
68
pages/delete_image.tsx
Normal file
68
pages/delete_image.tsx
Normal file
@ -0,0 +1,68 @@
|
||||
import AppBar from '../components/AppBar'
|
||||
import db_ops from '../server/helpers/db_ops'
|
||||
import Button from '@material-ui/core/Button';
|
||||
import axios from 'axios'
|
||||
import TextField from '@material-ui/core/TextField';
|
||||
|
||||
import ErrorPage from 'next/error'
|
||||
import { useState } from 'react';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default function deleteImage(props: any) {
|
||||
if (props.err) {
|
||||
return <ErrorPage statusCode={404} />
|
||||
}
|
||||
const [ImageID, setID] = useState(0);
|
||||
const handleKeyPress = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
if (e.keyCode === 13 || e.which === 13) {
|
||||
delete_image();
|
||||
}
|
||||
};
|
||||
const delete_image = () => {
|
||||
axios(`/delete_image`, {
|
||||
method: "post",
|
||||
data: { id: ImageID },
|
||||
withCredentials: true
|
||||
}).then((resp) => {
|
||||
alert(JSON.stringify(resp.data))
|
||||
setID(0)
|
||||
}).catch((err) => {
|
||||
alert('check console for error message')
|
||||
console.log(err)
|
||||
setID(0)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<AppBar />
|
||||
<TextField
|
||||
value ={ImageID}
|
||||
fullWidth
|
||||
type="number"
|
||||
label=" image id"
|
||||
placeholder="image id"
|
||||
margin="normal"
|
||||
onChange={(e) => setID(parseInt(e.target.value)||0)}
|
||||
onKeyPress={(e) => handleKeyPress(e)}
|
||||
/>
|
||||
<Button onClick={() => { delete_image() }} variant="contained" color="primary" >Delete image</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export async function getServerSideProps(context: any) {
|
||||
if (context.req.session.authed && context.req.session.user_id) {
|
||||
const user = await db_ops.activated_user.find_user_by_id(context.req.session.user_id)
|
||||
if (user[0].isAdmin) {
|
||||
return {
|
||||
props: {},
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
props: { err: true },
|
||||
}
|
||||
}
|
@ -129,36 +129,23 @@ async function get_all_color_hists(){
|
||||
return color_hists
|
||||
}
|
||||
|
||||
async function delete_color_hist_by_id(id:number){
|
||||
removeDocument("color_hist",{id:id})
|
||||
}
|
||||
|
||||
async function add_color_hist_by_id(id:number, color_hist:number[][]){
|
||||
insertDocuments("color_hist", [{
|
||||
id:id,
|
||||
color_hist:color_hist
|
||||
}])
|
||||
}
|
||||
// async function get_all_phash_distances(){
|
||||
// const phash_distances = findDocuments("img_search", {})
|
||||
// return phash_distances
|
||||
// }
|
||||
// async function get_phash_distances_by_image_id(id:number){
|
||||
// const phash_distances = findDocuments("img_search", {id:id})
|
||||
// return phash_distances
|
||||
// }
|
||||
|
||||
// async function add_image_to_image_search(id:number, phash_dist:Array<Record<string,unknown>>){
|
||||
// insertDocuments("img_search", [{
|
||||
// id:id,
|
||||
// phash_dist:phash_dist
|
||||
// }])
|
||||
// }
|
||||
// async function update_phash_dist_by_id(id:number, phash_dist:Array<Record<string,unknown>>){
|
||||
// updateDocument("images", {id: id},{phash_dist:phash_dist})
|
||||
// }
|
||||
|
||||
////////////////////////////////////////////////
|
||||
|
||||
|
||||
/////////////////////////////////////////////////IMAGES OPS
|
||||
|
||||
|
||||
async function add_tags_to_image_by_id(id:number,tags:string[]){
|
||||
await addToArrayInDocument("images",{id:id},{tags:{ $each:tags}})
|
||||
}
|
||||
@ -215,6 +202,9 @@ async function get_max_image_id(){
|
||||
const result = await collection.find({}).sort({id:-1}).limit(1).toArray()
|
||||
return result[0]?.id
|
||||
}
|
||||
async function delete_image_by_id(id:number){
|
||||
removeDocument("images",{id:id})
|
||||
}
|
||||
|
||||
async function add_image(id:number,file_ext:string,width:number,height:number,author:string,
|
||||
size:string,derpi_link:string,
|
||||
@ -364,6 +354,7 @@ export default {
|
||||
get_all_images,
|
||||
find_image_by_id,
|
||||
get_max_image_id,
|
||||
delete_image_by_id,
|
||||
find_images_by_tags,
|
||||
get_ids_and_phashes,
|
||||
find_image_by_phash,
|
||||
@ -376,15 +367,12 @@ export default {
|
||||
get_all_color_hists,
|
||||
get_color_hist_by_id,
|
||||
add_color_hist_by_id,
|
||||
// get_all_phash_distances,
|
||||
// update_phash_dist_by_id,
|
||||
// add_image_to_image_search,
|
||||
delete_color_hist_by_id,
|
||||
get_color_similarities_by_id,
|
||||
add_color_similarities_by_id,
|
||||
delete_id_from_color_similarities,
|
||||
add_color_similarity_to_other_image,
|
||||
get_image_ids_from_color_similarities,
|
||||
// get_phash_distances_by_image_id,
|
||||
},
|
||||
password_recovery:{
|
||||
update_user_password_by_id,
|
||||
|
@ -33,6 +33,7 @@ import change_password from './routes/change_password';
|
||||
import forgot_password from './routes/forgot_password';
|
||||
import activate_account_email from './routes/activate_account_email';
|
||||
import update_image_data from './routes/update_image_data'
|
||||
import delete_image from './routes/delete_image'
|
||||
import import_from_derpi from './routes/import_from_derpi'
|
||||
import reverse_search from './routes/reverse_search'
|
||||
next_app.prepare().then(() => {
|
||||
@ -91,7 +92,9 @@ next_app.prepare().then(() => {
|
||||
api_router.get('/auth/google/callback', google_oauth_callback)
|
||||
|
||||
api_router.post('/reverse_search', [upload.single('image'),recaptcha.middleware.verify], reverse_search)
|
||||
|
||||
api_router.post('/update_image_data', update_image_data)
|
||||
api_router.post('/delete_image', delete_image)
|
||||
api_router.post('/import_from_derpi', import_from_derpi)
|
||||
|
||||
api_router.post('/signup', [
|
||||
|
30
server/routes/delete_image.ts
Normal file
30
server/routes/delete_image.ts
Normal file
@ -0,0 +1,30 @@
|
||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||
import db_ops from './../helpers/db_ops'
|
||||
import {Request, Response} from 'express';
|
||||
import config from '../../config/config'
|
||||
import fs from 'fs'
|
||||
async function delete_image(req:Request,res:Response) {
|
||||
const id = parseInt(req.body.id);
|
||||
if(!isNaN(id) && req.session?.user_id){
|
||||
const user = await db_ops.activated_user.find_user_by_id(req.session?.user_id)
|
||||
if(user[0].isAdmin){
|
||||
const image=(await db_ops.image_ops.find_image_by_id(id))[0]
|
||||
db_ops.image_ops.delete_image_by_id(id)
|
||||
db_ops.image_search.delete_color_hist_by_id(id)
|
||||
await db_ops.image_search.delete_id_from_color_similarities(id)
|
||||
fs.unlink(`${config.root_path}/public/images/${id}.${image.file_ext}`,function(err){
|
||||
if(err) return console.log(err);
|
||||
console.log('file deleted successfully');
|
||||
});
|
||||
fs.unlink(`${config.root_path}/public/webp_images/${id}.webp`,function(err){
|
||||
if(err) return console.log(err);
|
||||
console.log('webp file deleted successfully');
|
||||
});
|
||||
res.json({message:"OK"})
|
||||
return
|
||||
}
|
||||
}
|
||||
res.sendStatus(404);
|
||||
}
|
||||
|
||||
export default delete_image;
|
Loading…
x
Reference in New Issue
Block a user