routes.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. from flask import render_template, Blueprint, request, redirect, url_for
  3. from flask_cors import CORS, cross_origin
  4. from flask.helpers import make_response, send_file, send_from_directory
  5. from werkzeug.wrappers import Response
  6. import requests
  7. from PIL import Image
  8. from urllib.parse import urlparse
  9. import uuid
  10. #from backstage.utils.routes import update_manage_table
  11. from backstage.config import PORTAL_SERVER, BHOUSE_SERVER
  12. upload_app = Blueprint('upload', __name__)
  13. @upload_app.route('/backstage/upload', methods=['POST','GET'])
  14. def upload_post():
  15. #bdata = request.stream.read()
  16. #aa = request.get_data()
  17. if request.method == 'POST':
  18. print(request.method)
  19. # check if the post request has the file part
  20. #print(request.files)
  21. """ if 'file' not in request.files:
  22. #flash('No file part')
  23. return redirect(request.url) """
  24. file = request.files['image']
  25. # If the user does not select a file, the browser submits an
  26. # empty file without a filename.
  27. """ if file.filename == '':
  28. #flash('No selected file')
  29. return redirect(request.url) """
  30. if file:
  31. #filename = secure_filename(file.filename)
  32. #print(os.getcwd().)
  33. file.save(os.getcwd()+ "/backstage/upload/img/"+ file.filename)
  34. #return redirect(url_for('download_file', name=file.filename))
  35. aa = {"success" : 1,"file": { "url" : "/backstage/upload/"+ file.filename, } }
  36. return aa
  37. if request.method == 'GET':
  38. print(request.method)
  39. #print(request.files)
  40. #print(request.form)
  41. #print(requests.post("/backstage/upload"))
  42. aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  43. return aa
  44. @upload_app.route('/backstage/upload/<filename>', methods=['GET'])
  45. def upload_get(filename):
  46. #print(filename)
  47. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  48. #return redirect(url_for('upload.upload_get',filename=filename), code=301)
  49. return send_from_directory(os.getcwd()+ "/backstage/upload/img" , filename)
  50. @upload_app.route('/backstage/getimage', methods=['POST','GET'])
  51. def get_image():
  52. print(request.get_json()['url'])
  53. fname = str(uuid.uuid4()) + str(request.get_json()['url'])[str(request.get_json()['url']).rfind("."):]
  54. f = open(os.getcwd()+ "/backstage/upload/img/" + fname,'wb')
  55. f.write(requests.get(request.get_json()['url']).content)
  56. f.close()
  57. #image_object = Image.open(os.getcwd()+ "/backstage/upload/tmp")
  58. #send_file()
  59. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  60. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  61. #resp.content_type = "image/jpeg"
  62. #resp.content_encoding = "Unicode"
  63. #return redirect(request.get_json()['url'], code=301)
  64. aa = {"success" : 1,"file": { "url" : "/backstage/upload/"+ fname , } }
  65. return aa