is-a / app.py
Kastg
Update app.py
53f7a0c verified
raw
history blame
2.67 kB
from flask import Flask, request, jsonify, send_from_directory
import shutil
import os
app = Flask(__name__)
IMAGE_FOLDER = "images"
app.config["UPLOAD_FOLDER"] = IMAGE_FOLDER
@app.route("/images/<filename>")
def get_image(filename):
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
# Create folders to store uploaded images
UPLOAD_FOLDER = "uploads"
IMAGE_FOLDER = os.path.join(UPLOAD_FOLDER, "images")
VIDEO_FOLDER = os.path.join(UPLOAD_FOLDER, "videos")
for folder in [UPLOAD_FOLDER, IMAGE_FOLDER, VIDEO_FOLDER]:
os.makedirs(folder, exist_ok=True)
# Create a dictionary to store available images
available_images = {}
@app.route("/upload", methods=["POST"])
def upload_image():
url = request.form.get("url")
file = request.files["file"]
allowed_formats = [".png", ".jpg"]
file_format = os.path.splitext(file.filename)[1]
if file_format not in allowed_formats:
return jsonify({"message": "Unsupported file format"}), 400
# Save the uploaded file
file.save(os.path.join(UPLOAD_FOLDER, file.filename))
# Determine the folder based on the file format
folder = "images" if file_format in [".png", ".jpg"] else "videos"
# Move the file to the appropriate folder
shutil.move(
os.path.join(UPLOAD_FOLDER, file.filename),
os.path.join(UPLOAD_FOLDER, folder, file.filename)
)
# Update available images dictionary
available_images[file.filename] = {"url": url, "name": file.filename}
return jsonify({"message": "File uploaded successfully"})
@app.route("/get-all-images", methods=["GET"])
def get_all_images():
password = request.args.get("password")
from_folder = request.args.get("from_folder")
if password != "Kastg@123":
return jsonify({"message": "Unauthorized"}), 401
folder_path = os.path.join(UPLOAD_FOLDER, from_folder)
if not os.path.exists(folder_path):
return jsonify({"message": "Folder not found"}), 404
images = os.listdir(folder_path)
return jsonify({"images": images})
@app.route("/delete-image", methods=["DELETE"])
def delete_image():
image_name = request.args.get("image_name")
password = request.args.get("password")
if password != "Kastg@123":
return jsonify({"message": "Unauthorized"}), 401
image_path = os.path.join(UPLOAD_FOLDER, image_name)
if not os.path.exists(image_path):
return jsonify({"message": "Image not found"}), 404
os.remove(image_path)
available_images.pop(image_name, None)
return jsonify({"message": "Image deleted successfully"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)