routes.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from backstage.config import UPLOAD_PATH_MAP
  2. import os
  3. from flask import render_template, Blueprint, request, redirect, url_for
  4. import flask
  5. from flask_cors import CORS, cross_origin
  6. from flask.helpers import make_response, send_file, send_from_directory
  7. from werkzeug.wrappers import Response
  8. import requests
  9. from PIL import Image
  10. from urllib.parse import urlparse
  11. import uuid
  12. #from backstage.utils.routes import update_manage_table
  13. upload_app = Blueprint('upload', __name__)
  14. @upload_app.route('/backstage/upload/<path:filepath>', methods=['POST'])
  15. def upload_post(filepath):
  16. #bdata = request.stream.read()
  17. #aa = request.get_data()
  18. if request.method == 'POST':
  19. # check if the post request has the file part
  20. # print(request.full_path)
  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. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  33. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  34. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  35. oimgtype = file.filename[file.filename.rfind(".")+1:]
  36. oimgtypeName = oimgtype
  37. if oimgtype.lower() == 'jpg':
  38. oimgtypeName = 'jpeg'
  39. wfname = str(uuid.uuid4()) + ".webp"
  40. owfname = wfname.replace('webp', oimgtype)
  41. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  42. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  43. # file.save(fullpath)
  44. #image_object = Image.open(fullpath)
  45. #print(request.args.get('url', type=str))
  46. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  47. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  48. #fullpath = wpath + fname
  49. if not os.path.exists(wpath):
  50. os.makedirs(wpath)
  51. if not os.path.exists(owpath):
  52. os.makedirs(owpath)
  53. if not os.path.exists(sitepath):
  54. os.makedirs(sitepath)
  55. # file.save(fullpath)
  56. image_object = Image.open(file)
  57. image_object.save(owpath+owfname, oimgtypeName)
  58. if image_object.size[0] > 1000:
  59. image_object.thumbnail(size=((800, 800)))
  60. image_object.save(wpath+wfname, 'webp')
  61. image_object.save(sitepath+wfname, 'webp')
  62. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  63. # return redirect(url_for('download_file', name=file.filename))
  64. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  65. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  66. # aa = {"success": 1, "file": {"url": UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind(
  67. # "/")+1:] + "/img/" + fname, "width": image_object.width, "height": image_object.height}}
  68. return aa
  69. if request.method == 'GET':
  70. print('GET')
  71. # print(request.files)
  72. # print(request.form)
  73. # print(requests.post("/backstage/upload"))
  74. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  75. return aa
  76. @upload_app.route('/backstage/upload/<path:filepath>', methods=['GET'])
  77. def upload_get(filepath):
  78. # print(filepath)
  79. #print(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  80. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  81. # return redirect(url_for('upload.upload_get',filename=filename), code=301)
  82. # return send_from_directory(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  83. return send_file(os.getcwd() + "/backstage/upload/"+filepath)
  84. @upload_app.route('/backstage/getimage/<path:filepath>', methods=['POST', 'GET'])
  85. def get_image(filepath):
  86. # print(request.get_json()['url'])
  87. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  88. oimgtype = str(request.get_json()['url'])[str(request.get_json()['url']).rfind(".")+1:]
  89. oimgtypeName = oimgtype
  90. if oimgtype.lower() == 'jpg':
  91. oimgtypeName = 'jpeg'
  92. # fname = str(uuid.uuid4()) + str(request.get_json()
  93. # ['url'])[str(request.get_json()['url']).rfind("."):]
  94. wfname = str(uuid.uuid4()) + ".webp"
  95. owfname = wfname.replace('webp', oimgtype)
  96. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  97. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  98. """ fullpath = wpath + fname
  99. if not os.path.exists(wpath):
  100. os.makedirs(wpath)
  101. f = open(fullpath, 'wb')
  102. f.write(requests.get(request.get_json()['url']).content)
  103. f.close() """
  104. if not os.path.exists(wpath):
  105. os.makedirs(wpath)
  106. if not os.path.exists(owpath):
  107. os.makedirs(owpath)
  108. if not os.path.exists(sitepath):
  109. os.makedirs(sitepath)
  110. image_object = Image.open(requests.get(request.get_json()['url'], stream=True).raw)
  111. image_object.save(owpath+owfname, oimgtypeName)
  112. if image_object.size[0] > 1000:
  113. image_object.thumbnail(size=((1600, 1600)))
  114. image_object.save(wpath+wfname, 'webp')
  115. image_object.save(sitepath+wfname, 'webp')
  116. # send_file()
  117. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  118. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  119. #resp.content_type = "image/jpeg"
  120. #resp.content_encoding = "Unicode"
  121. # return redirect(request.get_json()['url'], code=301)
  122. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  123. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  124. return aa