routes.py 7.7 KB

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