2019-06-12 13:55:49 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-02-27 22:05:37 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
import hashlib
|
2022-03-01 00:57:16 +08:00
|
|
|
|
import logging
|
2022-02-27 22:05:37 +08:00
|
|
|
|
import os
|
|
|
|
|
|
2019-06-12 13:55:49 +08:00
|
|
|
|
import tornado.web
|
|
|
|
|
|
2020-02-06 19:51:03 +08:00
|
|
|
|
import api.base
|
|
|
|
|
import config
|
|
|
|
|
import update
|
|
|
|
|
|
2022-03-01 00:57:16 +08:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2019-06-12 13:55:49 +08:00
|
|
|
|
|
2022-02-15 00:18:46 +08:00
|
|
|
|
class MainHandler(tornado.web.StaticFileHandler): # noqa
|
2020-08-18 21:48:33 +08:00
|
|
|
|
"""为了使用Vue Router的history模式,把不存在的文件请求转发到index.html"""
|
|
|
|
|
async def get(self, path, include_body=True):
|
|
|
|
|
try:
|
|
|
|
|
await super().get(path, include_body)
|
|
|
|
|
except tornado.web.HTTPError as e:
|
|
|
|
|
if e.status_code != 404:
|
|
|
|
|
raise
|
|
|
|
|
# 不存在的文件请求转发到index.html,交给前端路由
|
|
|
|
|
await super().get('index.html', include_body)
|
2020-02-06 19:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
2022-02-15 00:18:46 +08:00
|
|
|
|
class ServerInfoHandler(api.base.ApiHandler): # noqa
|
2020-02-06 19:51:03 +08:00
|
|
|
|
async def get(self):
|
|
|
|
|
cfg = config.get_config()
|
|
|
|
|
self.write({
|
|
|
|
|
'version': update.VERSION,
|
|
|
|
|
'config': {
|
2020-08-30 17:46:04 +08:00
|
|
|
|
'enableTranslate': cfg.enable_translate,
|
2022-02-27 22:05:37 +08:00
|
|
|
|
'enableUploadFile': cfg.enable_upload_file,
|
2020-08-30 17:46:04 +08:00
|
|
|
|
'loaderUrl': cfg.loader_url
|
2020-02-06 19:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
2022-02-27 22:05:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UploadEmoticonHandler(api.base.ApiHandler): # noqa
|
|
|
|
|
async def post(self):
|
|
|
|
|
cfg = config.get_config()
|
|
|
|
|
if not cfg.enable_upload_file:
|
|
|
|
|
raise tornado.web.HTTPError(403)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
file = self.request.files['file'][0]
|
|
|
|
|
except LookupError:
|
|
|
|
|
raise tornado.web.MissingArgumentError('file')
|
|
|
|
|
if len(file.body) > 1024 * 1024:
|
|
|
|
|
raise tornado.web.HTTPError(413, 'file is too large, size=%d', len(file.body))
|
|
|
|
|
if not file.content_type.lower().startswith('image/'):
|
|
|
|
|
raise tornado.web.HTTPError(415)
|
|
|
|
|
|
|
|
|
|
url = await asyncio.get_event_loop().run_in_executor(
|
2022-03-01 00:57:16 +08:00
|
|
|
|
None, self._save_file, file.body, self.settings['WEB_ROOT'], self.request.remote_ip
|
2022-02-27 22:05:37 +08:00
|
|
|
|
)
|
|
|
|
|
self.write({
|
|
|
|
|
'url': url
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2022-03-01 00:57:16 +08:00
|
|
|
|
def _save_file(body, web_root, client):
|
2022-02-27 22:05:37 +08:00
|
|
|
|
md5 = hashlib.md5(body).hexdigest()
|
|
|
|
|
rel_path = os.path.join('upload', md5 + '.png')
|
2022-03-01 00:57:16 +08:00
|
|
|
|
logger.info('client=%s uploaded file, path=%s, size=%d', client, rel_path, len(body))
|
|
|
|
|
|
2022-02-27 22:05:37 +08:00
|
|
|
|
abs_path = os.path.join(web_root, rel_path)
|
|
|
|
|
tmp_path = abs_path + '.tmp'
|
|
|
|
|
with open(tmp_path, 'wb') as f:
|
|
|
|
|
f.write(body)
|
|
|
|
|
os.replace(tmp_path, abs_path)
|
|
|
|
|
|
|
|
|
|
url = rel_path
|
|
|
|
|
if os.path.sep != '/':
|
|
|
|
|
url = url.replace(os.path.sep, '/')
|
|
|
|
|
url = '/' + url
|
|
|
|
|
return url
|