1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- from flask import Blueprint, request, jsonify, redirect, url_for, current_app, send_from_directory, make_response
- import datetime
- import os
- import shutil
- bp = Blueprint('api_v1', __name__, url_prefix='/api/v1')
- @bp.route('/')
- def hello_world():
- return redirect(url_for('static', filename='./index.html'))
- @bp.route('/upload', methods=['GET', 'POST'])
- def upload_file():
- file = request.files['file']
- print(datetime.datetime.now(), file.filename)
- if file and allowed_file(file.filename):
- src_path = os.path.join(bp.config['UPLOAD_FOLDER'], file.filename)
- file.save(src_path)
- shutil.copy(src_path, './tmp/ct')
- image_path = os.path.join('./tmp/ct', file.filename)
- pid, image_info = core.main.c_main(
- image_path, current_app.model, file.filename.rsplit('.', 1)[1])
- return jsonify({'status': 1,
- 'image_url': 'http://127.0.0.1:5003/tmp/ct/' + pid,
- 'draw_url': 'http://127.0.0.1:5003/tmp/draw/' + pid,
- 'image_info': image_info})
- return jsonify({'status': 0})
- # with app.app_context():
- # current_app.model = Detector()
- @bp.route("/download", methods=['GET'])
- def download_file():
- # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
- return send_from_directory('data', 'testfile.zip', as_attachment=True)
- @bp.route('/tmp/<path:file>', methods=['GET'])
- def show_photo(file):
- ''' 显示图片 '''
- if request.method == 'GET':
- if not file is None:
- image_data = open(f'tmp/{file}', "rb").read()
- response = make_response(image_data)
- response.headers['Content-Type'] = 'image/png'
- return response
- def allowed_file(filename) -> bool:
- '''判断文件后缀是否在允许范围'''
- ALLOWED_EXTENSIONS = set(['png', 'jpg'])
- return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
|