本文主要是介绍海报生成器源码(Python版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
海报生成器源码(Python版)
- 效果图
- 部分源码
- 领取源码
- 下期更新预报
效果图
部分源码
from flask import Flask, Response, request
from flask_cors import CORS
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_classimport C
import R
import dao
import poster
import keyapp = 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.pathfilter_list = ['/api/user/posters']ignore = Truefor w in filter_list:if w in path:ignore = Falsebreakif ignore:returnt = 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 = Trueresp.headers.add('Cache-Control', 'max-age=60')# resp.headers.add('Access-Control-Allow-Origin', '*')logger.info('请求返回了')return respif __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('启动...')
领取源码
源码地址:海报生成器源码(Python版)
下期更新预报
影视管理系统(高级版)
- 📢博客主页:孤客网络科技工作室官方账号
- 📢欢迎点赞👍收藏⭐️留言 📝如有错误敬请指正!
- 📢本文由孤客原创,若侵权联系作者,首发于CSDN博客
- 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更好的生活💻
这篇关于海报生成器源码(Python版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!