diff --git a/app.properties b/app.properties deleted file mode 100644 index ac82217cd542fb3157bbe8829d14f30d92fb35d7..0000000000000000000000000000000000000000 --- a/app.properties +++ /dev/null @@ -1,42 +0,0 @@ -# sanic -host = 0.0.0.0 -port = 8005 -debug = True -access_log = True -workers = 3 -sanic_key = test-library -auto_reload = False -jwt_secret_key = test-lib-jwt-secret-key -main_domain = http://test-lib:8005/ - -# log -log_mode = False -log_bytes = 20*1024*1024 -log_backup = 5 -formatter = %(asctime)s [%(process)d] [%(levelname)s] %(message)s -formatter_access = %(asctime)s [%(process)d] [%(levelname)s] [%(host)s]: %(request)s %(message)s %(status)d %(byte)d - -# daily -db_url = mysql+aiomysql://root:tonedbadmin@121.196.236.71:3306/test-lib -drop_all = False -create_all = False -pool_size = 10 -over_size = 10 -recycle = 3600 - -# redis -redis_url = redis://:toneredisadmin@121.196.236.71:6379/10 - -# tone prod -tone_host = http://tone:7001/ -tone_token = xxxx -tone_user_name = xxxxx - -# font -oss_url=http://0.0.0.0:8005 - -# tone-storage -storage_host = 127.0.0.1 -storage_sftp_port = 22 -storage_user = tonestorage -storage_password = 123456 diff --git a/app/__init__.py b/app/__init__.py index dbc9c41a5d6abeac2aa0d85cd92d66aaf1ebad07..795a21fa74b6bc2ad6ec031ab9b7b08145824224 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,7 +3,6 @@ import os.path from app.conf import conf from app.db import db from app.log import log -from app.oss import alg_oss from app.redis import redis from app.setting import STATIC_ROOT, STATICFILES_DIRS, OUTLINE_PATH diff --git a/app/conf.py b/app/conf.py index db8644ffa93fdef1ce3f93a61dbfd68a9eb8e7b9..b7d3b73f5a58cca6f51ab62b5510a20dd2a9eb8d 100644 --- a/app/conf.py +++ b/app/conf.py @@ -11,7 +11,10 @@ class SanicConf(object): def init_app(self, app, env=None, group=None): self.config = None - data = self.get_conf('app.properties') + conf_filename = 'app.properties' + data = dict() + if os.path.exists(conf_filename): + data = self.get_conf(conf_filename) self.update_environ(data, os.environ) app.config.update(data) self.config = app.config diff --git a/app/oss.py b/app/oss.py deleted file mode 100644 index f8052efd6a6824472932b42051a696f17747dbb7..0000000000000000000000000000000000000000 --- a/app/oss.py +++ /dev/null @@ -1,77 +0,0 @@ -import oss2 -from oss2.exceptions import NoSuchKey - - -class OSSClient(object): - LINK_VALID_TIME = 7200 - READABLE_FILE_LIST = ['log', 'json', 'txt', 'text', 'sh', 'yaml', 'time', 'output', 'task', 'fs', 'cvs'] - - def __init__(self): - pass - - def init_app(self, app): - self.access_id = None - self.access_key = None - self.endpoint = None - self.bucket_name = None - - @app.listener('before_server_start') - async def redis_conn(app, loop): - self.access_id = app.config['ALG_OSS_ACCESS_KEY'] - self.access_key = app.config['ALG_OSS_ACCESS_SECRET'] - self.bucket_name = app.config['ALG_OSS_BUCKET'] - self.endpoint = app.config['ALG_OSS_ENDPOINT'] - auth = oss2.Auth(self.access_id, self.access_key) - self.bucket = oss2.Bucket(auth, self.endpoint, self.bucket_name) - - def file_exists(self, file_name): - return self.bucket.object_exists(file_name) - - def put_file(self, file_name): - with open(file_name, 'rb') as file_handle: - return self.bucket.put_object(file_name, file_handle) - - def put_object_from_file(self, object_name, local_file): - return self.bucket.put_object_from_file(object_name, local_file) - - def put_object_from_bytes(self, object_name, file_bytes): - return self.bucket.put_object(object_name, file_bytes) - - def get_object(self, object_name, params=None, headers=None): - try: - return self.bucket.get_object(object_name, params=params, headers=headers) - except NoSuchKey: - return None - - def get_object_to_file(self, object_name, local_file): - try: - return self.bucket.get_object_to_file(object_name, local_file) - except NoSuchKey: - return None - - def get_sign_url(self, object_name, expires=7200): - object_name = object_name.strip('/') - if self.bucket.object_exists(object_name): - sign_url = self.bucket.sign_url('GET', object_name, expires) - if isinstance(sign_url, str): - sign_url = sign_url.replace('http://', 'https://') - sign_url = sign_url.replace('%2F', '/') - return sign_url - else: - raise RuntimeError('object:%s not exist in oss bucket:%s' % (object_name, self.bucket)) - - def update_file_content_type(self, object_name, content_type='text/plain'): - object_name = object_name.strip('/') - result = self.bucket.update_object_meta(object_name, {'Content-Type': content_type}) - return result.status == 200 - - def get_file_content_type(self, file): - file_headers = self.bucket.head_object(file) - return file_headers.headers.get('Content-Type') - - def remove_object(self, objects, params=None, headers=None): - result = self.bucket.delete_object(objects, params, headers) - return result.status == 204 - - -alg_oss = OSSClient() diff --git a/common/tone/tone_request.py b/common/tone/tone_request.py index e17d1f9a6e5ddea090379ee16e28de5f83ad441f..7bf394f87706fb4343e540d6c2b9be48c938b13e 100644 --- a/common/tone/tone_request.py +++ b/common/tone/tone_request.py @@ -9,7 +9,7 @@ from common.http import http_request async def get_res_from_tone(req_type, req_api, req_params): tone_url = conf.config['TONE_HOST'] + req_api - token = conf.config['TONE_USER_NAME'] + '|' + conf.config['TONE_TOKEN'] + '|' + str(time.time()) + token = conf.config['TONE_USER_NAME'] + '|' + str(conf.config['TONE_TOKEN']) + '|' + str(time.time()) signature = base64.b64encode(token.encode('utf-8')).decode('utf-8') req_data = { 'username': conf.config['TONE_USER_NAME'], diff --git a/common/tools.py b/common/tools.py index a861a770b9e3d99adf8a4248308316365df501d9..58109081e19644c4a267b543773223b8987fd753 100644 --- a/common/tools.py +++ b/common/tools.py @@ -30,6 +30,10 @@ def string_toDatetime(st): return datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S") +def string_toShortDatetime(st): + return datetime.datetime.strptime(st, "%Y-%m-%d") + + def string_toTimestamp(st): return time.mktime(time.strptime(st, "%Y-%m-%d %H:%M:%S")) diff --git a/common/utils/sftp_client.py b/common/utils/sftp_client.py new file mode 100644 index 0000000000000000000000000000000000000000..50ad67e3347187bb0714afbfd6104676957ec5b1 --- /dev/null +++ b/common/utils/sftp_client.py @@ -0,0 +1,48 @@ +import paramiko +import os +import uuid +from app.conf import conf +from app.log import log + + +async def upload_static_file(uploaded_file): + # SSH连接信息 + host = conf.config['TONE_STORAGE_HOST'] + domain = conf.config['TONE_STORAGE_DOMAIN'] + port = conf.config['TONE_STORAGE_SFTP_PORT'] + proxy_port = conf.config['TONE_STORAGE_PROXY_PORT'] + username = conf.config['TONE_STORAGE_USER'] + password = conf.config['TONE_STORAGE_PASSWORD'] + try: + # 生成一个随机且唯一的文件名 + file_uuid = str(uuid.uuid4()) + file_extension = os.path.splitext(uploaded_file.name)[1] # 获取文件扩展名 + random_filename = f"{file_uuid}{file_extension}" + temp_file_path = '/tmp/' + random_filename # 指定临时保存路径(包含随机文件名) + remote_path = '/testlib/' + random_filename + # 保存上传的文件到本地临时位置 + with open(temp_file_path, 'wb') as f: + f.write(uploaded_file.body) + # 创建SSH客户端 + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + # 连接SFTP服务器 + ssh.connect(host, port, username=username, password=password) + # 创建SFTP对象 + sftp = ssh.open_sftp() + # 检查目录是否存在 + try: + sftp.stat('/testlib') + except FileNotFoundError: + sftp.mkdir('/testlib', mode=0o755) # 创建目录 + # 将临时文件上传到SFTP服务器 + sftp.put(temp_file_path, remote_path) + # 删除本地临时文件(可选) + os.remove(temp_file_path) + # 关闭SFTP和SSH连接 + sftp.close() + ssh.close() + return 'http://' + domain + ':' + proxy_port + remote_path + except Exception as e: + log.logger.error(f'sftp upload file failed! error:{e}') + return '' diff --git a/dist/index.html b/dist/index.html index 9e8888eeb971909f745ebb2ee6545f6412e2280f..0553e18cd30df119778aab877ee29905b7397602 100644 --- a/dist/index.html +++ b/dist/index.html @@ -8,10 +8,10 @@ - +
- +