DmitrMakeev commited on
Commit
d8b02a6
·
verified ·
1 Parent(s): 7c48504

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -624,6 +624,7 @@ def uploaded_file(filename):
624
 
625
  # 🧠 2. Маршрут: сохраняет файл только в память (BytesIO)
626
  # Маршрут для загрузки файла в память
 
627
  @app.route('/upload_memory', methods=['POST'])
628
  def upload_file_to_memory():
629
  if 'file' not in request.files:
@@ -633,22 +634,18 @@ def upload_file_to_memory():
633
  if file.filename == '':
634
  return jsonify({"error": "No selected file"}), 400
635
 
636
- # Проверка типа файла (разрешаем только изображения)
637
  if not file.filename.lower().endswith(('.jpg', '.jpeg', '.png')):
638
  return jsonify({"error": "Invalid file type"}), 400
639
 
640
- # Создаем имя файла с timestamp
641
  timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
642
  filename = timestamp + file.filename
643
 
644
- # Сохраняем в память
645
- memory_buffer = BytesIO()
646
  try:
647
- file.save(memory_buffer)
648
- memory_buffer.seek(0)
649
-
650
- # Обновляем последнее изображение
651
- latest_image["data"] = memory_buffer
652
  latest_image["filename"] = filename
653
 
654
  return jsonify({
@@ -659,21 +656,19 @@ def upload_file_to_memory():
659
  except Exception as e:
660
  return jsonify({"error": str(e)}), 500
661
 
662
- # Маршрут для получения последнего изображения
663
  @app.route('/last_image', methods=['GET'])
664
  def get_last_image():
665
- if latest_image["data"] is None:
666
  return jsonify({"error": "No image available"}), 404
667
 
668
- latest_image["data"].seek(0)
669
  return send_file(
670
- latest_image["data"],
671
  mimetype='image/jpeg',
672
  download_name=latest_image["filename"]
673
  )
674
 
675
-
676
-
677
  @app.route('/view_image', methods=['GET'])
678
  def view_image():
679
  return render_template('show_image.html')
 
624
 
625
  # 🧠 2. Маршрут: сохраняет файл только в память (BytesIO)
626
  # Маршрут для загрузки файла в память
627
+ # Загрузка изображения в память (в виде байтов)
628
  @app.route('/upload_memory', methods=['POST'])
629
  def upload_file_to_memory():
630
  if 'file' not in request.files:
 
634
  if file.filename == '':
635
  return jsonify({"error": "No selected file"}), 400
636
 
 
637
  if not file.filename.lower().endswith(('.jpg', '.jpeg', '.png')):
638
  return jsonify({"error": "Invalid file type"}), 400
639
 
 
640
  timestamp = datetime.now().strftime('%Y.%m.%d_%H:%M:%S_')
641
  filename = timestamp + file.filename
642
 
 
 
643
  try:
644
+ # Читаем весь файл в байты
645
+ file_bytes = file.read()
646
+
647
+ # Сохраняем в переменную
648
+ latest_image["data"] = file_bytes
649
  latest_image["filename"] = filename
650
 
651
  return jsonify({
 
656
  except Exception as e:
657
  return jsonify({"error": str(e)}), 500
658
 
659
+ # Получение последнего изображения
660
  @app.route('/last_image', methods=['GET'])
661
  def get_last_image():
662
+ if not latest_image["data"]:
663
  return jsonify({"error": "No image available"}), 404
664
 
665
+ # Возвращаем через новый BytesIO каждый раз
666
  return send_file(
667
+ BytesIO(latest_image["data"]),
668
  mimetype='image/jpeg',
669
  download_name=latest_image["filename"]
670
  )
671
 
 
 
672
  @app.route('/view_image', methods=['GET'])
673
  def view_image():
674
  return render_template('show_image.html')