Update app.py
Browse files
app.py
CHANGED
@@ -579,40 +579,32 @@ def allowed_file(filename):
|
|
579 |
|
580 |
@app.route('/upload', methods=['POST'])
|
581 |
def upload_file():
|
582 |
-
# Проверяем наличие файла в запросе
|
583 |
if 'file' not in request.files:
|
584 |
return jsonify({"error": "No file part"}), 400
|
585 |
-
|
586 |
file = request.files['file']
|
587 |
-
|
588 |
-
# Если пользователь не выбрал файл
|
589 |
if file.filename == '':
|
590 |
return jsonify({"error": "No selected file"}), 400
|
591 |
-
|
592 |
-
# Проверяем что файл допустимого типа
|
593 |
if not allowed_file(file.filename):
|
594 |
return jsonify({"error": "Invalid file type"}), 400
|
595 |
-
|
596 |
-
#
|
597 |
timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
|
598 |
-
filename = timestamp +
|
599 |
save_path = os.path.join(UPLOAD_FOLDER, filename)
|
600 |
-
|
|
|
601 |
try:
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
if not os.path.getsize(save_path) > 0:
|
607 |
-
os.remove(save_path)
|
608 |
-
return jsonify({"error": "Empty file"}), 400
|
609 |
-
|
610 |
return jsonify({
|
611 |
"message": "File uploaded successfully",
|
612 |
"filename": filename,
|
613 |
"path": save_path
|
614 |
}), 200
|
615 |
-
|
616 |
except Exception as e:
|
617 |
return jsonify({"error": str(e)}), 500
|
618 |
|
|
|
579 |
|
580 |
@app.route('/upload', methods=['POST'])
|
581 |
def upload_file():
|
|
|
582 |
if 'file' not in request.files:
|
583 |
return jsonify({"error": "No file part"}), 400
|
584 |
+
|
585 |
file = request.files['file']
|
|
|
|
|
586 |
if file.filename == '':
|
587 |
return jsonify({"error": "No selected file"}), 400
|
588 |
+
|
|
|
589 |
if not allowed_file(file.filename):
|
590 |
return jsonify({"error": "Invalid file type"}), 400
|
591 |
+
|
592 |
+
# Генерируем имя для файла с использованием времени
|
593 |
timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
|
594 |
+
filename = timestamp + file.filename
|
595 |
save_path = os.path.join(UPLOAD_FOLDER, filename)
|
596 |
+
|
597 |
+
# Открываем файл для записи и собираем его части
|
598 |
try:
|
599 |
+
with open(save_path, 'wb') as f:
|
600 |
+
while chunk := file.read(1024): # Чтение и запись данных частями
|
601 |
+
f.write(chunk)
|
602 |
+
|
|
|
|
|
|
|
|
|
603 |
return jsonify({
|
604 |
"message": "File uploaded successfully",
|
605 |
"filename": filename,
|
606 |
"path": save_path
|
607 |
}), 200
|
|
|
608 |
except Exception as e:
|
609 |
return jsonify({"error": str(e)}), 500
|
610 |
|