b_markdown.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from hashlib import md5
  2. from pathlib import Path
  3. from flask import Flask,Response, request,jsonify,render_template, request, url_for
  4. import os
  5. from flask_wtf.csrf import CSRFProtect
  6. from flask import current_app as app
  7. from werkzeug.utils import secure_filename
  8. from datetime import datetime
  9. #basedir = os.path.abspath(os.path.dirname(__file__))
  10. basedir = "C:/website/DTAILab3"
  11. image_base_dir = os.path.join(basedir, app.config['IMG_UPLOAD_FOLDER'])
  12. if not os.path.exists(image_base_dir):
  13. os.makedirs(image_base_dir)
  14. csrf = CSRFProtect(app)
  15. @app.route("/")
  16. def mak():
  17. return render_template("admin/a_websitemanage2.html")
  18. @csrf.exempt
  19. @app.route('/vditor/uploads',methods=['POST','GET'])
  20. def vditor_uploads():
  21. """
  22. 支持黏贴、拖拽和点击图片上传
  23. """
  24. images = request.files.get('file[]', None)
  25. if not images:
  26. res = {
  27. 'success': 0,
  28. 'message': '上传失败',
  29. 'code': "404"
  30. }
  31. else:
  32. img = images.stream.read()
  33. digest=md5(img).hexdigest()
  34. suffix = Path(images.filename).suffix
  35. images_name = datetime.now().strftime('%Y%m%d%H%M%S') + f'{digest}{suffix}'
  36. image_full_name = os.path.join(app.config['IMG_UPLOAD_FOLDER'], images_name)
  37. if not Path(image_full_name).exists():
  38. with open(image_full_name,"wb") as f :
  39. f.write(img)
  40. #image_full_path = os.path.join(app.config['IMG_UPLOAD_URL'], images_name)
  41. image_full_path = os.path.join(app.config['IMG_UPLOAD_FOLDER'], images_name)
  42. #print(image_full_path)
  43. # 返回的json有指定的结构
  44. res = {
  45. "msg": "Success!",
  46. "code": '200',
  47. 'url': url_for('.image', name=image_full_path),
  48. "data": {
  49. "errFiles": [],
  50. "succMap": {
  51. images.filename: image_full_path,
  52. }
  53. }
  54. }
  55. return jsonify(res)
  56. @csrf.exempt
  57. @app.route('/vditor/save/',methods=['POST'])
  58. def vditor_save():
  59. print('ddd')
  60. """"
  61. markdown 保存
  62. json格式
  63. """
  64. data = request.json
  65. print(data['fname'])
  66. print(data['content'])
  67. # save it
  68. data.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(data['fname'])))
  69. return jsonify({"msg":0}),200
  70. # 返回图片
  71. @app.route('/image/<name>')
  72. def image(name):
  73. with open(os.path.join(image_base_dir, name), 'rb') as image_f:
  74. resp = Response(image_f.read(), mimetype='image/jpeg')
  75. return resp