DmitrMakeev commited on
Commit
02a45e5
·
verified ·
1 Parent(s): c1f1472

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -9
app.py CHANGED
@@ -104,11 +104,16 @@ app.config['DEBUG'] = True
104
 
105
 
106
 
107
- UPLOAD_FOLDER = os.path.join(os.getcwd(), 'uploads') # абсолютный путь
108
- os.makedirs(UPLOAD_FOLDER, exist_ok=True) # создаёт папку, если её нет
109
 
110
 
111
 
 
 
 
 
 
 
 
112
  # Настроим логирование
113
  logging.basicConfig(level=logging.DEBUG)
114
 
@@ -567,22 +572,48 @@ def set_res():
567
 
568
 
569
 
 
 
 
 
570
  @app.route('/upload', methods=['POST'])
571
  def upload_file():
 
572
  if 'file' not in request.files:
573
  return jsonify({"error": "No file part"}), 400
574
 
575
  file = request.files['file']
 
 
576
  if file.filename == '':
577
  return jsonify({"error": "No selected file"}), 400
578
-
579
- filename = str(uuid.uuid4()) + ".jpg"
580
- file.save(os.path.join(UPLOAD_FOLDER, filename))
581
 
582
- return jsonify({
583
- "message": "File uploaded successfully",
584
- "filename": filename
585
- }), 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
 
587
  @app.route('/uploads/<filename>', methods=['GET'])
588
  def uploaded_file(filename):
 
104
 
105
 
106
 
 
 
107
 
108
 
109
 
110
+ UPLOAD_FOLDER = 'uploads'
111
+ ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif'}
112
+
113
+ # Создаем папку для загрузок если ее нет
114
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
115
+
116
+
117
  # Настроим логирование
118
  logging.basicConfig(level=logging.DEBUG)
119
 
 
572
 
573
 
574
 
575
+ def allowed_file(filename):
576
+ return '.' in filename and \
577
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
578
+
579
  @app.route('/upload', methods=['POST'])
580
  def upload_file():
581
+ # Проверяем наличие файла в запросе
582
  if 'file' not in request.files:
583
  return jsonify({"error": "No file part"}), 400
584
 
585
  file = request.files['file']
586
+
587
+ # Если пользователь не выбрал файл
588
  if file.filename == '':
589
  return jsonify({"error": "No selected file"}), 400
 
 
 
590
 
591
+ # Проверяем что файл допустимого типа
592
+ if not allowed_file(file.filename):
593
+ return jsonify({"error": "Invalid file type"}), 400
594
+
595
+ # Создаем уникальное имя файла с timestamp
596
+ timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
597
+ filename = timestamp + secure_filename(file.filename)
598
+ save_path = os.path.join(UPLOAD_FOLDER, filename)
599
+
600
+ try:
601
+ # Сохраняем файл
602
+ file.save(save_path)
603
+
604
+ # Проверяем что это изображение
605
+ if not os.path.getsize(save_path) > 0:
606
+ os.remove(save_path)
607
+ return jsonify({"error": "Empty file"}), 400
608
+
609
+ return jsonify({
610
+ "message": "File uploaded successfully",
611
+ "filename": filename,
612
+ "path": save_path
613
+ }), 200
614
+
615
+ except Exception as e:
616
+ return jsonify({"error": str(e)}), 500
617
 
618
  @app.route('/uploads/<filename>', methods=['GET'])
619
  def uploaded_file(filename):