wewe / app.py
mistpe's picture
Update app.py
6534cc2 verified
raw
history blame
1.26 kB
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)