routes.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from backstage.config import PORTAL_SERVER, BHOUSE_SERVER
  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. UPLOAD_PATH_MAP = [["C:\\Users\\yukyo\\Desktop\\bhouseWeb\\content\\規劃作品集\\",
  13. "http://localhost:1313/collection/"]]
  14. #from backstage.utils.routes import update_manage_table
  15. upload_app = Blueprint('upload', __name__)
  16. @upload_app.route('/backstage/upload/<path:filepath>', methods=['POST'])
  17. def upload_post(filepath):
  18. #bdata = request.stream.read()
  19. #aa = request.get_data()
  20. if request.method == 'POST':
  21. # check if the post request has the file part
  22. # print(request.full_path)
  23. """ if 'file' not in request.files:
  24. #flash('No file part')
  25. return redirect(request.url) """
  26. file = request.files['image']
  27. # If the user does not select a file, the browser submits an
  28. # empty file without a filename.
  29. """ if file.filename == '':
  30. #flash('No selected file')
  31. return redirect(request.url) """
  32. if file:
  33. #filename = secure_filename(file.filename)
  34. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  35. sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  36. oimgtype = file.filename[file.filename.rfind(".")+1:]
  37. oimgtypeName = oimgtype
  38. if oimgtype.lower() == 'jpg':
  39. oimgtypeName = 'jpeg'
  40. wfname = str(uuid.uuid4()) + ".webp"
  41. owfname = wfname.replace('webp',oimgtype)
  42. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  43. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  44. # file.save(fullpath)
  45. #image_object = Image.open(fullpath)
  46. #print(request.args.get('url', type=str))
  47. wpath = os.getcwd() + "/backstage/upload/" + filepath[filepath.rfind("/")+1:]+"/img/"
  48. owpath = os.getcwd() + "/backstage/upload/" + filepath[filepath.rfind("/")+1:]+"/img/orig/"
  49. #fullpath = wpath + fname
  50. if not os.path.exists(wpath):
  51. os.makedirs(wpath)
  52. if not os.path.exists(owpath):
  53. os.makedirs(owpath)
  54. if not os.path.exists(sitepath):
  55. os.makedirs(sitepath)
  56. #file.save(fullpath)
  57. image_object = Image.open(file)
  58. image_object.save(owpath+owfname, oimgtypeName)
  59. if image_object.size[0] > 1000:
  60. image_object.thumbnail(size=((800, 800)))
  61. image_object.save(wpath+wfname, 'webp')
  62. image_object.save(sitepath+wfname , 'webp')
  63. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  64. # return redirect(url_for('download_file', name=file.filename))
  65. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  66. filepath[filepath.rfind("/")+1:]+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  67. # aa = {"success": 1, "file": {"url": UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind(
  68. # "/")+1:] + "/img/" + fname, "width": image_object.width, "height": image_object.height}}
  69. return aa
  70. if request.method == 'GET':
  71. print('GET')
  72. # print(request.files)
  73. # print(request.form)
  74. # print(requests.post("/backstage/upload"))
  75. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  76. return aa
  77. @upload_app.route('/backstage/upload/<path:filepath>', methods=['GET'])
  78. def upload_get(filepath):
  79. # print(filepath)
  80. #print(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  81. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  82. # return redirect(url_for('upload.upload_get',filename=filename), code=301)
  83. # return send_from_directory(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  84. return send_file(os.getcwd() + "/backstage/upload/"+filepath)
  85. @upload_app.route('/backstage/getimage/<path:filepath>', methods=['POST', 'GET'])
  86. def get_image(filepath):
  87. #print(request.get_json()['url'])
  88. sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  89. oimgtype = str(request.get_json()['url'])[str(request.get_json()['url']).rfind(".")+1:]
  90. oimgtypeName = oimgtype
  91. if oimgtype.lower() == 'jpg':
  92. oimgtypeName = 'jpeg'
  93. #fname = str(uuid.uuid4()) + str(request.get_json()
  94. # ['url'])[str(request.get_json()['url']).rfind("."):]
  95. wfname = str(uuid.uuid4()) + ".webp"
  96. owfname = wfname.replace('webp',oimgtype)
  97. wpath = os.getcwd() + "/backstage/upload/" + filepath[filepath.rfind("/")+1:]+"/img/"
  98. owpath = os.getcwd() + "/backstage/upload/" + filepath[filepath.rfind("/")+1:]+"/img/orig/"
  99. """ fullpath = wpath + fname
  100. if not os.path.exists(wpath):
  101. os.makedirs(wpath)
  102. f = open(fullpath, 'wb')
  103. f.write(requests.get(request.get_json()['url']).content)
  104. f.close() """
  105. if not os.path.exists(wpath):
  106. os.makedirs(wpath)
  107. if not os.path.exists(owpath):
  108. os.makedirs(owpath)
  109. if not os.path.exists(sitepath):
  110. os.makedirs(sitepath)
  111. image_object = Image.open(requests.get(request.get_json()['url'],stream=True).raw)
  112. image_object.save(owpath+owfname, oimgtypeName)
  113. if image_object.size[0] > 1000:
  114. image_object.thumbnail(size=((800, 800)))
  115. image_object.save(wpath+wfname, 'webp')
  116. image_object.save(sitepath+wfname , 'webp')
  117. # send_file()
  118. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  119. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  120. #resp.content_type = "image/jpeg"
  121. #resp.content_encoding = "Unicode"
  122. # return redirect(request.get_json()['url'], code=301)
  123. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  124. filepath[filepath.rfind("/")+1:]+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  125. return aa