Skip to content

Commit af88437

Browse files
committed
support delete request for images
1 parent 111d066 commit af88437

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

client/src/ImageUpload/index.jsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,28 @@ const ImageUpload = ({ onImageUpload }) => {
5757
}
5858
};
5959

60+
const deleteImage = async (filename) => {
61+
try {
62+
const response = await axios.delete(`${import.meta.env.VITE_SERVER_URL}/uploads/${filename}`);
63+
showSnackbar(response.data.message, 'success');
64+
65+
// Update the state to remove the deleted image
66+
const updatedImages = images.filter((image) => image.filename !== filename);
67+
setImages(updatedImages);
68+
onImageUpload(updatedImages);
69+
} catch (error) {
70+
if (error?.response?.data) {
71+
showSnackbar(error.response.data.message, 'error');
72+
} else {
73+
showSnackbar("Couldn't connect to the server", 'error');
74+
}
75+
console.error('Error deleting image:', error);
76+
}
77+
};
78+
6079
const handleRemoveImage = (index) => {
61-
setImages((prevImages) => prevImages.filter((_, i) => i !== index));
62-
onImageUpload(images.filter((_, i) => i !== index));
80+
const imageToRemove = images[index];
81+
deleteImage(imageToRemove.filename);
6382
};
6483

6584
const { getRootProps, getInputProps, isDragActive } = useDropzone({

server/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ def upload_file():
101101
def uploaded_file(filename):
102102
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
103103

104+
@app.route('/uploads/<filename>', methods=['DELETE'])
105+
def delete_file(filename):
106+
try:
107+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
108+
if os.path.exists(file_path):
109+
os.remove(file_path)
110+
return jsonify({"status": "success", "message": "File deleted successfully"}), 200
111+
else:
112+
return jsonify({"status": "error", "message": "File not found"}), 404
104113

114+
except Exception as e:
115+
return jsonify({"status": "error", "message": str(e)}), 500
105116

106117
@app.route('/activeImage', methods=['POST'])
107118
@cross_origin(origin=client_url, headers=['Content-Type'])

0 commit comments

Comments
 (0)