routes.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from backstage.config import UPLOAD_PATH_MAP
  2. import os
  3. import shutil
  4. from flask import render_template, Blueprint, request, redirect, url_for
  5. import flask
  6. from flask_cors import CORS, cross_origin
  7. from flask.helpers import make_response, send_file, send_from_directory
  8. from werkzeug.wrappers import Response
  9. import requests
  10. from PIL import Image
  11. from urllib.parse import urlparse
  12. import uuid
  13. #from backstage.utils.routes import update_manage_table
  14. upload_app = Blueprint('upload', __name__)
  15. @upload_app.route('/backstage/upload/<path:filepath>', methods=['POST'])
  16. def upload_post(filepath):
  17. #bdata = request.stream.read()
  18. #aa = request.get_data()
  19. if request.method == 'POST':
  20. # check if the post request has the file part
  21. # print(request.full_path)
  22. """ if 'file' not in request.files:
  23. #flash('No file part')
  24. return redirect(request.url) """
  25. if len(request.files) == 0:
  26. aa = {"success": 0}
  27. return aa
  28. else:
  29. file = request.files['image']
  30. # If the user does not select a file, the browser submits an
  31. # empty file without a filename.
  32. """ if file.filename == '':
  33. #flash('No selected file')
  34. return redirect(request.url) """
  35. if file:
  36. #filename = secure_filename(file.filename)
  37. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  38. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  39. if filepath == "title":
  40. sitepath = UPLOAD_PATH_MAP[0][0] + "../../static/img/collection/"
  41. else:
  42. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  43. oimgtype = file.filename[file.filename.rfind(".")+1:]
  44. oimgtypeName = oimgtype
  45. if oimgtype.lower() == 'jpg':
  46. oimgtypeName = 'jpeg'
  47. wfname = str(uuid.uuid4()) + ".webp"
  48. owfname = wfname.replace('webp', oimgtype)
  49. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  50. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  51. # file.save(fullpath)
  52. #image_object = Image.open(fullpath)
  53. #print(request.args.get('url', type=str))
  54. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  55. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  56. #fullpath = wpath + fname
  57. if not os.path.exists(wpath):
  58. os.makedirs(wpath)
  59. if not os.path.exists(owpath):
  60. os.makedirs(owpath)
  61. if not os.path.exists(sitepath):
  62. os.makedirs(sitepath)
  63. # file.save(fullpath)
  64. image_object = Image.open(file)
  65. image_object.save(owpath+owfname, oimgtypeName)
  66. if image_object.size[0] > 1000:
  67. image_object.thumbnail(size=((1600, 1600)))
  68. image_object.save(wpath+wfname, 'webp')
  69. image_object.save(sitepath+wfname, 'webp')
  70. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  71. # return redirect(url_for('download_file', name=file.filename))
  72. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  73. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  74. # aa = {"success": 1, "file": {"url": UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind(
  75. # "/")+1:] + "/img/" + fname, "width": image_object.width, "height": image_object.height}}
  76. return aa
  77. if request.method == 'GET':
  78. print('GET')
  79. # print(request.files)
  80. # print(request.form)
  81. # print(requests.post("/backstage/upload"))
  82. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  83. return aa
  84. @upload_app.route('/backstage/upload/<path:filepath>', methods=['GET'])
  85. def upload_get(filepath):
  86. # print(filepath)
  87. #print(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  88. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  89. # return redirect(url_for('upload.upload_get',filename=filename), code=301)
  90. # return send_from_directory(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  91. return send_file(os.getcwd() + "/backstage/upload/"+filepath)
  92. @upload_app.route('/backstage/getimage/<path:filepath>', methods=['POST', 'GET'])
  93. def get_image(filepath):
  94. # print(request.get_json()['url'])
  95. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  96. oimgtype = str(request.get_json()['url'])[str(request.get_json()['url']).rfind(".")+1:]
  97. oimgtypeName = oimgtype
  98. if oimgtype.lower() == 'jpg':
  99. oimgtypeName = 'jpeg'
  100. # fname = str(uuid.uuid4()) + str(request.get_json()
  101. # ['url'])[str(request.get_json()['url']).rfind("."):]
  102. wfname = str(uuid.uuid4()) + ".webp"
  103. owfname = wfname.replace('webp', oimgtype)
  104. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  105. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  106. """ fullpath = wpath + fname
  107. if not os.path.exists(wpath):
  108. os.makedirs(wpath)
  109. f = open(fullpath, 'wb')
  110. f.write(requests.get(request.get_json()['url']).content)
  111. f.close() """
  112. if not os.path.exists(wpath):
  113. os.makedirs(wpath)
  114. if not os.path.exists(owpath):
  115. os.makedirs(owpath)
  116. if not os.path.exists(sitepath):
  117. os.makedirs(sitepath)
  118. image_object = Image.open(requests.get(request.get_json()['url'], stream=True).raw)
  119. image_object.save(owpath+owfname, oimgtypeName)
  120. if image_object.size[0] > 1000:
  121. image_object.thumbnail(size=((1600, 1600)))
  122. image_object.save(wpath+wfname, 'webp')
  123. image_object.save(sitepath+wfname, 'webp')
  124. # send_file()
  125. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  126. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  127. #resp.content_type = "image/jpeg"
  128. #resp.content_encoding = "Unicode"
  129. # return redirect(request.get_json()['url'], code=301)
  130. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  131. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  132. return aa
  133. @upload_app.route('/backstage/modTitle/<path:filepath>', methods=['GET'])
  134. def modify_title(filepath):
  135. oldPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[0]
  136. newPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[1]
  137. # os.renames(oldPath,newPath)
  138. if os.path.exists(newPath):
  139. return {"success" : 0, "message" : "New directory exists."}
  140. shutil.copytree(oldPath, newPath)
  141. shutil.rmtree(oldPath)
  142. print(oldPath + ' => ' + newPath)
  143. return {"success": 1}