Spaces:
Running
Running
Kastg
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,8 @@
|
|
1 |
-
from
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
-
from pydantic import BaseModel
|
4 |
import shutil
|
5 |
import os
|
6 |
-
import json
|
7 |
|
8 |
-
app =
|
9 |
|
10 |
# Create folders to store uploaded images
|
11 |
UPLOAD_FOLDER = "uploads"
|
@@ -18,64 +15,67 @@ for folder in [UPLOAD_FOLDER, IMAGE_FOLDER, VIDEO_FOLDER]:
|
|
18 |
# Create a dictionary to store available images
|
19 |
available_images = {}
|
20 |
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
class ImageInfo(BaseModel):
|
23 |
-
url: str
|
24 |
-
name: str
|
25 |
-
|
26 |
-
|
27 |
-
@app.post("/upload")
|
28 |
-
async def upload_image(url: str, file: UploadFile = File(...)):
|
29 |
allowed_formats = [".png", ".jpg"]
|
30 |
file_format = os.path.splitext(file.filename)[1]
|
31 |
|
32 |
if file_format not in allowed_formats:
|
33 |
-
|
34 |
|
35 |
# Save the uploaded file
|
36 |
-
|
37 |
-
shutil.copyfileobj(file.file, buffer)
|
38 |
|
39 |
# Determine the folder based on the file format
|
40 |
folder = "images" if file_format in [".png", ".jpg"] else "videos"
|
41 |
|
42 |
# Move the file to the appropriate folder
|
43 |
-
|
44 |
os.path.join(UPLOAD_FOLDER, file.filename),
|
45 |
os.path.join(UPLOAD_FOLDER, folder, file.filename)
|
46 |
)
|
47 |
|
48 |
# Update available images dictionary
|
49 |
-
available_images[file.filename] =
|
|
|
|
|
50 |
|
51 |
-
return JSONResponse(content={"message": "File uploaded successfully"})
|
52 |
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
@app.get("/get-all-images")
|
55 |
-
async def get_all_images(password: str, from_folder: str):
|
56 |
if password != "Kastg@123":
|
57 |
-
|
58 |
|
59 |
folder_path = os.path.join(UPLOAD_FOLDER, from_folder)
|
60 |
if not os.path.exists(folder_path):
|
61 |
-
|
62 |
|
63 |
images = os.listdir(folder_path)
|
64 |
-
return
|
65 |
|
66 |
|
67 |
-
@app.
|
68 |
-
|
|
|
|
|
|
|
69 |
if password != "Kastg@123":
|
70 |
-
|
71 |
|
72 |
image_path = os.path.join(UPLOAD_FOLDER, image_name)
|
73 |
if not os.path.exists(image_path):
|
74 |
-
|
75 |
|
76 |
os.remove(image_path)
|
77 |
available_images.pop(image_name, None)
|
78 |
-
return
|
|
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
|
|
|
|
2 |
import shutil
|
3 |
import os
|
|
|
4 |
|
5 |
+
app = Flask(__name__)
|
6 |
|
7 |
# Create folders to store uploaded images
|
8 |
UPLOAD_FOLDER = "uploads"
|
|
|
15 |
# Create a dictionary to store available images
|
16 |
available_images = {}
|
17 |
|
18 |
+
@app.route("/upload", methods=["POST"])
|
19 |
+
def upload_image():
|
20 |
+
url = request.form.get("url")
|
21 |
+
file = request.files["file"]
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
allowed_formats = [".png", ".jpg"]
|
24 |
file_format = os.path.splitext(file.filename)[1]
|
25 |
|
26 |
if file_format not in allowed_formats:
|
27 |
+
return jsonify({"message": "Unsupported file format"}), 400
|
28 |
|
29 |
# Save the uploaded file
|
30 |
+
file.save(os.path.join(UPLOAD_FOLDER, file.filename))
|
|
|
31 |
|
32 |
# Determine the folder based on the file format
|
33 |
folder = "images" if file_format in [".png", ".jpg"] else "videos"
|
34 |
|
35 |
# Move the file to the appropriate folder
|
36 |
+
shutil.move(
|
37 |
os.path.join(UPLOAD_FOLDER, file.filename),
|
38 |
os.path.join(UPLOAD_FOLDER, folder, file.filename)
|
39 |
)
|
40 |
|
41 |
# Update available images dictionary
|
42 |
+
available_images[file.filename] = {"url": url, "name": file.filename}
|
43 |
+
|
44 |
+
return jsonify({"message": "File uploaded successfully"})
|
45 |
|
|
|
46 |
|
47 |
+
@app.route("/get-all-images", methods=["GET"])
|
48 |
+
def get_all_images():
|
49 |
+
password = request.args.get("password")
|
50 |
+
from_folder = request.args.get("from_folder")
|
51 |
|
|
|
|
|
52 |
if password != "Kastg@123":
|
53 |
+
return jsonify({"message": "Unauthorized"}), 401
|
54 |
|
55 |
folder_path = os.path.join(UPLOAD_FOLDER, from_folder)
|
56 |
if not os.path.exists(folder_path):
|
57 |
+
return jsonify({"message": "Folder not found"}), 404
|
58 |
|
59 |
images = os.listdir(folder_path)
|
60 |
+
return jsonify({"images": images})
|
61 |
|
62 |
|
63 |
+
@app.route("/delete-image", methods=["DELETE"])
|
64 |
+
def delete_image():
|
65 |
+
image_name = request.args.get("image_name")
|
66 |
+
password = request.args.get("password")
|
67 |
+
|
68 |
if password != "Kastg@123":
|
69 |
+
return jsonify({"message": "Unauthorized"}), 401
|
70 |
|
71 |
image_path = os.path.join(UPLOAD_FOLDER, image_name)
|
72 |
if not os.path.exists(image_path):
|
73 |
+
return jsonify({"message": "Image not found"}), 404
|
74 |
|
75 |
os.remove(image_path)
|
76 |
available_images.pop(image_name, None)
|
77 |
+
return jsonify({"message": "Image deleted successfully"})
|
78 |
+
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|