From 618164d77db2c5f60101cf3d358651c5a1e13344 Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 1 Mar 2022 22:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=A1=A8=E6=83=85?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E7=9B=AE=E5=BD=95=E6=94=B9=E5=88=B0data?= =?UTF-8?q?=E9=87=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 3 +++ .gitignore | 3 +++ README.md | 3 +++ api/main.py | 23 +++++++++---------- config.py | 8 +++++-- .../upload/gitkeep => data/emoticons/.gitkeep | 0 frontend/vue.config.js | 2 +- main.py | 11 ++++----- 8 files changed, 31 insertions(+), 22 deletions(-) rename frontend/public/upload/gitkeep => data/emoticons/.gitkeep (100%) diff --git a/.dockerignore b/.dockerignore index a9a6e8b..dd66663 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,4 +18,7 @@ README.md # runtime data data/* !data/config.example.ini +!data/emoticons/ +data/emoticons/* +!data/emoticons/.gitkeep log/* diff --git a/.gitignore b/.gitignore index b3fc540..99e4566 100644 --- a/.gitignore +++ b/.gitignore @@ -107,4 +107,7 @@ venv.bak/ .idea/ data/* !data/config.example.ini +!data/emoticons/ +data/emoticons/* +!data/emoticons/.gitkeep log/* diff --git a/README.md b/README.md index f9e4a05..f271b8c 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,9 @@ server { # 如果文件不存在,交给前端路由 try_files $uri $uri/ /index.html; } + location /emoticons { + alias /PATH/TO/BLIVECHAT/data/emoticons; + } # 动态API location /api { proxy_pass http://blivechat; diff --git a/api/main.py b/api/main.py index 4e6f9bd..f0b00f9 100644 --- a/api/main.py +++ b/api/main.py @@ -12,6 +12,9 @@ import update logger = logging.getLogger(__name__) +EMOTICON_UPLOAD_PATH = os.path.join(config.DATA_PATH, 'emoticons') +EMOTICON_BASE_URL = '/emoticons' + class MainHandler(tornado.web.StaticFileHandler): # noqa """为了使用Vue Router的history模式,把不存在的文件请求转发到index.html""" @@ -54,26 +57,22 @@ class UploadEmoticonHandler(api.base.ApiHandler): # noqa raise tornado.web.HTTPError(415) url = await asyncio.get_event_loop().run_in_executor( - None, self._save_file, file.body, self.settings['WEB_ROOT'], self.request.remote_ip + None, self._save_file, file.body, self.request.remote_ip ) self.write({ 'url': url }) @staticmethod - def _save_file(body, web_root, client): + def _save_file(body, client): md5 = hashlib.md5(body).hexdigest() - rel_path = os.path.join('upload', md5 + '.png') - logger.info('client=%s uploaded file, path=%s, size=%d', client, rel_path, len(body)) + filename = md5 + '.png' + path = os.path.join(EMOTICON_UPLOAD_PATH, filename) + logger.info('client=%s uploaded file, path=%s, size=%d', client, path, len(body)) - abs_path = os.path.join(web_root, rel_path) - tmp_path = abs_path + '.tmp' + tmp_path = path + '.tmp' with open(tmp_path, 'wb') as f: f.write(body) - os.replace(tmp_path, abs_path) + os.replace(tmp_path, path) - url = rel_path - if os.path.sep != '/': - url = url.replace(os.path.sep, '/') - url = '/' + url - return url + return f'{EMOTICON_BASE_URL}/{filename}' diff --git a/config.py b/config.py index 0dcaf30..c7a23f9 100644 --- a/config.py +++ b/config.py @@ -6,9 +6,13 @@ from typing import * logger = logging.getLogger(__name__) +BASE_PATH = os.path.dirname(os.path.realpath(__file__)) +WEB_ROOT = os.path.join(BASE_PATH, 'frontend', 'dist') +DATA_PATH = os.path.join(BASE_PATH, 'data') + CONFIG_PATH_LIST = [ - os.path.join('data', 'config.ini'), - os.path.join('data', 'config.example.ini') + os.path.join(DATA_PATH, 'config.ini'), + os.path.join(DATA_PATH, 'config.example.ini') ] _config: Optional['AppConfig'] = None diff --git a/frontend/public/upload/gitkeep b/data/emoticons/.gitkeep similarity index 100% rename from frontend/public/upload/gitkeep rename to data/emoticons/.gitkeep diff --git a/frontend/vue.config.js b/frontend/vue.config.js index 24e883e..a6bb0df 100644 --- a/frontend/vue.config.js +++ b/frontend/vue.config.js @@ -7,7 +7,7 @@ module.exports = { target: API_BASE_URL, ws: true }, - '/upload': { + '/emoticons': { target: API_BASE_URL } } diff --git a/main.py b/main.py index 9917abc..aefb2b7 100644 --- a/main.py +++ b/main.py @@ -19,10 +19,6 @@ import update logger = logging.getLogger(__name__) -BASE_PATH = os.path.dirname(os.path.realpath(__file__)) -WEB_ROOT = os.path.join(BASE_PATH, 'frontend', 'dist') -LOG_FILE_NAME = os.path.join(BASE_PATH, 'log', 'blivechat.log') - routes = [ (r'/api/server_info', api.main.ServerInfoHandler), (r'/api/emoticon', api.main.UploadEmoticonHandler), @@ -31,7 +27,8 @@ routes = [ (r'/api/room_info', api.chat.RoomInfoHandler), (r'/api/avatar_url', api.chat.AvatarHandler), - (r'/(.*)', api.main.MainHandler, {'path': WEB_ROOT, 'default_filename': 'index.html'}) + (rf'{api.main.EMOTICON_BASE_URL}/(.*)', tornado.web.StaticFileHandler, {'path': api.main.EMOTICON_UPLOAD_PATH}), + (r'/(.*)', api.main.MainHandler, {'path': config.WEB_ROOT, 'default_filename': 'index.html'}) ] @@ -58,9 +55,10 @@ def parse_args(): def init_logging(debug): + filename = os.path.join(config.BASE_PATH, 'log', 'blivechat.log') stream_handler = logging.StreamHandler() file_handler = logging.handlers.TimedRotatingFileHandler( - LOG_FILE_NAME, encoding='utf-8', when='midnight', backupCount=7, delay=True + filename, encoding='utf-8', when='midnight', backupCount=7, delay=True ) logging.basicConfig( format='{asctime} {levelname} [{name}]: {message}', @@ -77,7 +75,6 @@ def init_logging(debug): def run_server(host, port, debug): app = tornado.web.Application( routes, - WEB_ROOT=WEB_ROOT, websocket_ping_interval=10, debug=debug, autoreload=False