routes.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. Image.MAX_IMAGE_PIXELS = None
  16. #from backstage.utils.routes import update_manage_table
  17. upload_app = Blueprint('upload', __name__)
  18. @upload_app.route('/backstage/test/<path:iurl>', methods=['GET'])
  19. def upload_test(iurl):
  20. test = iurl[0:iurl.rfind("/")+1]
  21. new_response = requests.get(PORTAL_SERVER + 'contents?url=' + test)
  22. #my_dict = json.loads(new_response.content.decode('utf-8'))[0].items()
  23. for key, value in json.loads(new_response.content.decode('utf-8'))[0].items():
  24. if key == 'path':
  25. print(value.replace('\\', '/'))
  26. # print(my_dict[0].items())
  27. return new_response.content
  28. @upload_app.route('/backstage/multiupload/<path:iurl>', methods=['POST']) #collections
  29. def upload_multi_post(iurl):
  30. # 取得md路徑
  31. mdPath = ""
  32. new_response = requests.get(PORTAL_SERVER + 'contents?url=' + iurl)
  33. #my_dict = json.loads(new_response.content.decode('utf-8'))[0].items()
  34. obj = json.loads(new_response.content.decode('utf-8'))
  35. if len(obj) > 0:
  36. for key, value in obj[0].items():
  37. if key == 'path':
  38. mdPath = value.replace('\\', '/')
  39. #print(mdPath)
  40. mdPath = mdPath[0:mdPath.rfind('/')+1]
  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("markdown path is" + 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. print(request.files)
  63. print(len(request.files))
  64. if len(request.files) == 0:
  65. aa = {"success": 0}
  66. return aa
  67. else:
  68. n=0
  69. filelink = ''
  70. for i in request.files:
  71. n+=1
  72. fname = 'image' + str(n)
  73. file = request.files[fname]
  74. print("The file is at" + filepath)
  75. # If the user does not select a file, the browser submits an
  76. # empty file without a filename.
  77. """ if file.filename == '':
  78. #flash('No selected file')
  79. return redirect(request.url) """
  80. oimgtype = file.filename[file.filename.rfind(".")+1:]
  81. oimgtypeName = oimgtype
  82. if oimgtype.lower() == 'jpg':
  83. oimgtypeName = 'jpeg'
  84. if file and not isProduct:
  85. #filename = secure_filename(file.filename)
  86. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  87. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  88. if filepath == "title":
  89. sitepath = UPLOAD_PATH_MAP[0][2] + "/static/img/title/"
  90. else:
  91. sitepath = mdPath + "/img/"
  92. #sitepath = UPLOAD_PATH_MAP[0][0] + "../" + itype + "/" + filepath + "/img/"
  93. wfname = str(uuid.uuid4()).replace('-','') + ".webp"
  94. owfname = wfname.replace('webp', oimgtype)
  95. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  96. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  97. # file.save(fullpath)
  98. #image_object = Image.open(fullpath)
  99. #print(request.args.get('url', type=str))
  100. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  101. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  102. print("1wpath is " + wpath)
  103. print("1owpath is " + owpath)
  104. #fullpath = wpath + fname
  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. # file.save(fullpath)
  112. image_object = Image.open(file)
  113. image_object.save(owpath+owfname)
  114. if image_object.size[0] > 1000:
  115. image_object.thumbnail(size=((1600, 1600)))
  116. image_object.save(wpath+wfname)
  117. image_object.save(sitepath+wfname)
  118. print("Loc1a: " + wpath+wfname)
  119. print("Loc1b: " + owpath+wfname)
  120. print("Loc2: " + sitepath+wfname)
  121. print(filepath)
  122. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  123. # return redirect(url_for('download_file', name=file.filename))
  124. filelink += wfname + ";;;"
  125. print(filelink)
  126. aa = {"success": 1, "fileurl": filelink}
  127. return aa
  128. if request.method == 'GET':
  129. print('GET')
  130. # print(request.files)
  131. # print(request.form)
  132. # print(requests.post("/backstage/upload"))
  133. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  134. return aa
  135. @upload_app.route('/backstage/upload/<path:iurl>', methods=['POST'])
  136. def upload_post(iurl):
  137. # 取得md路徑
  138. mdPath = ""
  139. new_response = requests.get(PORTAL_SERVER + 'contents?url=' + iurl)
  140. #my_dict = json.loads(new_response.content.decode('utf-8'))[0].items()
  141. obj = json.loads(new_response.content.decode('utf-8'))
  142. if len(obj) > 0:
  143. for key, value in obj[0].items():
  144. if key == 'path':
  145. mdPath = value.replace('\\', '/')
  146. #print(mdPath)
  147. mdPath = mdPath[0:mdPath.rfind('/')+1]
  148. #print(mdPath)
  149. print(mdPath)
  150. isProduct = False
  151. if mdPath.find("設計家具") >= 0 or mdPath.find("模組系統櫃") >= 0:
  152. isProduct = True
  153. #bdata = request.stream.read()
  154. filepath = ""
  155. itype = ""
  156. print(mdPath)
  157. #aa = request.get_data()
  158. if iurl == "title":
  159. itype = iurl
  160. filepath = iurl
  161. else:
  162. itype = iurl[0:iurl.find("/")]
  163. filepath = iurl[iurl.rfind("/")+1:]
  164. if request.method == 'POST':
  165. # check if the post request has the file part
  166. # print(request.full_path)
  167. """ if 'file' not in request.files:
  168. #flash('No file part')
  169. return redirect(request.url) """
  170. if len(request.files) == 0:
  171. aa = {"success": 0}
  172. return aa
  173. else:
  174. file = request.files['image']
  175. #file2 = request.files['image2'] #again, reference
  176. print("The file is at" + filepath)
  177. # If the user does not select a file, the browser submits an
  178. # empty file without a filename.
  179. """ if file.filename == '':
  180. #flash('No selected file')
  181. return redirect(request.url) """
  182. oimgtype = file.filename[file.filename.rfind(".")+1:]
  183. oimgtypeName = oimgtype
  184. if oimgtype.lower() == 'jpg':
  185. oimgtypeName = 'jpeg'
  186. if file and not isProduct:
  187. #filename = secure_filename(file.filename)
  188. #fname = str(uuid.uuid4()) + file.filename[file.filename.rfind("."):]
  189. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "/img/"
  190. if filepath == "title":
  191. sitepath = UPLOAD_PATH_MAP[0][2] + "/static/img/title/"
  192. else:
  193. sitepath = mdPath + "/img/"
  194. #sitepath = UPLOAD_PATH_MAP[0][0] + "../" + itype + "/" + filepath + "/img/"
  195. wfname = str(uuid.uuid4()).replace('-','') + ".webp"
  196. owfname = wfname.replace('webp', oimgtype)
  197. #fullpath = UPLOAD_PATH_MAP[0][0] + filepath[filepath.rfind("/")+1:] + "\\img\\" + fname
  198. #print(UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind("/")+1:] + "/img/" + fname)
  199. # file.save(fullpath)
  200. #image_object = Image.open(fullpath)
  201. #print(request.args.get('url', type=str))
  202. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  203. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  204. print("1wpath is " + wpath)
  205. print("1owpath is " + owpath)
  206. #fullpath = wpath + fname
  207. if not os.path.exists(wpath):
  208. os.makedirs(wpath)
  209. if not os.path.exists(owpath):
  210. os.makedirs(owpath)
  211. if not os.path.exists(sitepath):
  212. os.makedirs(sitepath)
  213. # file.save(fullpath)
  214. image_object = Image.open(file)
  215. image_object.save(owpath+owfname)
  216. if image_object.size[0] > 1000:
  217. image_object.thumbnail(size=((1600, 1600)))
  218. image_object.save(wpath+wfname)
  219. image_object.save(sitepath+wfname)
  220. print("Loc1a: " + wpath+wfname)
  221. print("Loc1b: " + owpath+wfname)
  222. print("Loc2: " + sitepath+wfname)
  223. print(filepath)
  224. #file.save(os.getcwd()+ "/backstage/upload/img/"+ fname)
  225. # return redirect(url_for('download_file', name=file.filename))
  226. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  227. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  228. # aa = {"success": 1, "file": {"url": UPLOAD_PATH_MAP[0][1] + filepath[filepath.rfind(
  229. # "/")+1:] + "/img/" + fname, "width": image_object.width, "height": image_object.height}}
  230. return aa
  231. elif isProduct:
  232. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  233. print("2wpath is " + wpath)
  234. wfname = str(uuid.uuid4()) + ".webp"
  235. image_object = Image.open(file)
  236. #image_object.save(mdPath+wfname, oimgtypeName)
  237. if image_object.size[0] > 1000:
  238. image_object.thumbnail(size=((1600, 1600)))
  239. if not os.path.exists(wpath):
  240. os.makedirs(wpath)
  241. image_object.save(wpath+wfname)
  242. image_object.save(mdPath+wfname)
  243. print("Loc3: " + wpath+wfname)
  244. print("Loc4: " + mdPath+wfname)
  245. owfname = wfname.replace('webp', oimgtype)
  246. aa = {"success": 1, "file": {"url": "/backstage/upload/" + filepath+"/img/" + wfname,
  247. "width": image_object.width, "height": image_object.height}}
  248. return aa
  249. if request.method == 'GET':
  250. print('GET')
  251. # print(request.files)
  252. # print(request.form)
  253. # print(requests.post("/backstage/upload"))
  254. aa = {"success": 1, "file": {"url": "http://www.choozmo.com/images/logo%20%281%29.webp", }}
  255. return aa
  256. @upload_app.route('/backstage/upload/<path:filepath>', methods=['GET'])
  257. def upload_get(filepath):
  258. # print(filepath)
  259. #print(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  260. #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } }
  261. # return redirect(url_for('upload.upload_get',filename=filename), code=301)
  262. # return send_from_directory(os.getcwd() + "/backstage/upload/"+filepath, filepath[filepath.rfind("/")+1:])
  263. return send_file(os.getcwd() + "/backstage/upload/"+filepath)
  264. @upload_app.route('/backstage/getimage/<path:iurl>', methods=['POST', 'GET'])
  265. def get_image(iurl):
  266. itype = iurl[0:iurl.find("/")]
  267. filepath = iurl[iurl.rfind("/")+1:]
  268. # print(request.get_json()['url'])
  269. if itype == "blog":
  270. sitepath = UPLOAD_PATH_MAP[0][0] + "../blog/" + filepath + "/img/"
  271. else:
  272. sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  273. #sitepath = UPLOAD_PATH_MAP[0][0] + filepath + "/img/"
  274. oimgtype = str(request.get_json()['url'])[str(request.get_json()['url']).rfind(".")+1:]
  275. oimgtypeName = oimgtype
  276. if oimgtype.lower() == 'jpg':
  277. oimgtypeName = 'jpeg'
  278. # fname = str(uuid.uuid4()) + str(request.get_json()
  279. # ['url'])[str(request.get_json()['url']).rfind("."):]
  280. wfname = str(uuid.uuid4()) + ".webp"
  281. owfname = wfname.replace('webp', oimgtype)
  282. wpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/"
  283. owpath = os.getcwd() + "/backstage/upload/" + filepath+"/img/orig/"
  284. """ fullpath = wpath + fname
  285. if not os.path.exists(wpath):
  286. os.makedirs(wpath)
  287. f = open(fullpath, 'wb')
  288. f.write(requests.get(request.get_json()['url']).content)
  289. f.close() """
  290. if not os.path.exists(wpath):
  291. os.makedirs(wpath)
  292. if not os.path.exists(owpath):
  293. os.makedirs(owpath)
  294. if not os.path.exists(sitepath):
  295. os.makedirs(sitepath)
  296. image_object = Image.open(requests.get(request.get_json()['url'], stream=True).raw)
  297. image_object.save(owpath+owfname)
  298. if image_object.size[0] > 1000:
  299. image_object.thumbnail(size=((1600, 1600)))
  300. image_object.save(wpath+wfname)
  301. image_object.save(sitepath+wfname)
  302. # send_file()
  303. #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } }
  304. #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301)
  305. #resp.content_type = "image/jpeg"
  306. #resp.content_encoding = "Unicode"
  307. # return redirect(request.get_json()['url'], code=301)
  308. aa = {"success": 1, "file": {"url": "/backstage/upload/" +
  309. filepath+"/img/" + wfname, "width": image_object.width, "height": image_object.height}}
  310. return aa
  311. @upload_app.route('/backstage/modTitle/<path:filepath>', methods=['GET'])
  312. def modify_title(filepath):
  313. oldPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[0]
  314. newPath = UPLOAD_PATH_MAP[0][0] + filepath.split('/')[1]
  315. # os.renames(oldPath,newPath)
  316. """ if os.path.exists(newPath):
  317. return {"success" : 0, "message" : "New directory exists."} """
  318. """ shutil.copytree(oldPath, newPath)
  319. shutil.rmtree(oldPath) """
  320. print(oldPath + ' => ' + newPath)
  321. return {"success": 1}