Update app.py
Browse files
app.py
CHANGED
@@ -115,6 +115,9 @@ ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png', 'gif'}
|
|
115 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
116 |
|
117 |
|
|
|
|
|
|
|
118 |
# Настроим логирование
|
119 |
logging.basicConfig(level=logging.DEBUG)
|
120 |
|
@@ -614,7 +617,46 @@ def uploaded_file(filename):
|
|
614 |
|
615 |
|
616 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
617 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
618 |
|
619 |
|
620 |
|
|
|
115 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
116 |
|
117 |
|
118 |
+
# Глобальная переменная для хранения последнего изображения в памяти
|
119 |
+
latest_image = {"data": None, "filename": None}
|
120 |
+
|
121 |
# Настроим логирование
|
122 |
logging.basicConfig(level=logging.DEBUG)
|
123 |
|
|
|
617 |
|
618 |
|
619 |
|
620 |
+
# 🧠 2. Маршрут: сохраняет файл только в память (BytesIO)
|
621 |
+
@app.route('/upload_memory', methods=['POST'])
|
622 |
+
def upload_file_to_memory():
|
623 |
+
if 'file' not in request.files:
|
624 |
+
return jsonify({"error": "No file part"}), 400
|
625 |
+
|
626 |
+
file = request.files['file']
|
627 |
+
if file.filename == '':
|
628 |
+
return jsonify({"error": "No selected file"}), 400
|
629 |
+
|
630 |
+
if not allowed_file(file.filename):
|
631 |
+
return jsonify({"error": "Invalid file type"}), 400
|
632 |
+
|
633 |
+
timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
|
634 |
+
filename = timestamp + file.filename
|
635 |
+
|
636 |
+
memory_buffer = BytesIO()
|
637 |
+
try:
|
638 |
+
while chunk := file.read(1024):
|
639 |
+
memory_buffer.write(chunk)
|
640 |
+
|
641 |
+
memory_buffer.seek(0)
|
642 |
+
latest_image["data"] = memory_buffer
|
643 |
+
latest_image["filename"] = filename
|
644 |
+
|
645 |
+
return jsonify({
|
646 |
+
"message": "Image uploaded to memory",
|
647 |
+
"filename": filename
|
648 |
+
}), 200
|
649 |
+
|
650 |
+
except Exception as e:
|
651 |
+
return jsonify({"error": str(e)}), 500
|
652 |
|
653 |
+
# 📤 Маршрут для получения последнего изображения из памяти
|
654 |
+
@app.route('/last_image', methods=['GET'])
|
655 |
+
def get_last_image():
|
656 |
+
if latest_image["data"] is None:
|
657 |
+
return jsonify({"error": "No image available"}), 404
|
658 |
+
latest_image["data"].seek(0)
|
659 |
+
return send_file(latest_image["data"], mimetype='image/jpeg', download_name=latest_image["filename"])
|
660 |
|
661 |
|
662 |
|