File size: 2,378 Bytes
157a73d
bfd5b9c
 
23db19f
a584bd9
 
 
157a73d
 
 
 
 
 
23db19f
 
 
 
157a73d
5f155f4
6c11ffb
 
 
 
 
045a9fb
6c11ffb
 
 
 
 
157a73d
 
 
 
045a9fb
157a73d
6c11ffb
bfd5b9c
157a73d
23db19f
bfd5b9c
157a73d
 
 
 
 
a584bd9
6c11ffb
a584bd9
157a73d
 
 
 
 
a584bd9
bfd5b9c
 
157a73d
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, request, jsonify, url_for, send_from_directory
import os
from datetime import datetime
import logging

app = Flask(__name__)

# Настройка папки для загрузки изображений
app.config['UPLOAD_FOLDER'] = 'static/images'

# Настройка логирования
logging.basicConfig(level=logging.DEBUG)

handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
app.logger.addHandler(handler)

# Главный маршрут, теперь принимающий POST-запросы
@app.route('/', methods=['POST'])
def process_image():
    if 'image' not in request.files:
        return jsonify({"error": "No image part in the request"}), 400

    file = request.files['image']

    if file.filename == '':
        return jsonify({"error": "No image selected for uploading"}), 400

    if file:
        try:
            # Генерируем уникальное имя файла для сохранения изображения
            timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
            filename = f'image_{timestamp}.png'
            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)

            # Сохраняем изображение
            file.save(filepath)

            # Генерируем URL для доступа к изображению
            image_url = url_for('uploaded_file', filename=filename, _external=True)

            # Отправляем ответ с URL изображения
            return jsonify({"image_url": image_url})
        except Exception as e:
            app.logger.error(f'An error occurred: {e}')
            return jsonify({"error": "An error occurred while processing the image"}), 500
    else:
        return jsonify({"error": "Invalid image data"}), 400

# Отдача статических файлов (изображений)
@app.route('/static/images/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
    # Убедитесь, что папка для загрузки существует
    os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
    
    # Запуск Flask-приложения с включенным режимом отладки
    app.run(host='0.0.0.0', port=7860, debug=True)