routes.py 15 KB

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