routes.py 6.4 KB

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