v1.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from flask import Blueprint, request, jsonify, redirect, url_for, current_app, send_from_directory, make_response
  2. import datetime
  3. import os
  4. import shutil
  5. bp = Blueprint('api_v1', __name__, url_prefix='/api/v1')
  6. @bp.route('/')
  7. def hello_world():
  8. return redirect(url_for('static', filename='./index.html'))
  9. @bp.route('/upload', methods=['GET', 'POST'])
  10. def upload_file():
  11. file = request.files['file']
  12. print(datetime.datetime.now(), file.filename)
  13. if file and allowed_file(file.filename):
  14. src_path = os.path.join(bp.config['UPLOAD_FOLDER'], file.filename)
  15. file.save(src_path)
  16. shutil.copy(src_path, './tmp/ct')
  17. image_path = os.path.join('./tmp/ct', file.filename)
  18. pid, image_info = core.main.c_main(
  19. image_path, current_app.model, file.filename.rsplit('.', 1)[1])
  20. return jsonify({'status': 1,
  21. 'image_url': 'http://127.0.0.1:5003/tmp/ct/' + pid,
  22. 'draw_url': 'http://127.0.0.1:5003/tmp/draw/' + pid,
  23. 'image_info': image_info})
  24. return jsonify({'status': 0})
  25. # with app.app_context():
  26. # current_app.model = Detector()
  27. @bp.route("/download", methods=['GET'])
  28. def download_file():
  29. # 需要知道2个参数, 第1个参数是本地目录的path, 第2个参数是文件名(带扩展名)
  30. return send_from_directory('data', 'testfile.zip', as_attachment=True)
  31. @bp.route('/tmp/<path:file>', methods=['GET'])
  32. def show_photo(file):
  33. ''' 显示图片 '''
  34. if request.method == 'GET':
  35. if not file is None:
  36. image_data = open(f'tmp/{file}', "rb").read()
  37. response = make_response(image_data)
  38. response.headers['Content-Type'] = 'image/png'
  39. return response
  40. def allowed_file(filename) -> bool:
  41. '''判断文件后缀是否在允许范围'''
  42. ALLOWED_EXTENSIONS = set(['png', 'jpg'])
  43. return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS