代码拉取完成,页面将自动刷新
同步操作将从 vitojc/fast-poster 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from flask import Flask, Response, request
from flask_cors import CORS
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
import C
import R
import dao
import poster
import key
app = Flask(__name__, static_url_path='')
logger = app.logger
# 跨域配置
CORS(app, supports_credentials=True)
# 上传配置
app.config['UPLOADED_PHOTOS_DEST'] = C.STORE_UPLOAD # 文件储存地址
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app) # 文件大小限制,默认为16MB
@app.route('/')
def index():
"""
重定向首页
:return:
"""
return app.send_static_file('index.html')
@app.route('/api/login', methods=['POST'])
def login():
"""
登录
:return:
"""
accessKey = request.form['accessKey']
secretKey = request.form['secretKey']
if key.check(accessKey, secretKey):
token = C.code(32)
dao.save_token(token)
return R.ok('登录成功').add('token', token).add('user', {'accessKey': accessKey, 'secretKey': secretKey}).json()
else:
return R.error('accessKey or secretKey not match!').json()
@app.before_request
def check_token():
"""
检查token
:return:
"""
path = request.path
filter_list = ['/api/user/posters']
ignore = True
for w in filter_list:
if w in path:
ignore = False
break
if ignore:
return
t = request.headers.get('token', None)
if not t:
return R.expire('没有token').json()
dbtoken = dao.query_token(t)
print(f'token判断: path={path}, token={t}, dbtoken={dbtoken}')
if not dbtoken:
return R.expire().json()
@app.route('/api/user/posters', methods=['GET'])
def query_user_posters():
posters = dao.query_user_posters()
return R.ok().add('posters', posters).json()
@app.route('/api/user/posters', methods=['POST'])
def save_or_update_user_poster():
id = dao.save_or_update_user_poster(request.get_json())
return R.ok().add("id", id).json()
@app.route('/api/user/posters/<id>', methods=['DELETE'])
def delete_user_posters(id):
dao.db_delete_poster(int(id))
return R.ok().json()
@app.route('/api/user/posters/copy/<id>', methods=['POST'])
def copy_user_poster(id):
id = dao.copy_user_poster(id)
return R.ok().add("id", id).json()
@app.route('/api/upload', methods=['POST'])
def upload_file():
name = C.code(16)
filename = photos.save(request.files['file'], name=name + '.')
# filename = photos.save(request.files['file'], name=name)
path = photos.path(filename)
path = C.get_url_path(path)
return R.ok().add("url", path).json()
@app.route('/api/link', methods=['POST'])
def get_link():
"""获取分享链接"""
# TODO: 接口参数校验
# print("获取分享链接")
param = request.get_json()
if not key.check(param['accessKey'], param['secretKey']):
return R.error('accessKey or secretKey not match').json()
return dao.get_share_link(param)
@app.route('/view/<string:code>', methods=['GET'])
def view(code: str):
"""通过分享链接访问"""
# print('通过分享链接访问 code=' + code)
code = code[:code.index('.')]
# print('通过分享链接访问 code=' + code)
data = dao.find_share_data(code)
if data is None:
# TODO: 返回一张提示图片
return 'dfs'
return resp_poster_img(data)
@app.route('/api/preview', methods=['POST'])
def preview():
"""预览"""
logger.info('预览')
data = request.get_json()
return resp_poster_img(data)
def resp_poster_img(data):
"""返回海报图片"""
buf, mimetype = poster.drawio(data)
resp = Response(buf, mimetype=mimetype)
resp.add_etag()
resp.automatically_set_content_length = True
resp.headers.add('Cache-Control', 'max-age=60')
# resp.headers.add('Access-Control-Allow-Origin', '*')
logger.info('请求返回了')
return resp
if __name__ == '__main__':
# app.run(host="0.0.0.0", port=9001, debug=True)
app.run(host="0.0.0.0", port=9001, debug=True, threaded=False, processes=2)
print('启动...')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。