From fd26bace790e4bff08f24c7f6ae380423ff2cd5e Mon Sep 17 00:00:00 2001 From: lan Date: Sat, 7 Jan 2023 12:09:25 +0800 Subject: [PATCH] update:encoding convert --- database.py | 4 +- utf8togbk.py => encoding_convert.py | 23 ++++++----- main.py | 4 +- settings.py | 10 ++--- templates/admin.html | 62 +---------------------------- templates/index.html | 6 +-- utils.py | 2 + 7 files changed, 24 insertions(+), 87 deletions(-) rename utf8togbk.py => encoding_convert.py (52%) diff --git a/database.py b/database.py index 41322ed..53c9b9c 100644 --- a/database.py +++ b/database.py @@ -1,5 +1,5 @@ import datetime -from sqlalchemy import Boolean, Column, Integer, String, DateTime, JSON +from sqlalchemy import Boolean, Column, Integer, String, DateTime, JSON, Text from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy.ext.asyncio.session import AsyncSession @@ -35,7 +35,7 @@ class Codes(Base): name = Column(String(500)) size = Column(Integer) type = Column(String(20)) - text = Column(String(500)) + text = Column(Text) used = Column(Boolean, default=False) count = Column(Integer, default=-1) use_time = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now) diff --git a/utf8togbk.py b/encoding_convert.py similarity index 52% rename from utf8togbk.py rename to encoding_convert.py index fae1584..97a6a60 100644 --- a/utf8togbk.py +++ b/encoding_convert.py @@ -1,21 +1,22 @@ +# 获取文件编码 import chardet -# 将UTF-8编码的文件转换为GBK编码 -def utf8togbk(file): - with open(file, 'r', encoding='utf-8') as f: - lines = f.readlines() - with open(file, 'w', encoding='gbk') as f: - f.writelines(lines) - - -# 获取文件编码 def get_encoding(file): with open(file, 'rb') as f: return chardet.detect(f.read())['encoding'] +# 将其他编码的文件转换为UTF-8编码 +def other2utf8(file, encoding): + with open(file, 'r', encoding=encoding) as f: + lines = f.readlines() + with open(file, 'w', encoding='utf-8') as f: + f.writelines(lines) + + # 获取文件编码,如果为utf-8编码,就转换为gbk编码 def convert_encoding(file): - if get_encoding(file) == 'utf-8': - utf8togbk(file) + encoding = get_encoding(file) + if encoding != 'utf-8': + other2utf8(file, encoding) diff --git a/main.py b/main.py index e11a96d..4e920f4 100644 --- a/main.py +++ b/main.py @@ -212,7 +212,7 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None await s.commit() upload_ip_limit.add_ip(ip) return { - 'detail': '分享成功,请点击取件码按钮查看上传列表', + 'detail': '分享成功,请点击我的文件按钮查看上传列表', 'data': {'code': code, 'key': key, 'name': name} } @@ -220,4 +220,4 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None if __name__ == '__main__': import uvicorn - uvicorn.run('main:app', host='0.0.0.0', port=settings.PORT, reload=settings.DEBUG) + uvicorn.run('main:app', host=':', port=settings.PORT, reload=settings.DEBUG) diff --git a/settings.py b/settings.py index bd6fcf4..7f1739a 100644 --- a/settings.py +++ b/settings.py @@ -1,19 +1,17 @@ from starlette.config import Config -from utf8togbk import convert_encoding import os import shutil +import encoding_convert + # 配置文件.env # 请将.env移动至data目录,方便docker部署 - # 判断根目录下是否存在.env文件 if os.path.exists('.env'): # 将文件复制到data目录 shutil.copy('.env', 'data/.env') +if os.path.exists('data/.env'): # 获取data目录下的.env文件编码,如果为utf-8编码,就转换为gbk编码 - convert_encoding('./data/.env') -elif os.path.exists('data/.env'): - # 获取data目录下的.env文件编码,如果为utf-8编码,就转换为gbk编码 - convert_encoding('./data/.env') + encoding_convert.convert_encoding('./data/.env') config = Config("data/.env") # 是否开启DEBUG模式 diff --git a/templates/admin.html b/templates/admin.html index 4afe3b3..ccd006a 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -73,58 +73,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
@@ -162,15 +111,6 @@ pwd: '', activeIndex: '2', config: { - // enable_upload: true, - // max_days: 7, - // error_count: 3, - // error_minute: 10, - // upload_count: 10, - // upload_minute: 10, - // delete_expire_files_interval: 10, - // admin_password: 'admin', - // file_size_limit: 10, banners: [] }, files: [], diff --git a/templates/index.html b/templates/index.html index e83a302..51c6b05 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,11 +11,7 @@ - - - - - +