File size: 1,262 Bytes
b9825a0
 
4b1765d
b9825a0
4b1765d
 
 
b9825a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b1765d
b9825a0
4b1765d
 
6534cc2
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
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
import os
import uuid

app = Flask(__name__)

UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({"code": 1, "msg": "No file part"}), 400
    file = request.files['file']
    if file.filename == '':
        return jsonify({"code": 1, "msg": "No selected file"}), 400
    if file:
        filename = secure_filename(file.filename)
        file_extension = os.path.splitext(filename)[1]
        unique_filename = f"{uuid.uuid4()}{file_extension}"
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
        file.save(file_path)
        
        is_image = file.content_type.startswith('image/')
        
        return jsonify({
            "code": 0,
            "msg": "ok",
            "data": {
                "url": f"https://mistpe-file.hf.space/uploads/{unique_filename}",
                "filename": filename,
                "image": is_image
            }
        })

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)