| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- from hashlib import md5
- from pathlib import Path
- from flask import Flask,Response, request,jsonify,render_template, request, url_for
- import os
- from flask_wtf.csrf import CSRFProtect
- from flask import current_app as app
- from werkzeug.utils import secure_filename
- from datetime import datetime
- #basedir = os.path.abspath(os.path.dirname(__file__))
- basedir = "C:/website/DTAILab3"
- image_base_dir = os.path.join(basedir, app.config['IMG_UPLOAD_FOLDER'])
- if not os.path.exists(image_base_dir):
- os.makedirs(image_base_dir)
- csrf = CSRFProtect(app)
- @app.route("/")
- def mak():
- return render_template("admin/a_websitemanage2.html")
- @csrf.exempt
- @app.route('/vditor/uploads',methods=['POST','GET'])
- def vditor_uploads():
- """
- 支持黏贴、拖拽和点击图片上传
- """
- images = request.files.get('file[]', None)
- if not images:
- res = {
- 'success': 0,
- 'message': '上传失败',
- 'code': "404"
- }
- else:
- img = images.stream.read()
- digest=md5(img).hexdigest()
- suffix = Path(images.filename).suffix
- images_name = datetime.now().strftime('%Y%m%d%H%M%S') + f'{digest}{suffix}'
- image_full_name = os.path.join(app.config['IMG_UPLOAD_FOLDER'], images_name)
- if not Path(image_full_name).exists():
- with open(image_full_name,"wb") as f :
- f.write(img)
- #image_full_path = os.path.join(app.config['IMG_UPLOAD_URL'], images_name)
- image_full_path = os.path.join(app.config['IMG_UPLOAD_FOLDER'], images_name)
- #print(image_full_path)
- # 返回的json有指定的结构
- res = {
- "msg": "Success!",
- "code": '200',
- 'url': url_for('.image', name=image_full_path),
- "data": {
- "errFiles": [],
- "succMap": {
- images.filename: image_full_path,
- }
- }
- }
- return jsonify(res)
- @csrf.exempt
- @app.route('/vditor/save/',methods=['POST'])
- def vditor_save():
- print('ddd')
- """"
- markdown 保存
- json格式
- """
- data = request.json
- print(data['fname'])
- print(data['content'])
- # save it
- data.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(data['fname'])))
- return jsonify({"msg":0}),200
- # 返回图片
- @app.route('/image/<name>')
- def image(name):
- with open(os.path.join(image_base_dir, name), 'rb') as image_f:
- resp = Response(image_f.read(), mimetype='image/jpeg')
- return resp
|