自定义表情存储目录改到data里

This commit is contained in:
John Smith 2022-03-01 22:13:25 +08:00
parent 9f8f238426
commit 618164d77d
8 changed files with 31 additions and 22 deletions

View File

@ -18,4 +18,7 @@ README.md
# runtime data
data/*
!data/config.example.ini
!data/emoticons/
data/emoticons/*
!data/emoticons/.gitkeep
log/*

3
.gitignore vendored
View File

@ -107,4 +107,7 @@ venv.bak/
.idea/
data/*
!data/config.example.ini
!data/emoticons/
data/emoticons/*
!data/emoticons/.gitkeep
log/*

View File

@ -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;

View File

@ -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}'

View File

@ -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

View File

@ -7,7 +7,7 @@ module.exports = {
target: API_BASE_URL,
ws: true
},
'/upload': {
'/emoticons': {
target: API_BASE_URL
}
}

11
main.py
View File

@ -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