routes.py 15 KB

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