Kastg commited on
Commit
0006475
·
verified ·
1 Parent(s): 53f7a0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -55
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from flask import Flask, request, jsonify, send_from_directory
2
  import shutil
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
@@ -14,9 +15,8 @@ def get_image(filename):
14
  # Create folders to store uploaded images
15
  UPLOAD_FOLDER = "uploads"
16
  IMAGE_FOLDER = os.path.join(UPLOAD_FOLDER, "images")
17
- VIDEO_FOLDER = os.path.join(UPLOAD_FOLDER, "videos")
18
 
19
- for folder in [UPLOAD_FOLDER, IMAGE_FOLDER, VIDEO_FOLDER]:
20
  os.makedirs(folder, exist_ok=True)
21
 
22
  # Create a dictionary to store available images
@@ -25,63 +25,40 @@ available_images = {}
25
  @app.route("/upload", methods=["POST"])
26
  def upload_image():
27
  url = request.form.get("url")
28
- file = request.files["file"]
29
 
30
- allowed_formats = [".png", ".jpg"]
31
- file_format = os.path.splitext(file.filename)[1]
32
-
33
- if file_format not in allowed_formats:
34
- return jsonify({"message": "Unsupported file format"}), 400
35
-
36
- # Save the uploaded file
37
- file.save(os.path.join(UPLOAD_FOLDER, file.filename))
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
- shutil.move(
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] = {"url": url, "name": file.filename}
50
-
51
- return jsonify({"message": "File uploaded successfully"})
52
-
53
-
54
- @app.route("/get-all-images", methods=["GET"])
55
- def get_all_images():
56
- password = request.args.get("password")
57
- from_folder = request.args.get("from_folder")
58
-
59
- if password != "Kastg@123":
60
- return jsonify({"message": "Unauthorized"}), 401
61
-
62
- folder_path = os.path.join(UPLOAD_FOLDER, from_folder)
63
- if not os.path.exists(folder_path):
64
- return jsonify({"message": "Folder not found"}), 404
65
-
66
- images = os.listdir(folder_path)
67
- return jsonify({"images": images})
68
-
69
-
70
- @app.route("/delete-image", methods=["DELETE"])
71
- def delete_image():
72
- image_name = request.args.get("image_name")
73
- password = request.args.get("password")
74
-
75
- if password != "Kastg@123":
76
- return jsonify({"message": "Unauthorized"}), 401
77
-
78
- image_path = os.path.join(UPLOAD_FOLDER, image_name)
79
- if not os.path.exists(image_path):
80
- return jsonify({"message": "Image not found"}), 404
81
 
82
- os.remove(image_path)
83
- available_images.pop(image_name, None)
84
- return jsonify({"message": "Image deleted successfully"})
85
 
86
 
87
  if __name__ == "__main__":
 
1
  from flask import Flask, request, jsonify, send_from_directory
2
  import shutil
3
  import os
4
+ import requests
5
 
6
  app = Flask(__name__)
7
 
 
15
  # Create folders to store uploaded images
16
  UPLOAD_FOLDER = "uploads"
17
  IMAGE_FOLDER = os.path.join(UPLOAD_FOLDER, "images")
 
18
 
19
+ for folder in [UPLOAD_FOLDER, IMAGE_FOLDER]:
20
  os.makedirs(folder, exist_ok=True)
21
 
22
  # Create a dictionary to store available images
 
25
  @app.route("/upload", methods=["POST"])
26
  def upload_image():
27
  url = request.form.get("url")
 
28
 
29
+ if not url:
30
+ return jsonify({"message": "URL parameter is missing"}), 400
31
+
32
+ # Check if the URL is valid and get the image content
33
+ try:
34
+ response = requests.get(url)
35
+ response.raise_for_status()
36
+ except requests.exceptions.RequestException as e:
37
+ return jsonify({"message": f"Error fetching image from URL: {str(e)}"}), 400
38
+
39
+ # Check if the response content is an image
40
+ content_type = response.headers.get('content-type')
41
+ if 'image' not in content_type:
42
+ return jsonify({"message": "The provided URL does not lead to an image"}), 400
43
+
44
+ # Determine the file format
45
+ if 'jpeg' in content_type:
46
+ file_extension = 'jpg'
47
+ elif 'png' in content_type:
48
+ file_extension = 'png'
49
+ else:
50
+ return jsonify({"message": "Unsupported image format"}), 400
51
+
52
+ # Save the image
53
+ filename = f"uploaded_image.{file_extension}"
54
+ image_path = os.path.join(IMAGE_FOLDER, filename)
55
+ with open(image_path, 'wb') as f:
56
+ f.write(response.content)
57
 
58
  # Update available images dictionary
59
+ available_images[filename] = {"url": url, "name": filename}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ return jsonify({"message": "File uploaded successfully", "image_url": f"/images/{filename}"})
 
 
62
 
63
 
64
  if __name__ == "__main__":