routes.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. import json
  14. from backstage.config import PORTAL_SERVER
  15. #from backstage.utils.routes import update_manage_table
  16. upload_app = Blueprint('upload', __name__)
  17. @upload_app.route('/backstage/test/<path:iurl>', methods=['GET'])
  18. def upload_test(iurl):
  19. test = iurl[0:iurl.rfind("/")+1]
  20. new_response = requests.get(PORTAL_SERVER + 'contents?url=' + test)
  21. #my_dict = json.loads(new_response.content.decode('utf-8'))[0].items()
  22. for key, value in json.loads(new_response.content.decode('utf-8'))[0].items():
  23. if key == 'path':
  24. print(value.replace('\\', '/'))
  25. # print(my_dict[0].items())
  26. return new_response.content
  27. @upload_app.route('/backstage/upload/<path:iurl>', methods=['POST'])
  28. def upload_post(iurl):
  29. # 取得md路徑
  30. mdPath = ""
  31. new_response = requests.get(PORTAL_SERVER + 'contents?url=' + iurl)
  32. #my_dict = json.loads(new_response.content.decode('utf-8'))[0].items()
  33. obj = json.loads(new_response.content.decode('utf-8'))
  34. if len(obj) > 0:
  35. for key, value in obj[0].items():
  36. if key == 'path':
  37. mdPath = value.replace('\\', '/')
  38. #print(mdPath)
  39. mdPath = mdPath[0:mdPath.rfind('/')+1]
  40. #print(mdPath)
  41. print(mdPath)
  42. isProduct = False
  43. if mdPath.find("設計家具") >= 0 or mdPath.find("模組系統櫃") >= 0:
  44. isProduct = True
  45. #bdata = request.stream.read()
  46. filepath = ""
  47. itype = ""
  48. print(mdPath)
  49. #aa = request.get_data()
  50. if iurl == "title":
  51. itype = iurl
  52. filepath = iurl
  53. else:
  54. itype = iurl[0:iurl.find("/")]
  55. filepath = iurl[iurl.rfind("/")+1:]
  56. if request.method == 'POST':
  57. # check if the post request has the file part
  58. # print(request.full_path)
  59. """ if 'file' not in request.files:
  60. #flash('No file part')
  61. return redirect(request.url) """
  62. if len(request.files) == 0:
  63. aa = {"success": 0}
  64. return aa
  65. else:
  66. file = request.files['image']
  67. print("The file is at" + filepath)
  68. # If the user does not select a file, the browser submits an
  69. # empty file without a filename.
  70. """ if file.filename == '':
  71. #flash('No selected file')
  72. return redirect(request.url) """
  73. oimgtype = file.filename[file.filename.rfind(".")+1:]
  74. oimgtypeName = oimgtype
  75. if oimgtype.lower() == 'jpg':
  76. oimgtypeName = 'jpeg'
  77. if file and not isProduct:
  78. #filename = secure_filename(file.filename)
  79. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  80. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  81. if filepath == "title":
  82. sitepath = UPLOAD_PATH_MAP[0][2] + "/static/img/title/"
  83. else:
  84. sitepath = mdPath + "/img/"
  85. #sitepath = UPLOAD_PATH_MAP[0][0] + "../" + itype + "/" + filepath + "/img/"
  86. wfname = str(uuid.uuid4()).replace('-','') + ".webp"
  87. owfname = wfname.replace('webp', oimgtype)
  88. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  89. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  90. # file.save(fullpath)
  91. #image_object = Image.open(fullpath)
  92. #print(request.args.get('url', type=str))
  93. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  94. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  95. print("1wpath is " + wpath)
  96. print("1owpath is " + owpath)
  97. #fullpath = wpath + fname
  98. if not os.path.exists(wpath):
  99. os.makedirs(wpath)
  100. if not os.path.exists(owpath):
  101. os.makedirs(owpath)
  102. if not os.path.exists(sitepath):
  103. os.makedirs(sitepath)
  104. # file.save(fullpath)
  105. image_object = Image.open(file)
  106. image_object.save(owpath+owfname)
  107. if image_object.size[0] > 1000:
  108. image_object.thumbnail(size=((1600, 1600)))
  109. image_object.save(wpath+wfname)
  110. image_object.save(sitepath+wfname)
  111. print("Loc1a: " + wpath+wfname)
  112. print("Loc1b: " + owpath+wfname)
  113. print("Loc2: " + sitepath+wfname)
  114. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  115. # return redirect(url_for('download_file', name=file.filename))
  116. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  117. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  118. # aa = {"success": 1, "file": {"url": UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind(
  119. # "/")+1:] + "/img/" + fname, "width": image_object.width, "height": image_object.height}}
  120. return aa
  121. elif isProduct:
  122. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  123. print("2wpath is " + wpath)
  124. wfname = str(uuid.uuid4()) + ".webp"
  125. image_object = Image.open(file)
  126. #image_object.save(mdPath+wfname, oimgtypeName)
  127. if image_object.size[0] > 1000:
  128. image_object.thumbnail(size=((1600, 1600)))
  129. if not os.path.exists(wpath):
  130. os.makedirs(wpath)
  131. image_object.save(wpath+wfname)
  132. image_object.save(mdPath+wfname)
  133. print("Loc3: " + wpath+wfname)
  134. print("Loc4: " + mdPath+wfname)
  135. owfname = wfname.replace('webp', oimgtype)
  136. aa = {"success": 1, "file": {"url": "/backstage/upload/" + filepath+"/img/" + wfname,
  137. "width": image_object.width, "height": image_object.height}}
  138. return aa
  139. if request.method == 'GET':
  140. print('GET')
  141. # print(request.files)
  142. # print(request.form)
  143. # print(requests.post("/backstage/upload"))
  144. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  145. return aa
  146. @upload_app.route('/backstage/upload/<path:filepath>', methods=['GET'])
  147. def upload_get(filepath):
  148. # print(filepath)
  149. #print(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  150. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  151. # return redirect(url_for('upload.upload_get',filename=filename), code=301)
  152. # return send_from_directory(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  153. return send_file(os.getcwd() + "/backstage/upload/"+filepath)
  154. @upload_app.route('/backstage/getimage/<path:iurl>', methods=['POST', 'GET'])
  155. def get_image(iurl):
  156. itype = iurl[0:iurl.find("/")]
  157. filepath = iurl[iurl.rfind("/")+1:]
  158. # print(request.get_json()['url'])
  159. if itype == "blog":
  160. sitepath = UPLOAD_PATH_MAP[0][0] + "../blog/" + filepath + "/img/"
  161. else:
  162. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  163. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  164. oimgtype = str(request.get_json()['url'])[str(request.get_json()['url']).rfind(".")+1:]
  165. oimgtypeName = oimgtype
  166. if oimgtype.lower() == 'jpg':
  167. oimgtypeName = 'jpeg'
  168. # fname = str(uuid.uuid4()) + str(request.get_json()
  169. # ['url'])[str(request.get_json()['url']).rfind("."):]
  170. wfname = str(uuid.uuid4()) + ".webp"
  171. owfname = wfname.replace('webp', oimgtype)
  172. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  173. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  174. """ fullpath = wpath + fname
  175. if not os.path.exists(wpath):
  176. os.makedirs(wpath)
  177. f = open(fullpath, 'wb')
  178. f.write(requests.get(request.get_json()['url']).content)
  179. f.close() """
  180. if not os.path.exists(wpath):
  181. os.makedirs(wpath)
  182. if not os.path.exists(owpath):
  183. os.makedirs(owpath)
  184. if not os.path.exists(sitepath):
  185. os.makedirs(sitepath)
  186. image_object = Image.open(requests.get(request.get_json()['url'], stream=True).raw)
  187. image_object.save(owpath+owfname)
  188. if image_object.size[0] > 1000:
  189. image_object.thumbnail(size=((1600, 1600)))
  190. image_object.save(wpath+wfname)
  191. image_object.save(sitepath+wfname)
  192. # send_file()
  193. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  194. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  195. #resp.content_type = "image/jpeg"
  196. #resp.content_encoding = "Unicode"
  197. # return redirect(request.get_json()['url'], code=301)
  198. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  199. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  200. return aa
  201. @upload_app.route('/backstage/modTitle/<path:filepath>', methods=['GET'])
  202. def modify_title(filepath):
  203. oldPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[0]
  204. newPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[1]
  205. # os.renames(oldPath,newPath)
  206. """ if os.path.exists(newPath):
  207. return {"success" : 0, "message" : "New directory exists."} """
  208. """ shutil.copytree(oldPath, newPath)
  209. shutil.rmtree(oldPath) """
  210. print(oldPath + ' => ' + newPath)
  211. return {"success": 1}