test/custom-admin-ui #3
@@ -1,4 +1,3 @@
|
|||||||
import asyncio
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ app.mount(STATIC_URL, StaticFiles(directory=DATA_ROOT), name="static")
|
|||||||
@app.on_event('startup')
|
@app.on_event('startup')
|
||||||
async def startup():
|
async def startup():
|
||||||
await init_models()
|
await init_models()
|
||||||
|
|
||||||
asyncio.create_task(delete_expire_files())
|
asyncio.create_task(delete_expire_files())
|
||||||
|
|
||||||
|
|
||||||
index_html = open('templates/index.html', 'r', encoding='utf-8').read() \
|
index_html = open('templates/index.html', 'r', encoding='utf-8').read() \
|
||||||
.replace('{{title}}', settings.TITLE) \
|
.replace('{{title}}', settings.TITLE) \
|
||||||
.replace('{{description}}', settings.DESCRIPTION) \
|
.replace('{{description}}', settings.DESCRIPTION) \
|
||||||
@@ -57,12 +57,10 @@ async def delete_expire_files():
|
|||||||
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
|
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
|
||||||
exps = (await s.execute(query)).scalars().all()
|
exps = (await s.execute(query)).scalars().all()
|
||||||
await asyncio.to_thread(delete_file, [{'type': old.type, 'text': old.text} for old in exps])
|
await asyncio.to_thread(delete_file, [{'type': old.type, 'text': old.text} for old in exps])
|
||||||
|
|
||||||
exps_ids = [exp.id for exp in exps]
|
exps_ids = [exp.id for exp in exps]
|
||||||
query = delete(Codes).where(Codes.id.in_(exps_ids))
|
query = delete(Codes).where(Codes.id.in_(exps_ids))
|
||||||
await s.execute(query)
|
await s.execute(query)
|
||||||
await s.commit()
|
await s.commit()
|
||||||
|
|
||||||
await asyncio.sleep(random.randint(60, 300))
|
await asyncio.sleep(random.randint(60, 300))
|
||||||
|
|
||||||
|
|
||||||
@@ -73,12 +71,8 @@ async def get_code(s: AsyncSession):
|
|||||||
return str(code)
|
return str(code)
|
||||||
|
|
||||||
|
|
||||||
def get_file_name(key, ext, file):
|
def get_file_name(key, ext, file, file_bytes):
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
file_bytes = file.file.read()
|
|
||||||
size = len(file_bytes)
|
|
||||||
if size > settings.FILE_SIZE_LIMIT:
|
|
||||||
return size, '', '', ''
|
|
||||||
path = DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
|
path = DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
|
||||||
name = f'{key}.{ext}'
|
name = f'{key}.{ext}'
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
@@ -86,7 +80,7 @@ def get_file_name(key, ext, file):
|
|||||||
filepath = path / name
|
filepath = path / name
|
||||||
with open(filepath, 'wb') as f:
|
with open(filepath, 'wb') as f:
|
||||||
f.write(file_bytes)
|
f.write(file_bytes)
|
||||||
return size, f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename
|
return f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename
|
||||||
|
|
||||||
|
|
||||||
@app.get(f'/{settings.ADMIN_ADDRESS}')
|
@app.get(f'/{settings.ADMIN_ADDRESS}')
|
||||||
@@ -173,7 +167,6 @@ async def index(request: Request, code: str, s: AsyncSession = Depends(get_sessi
|
|||||||
await s.commit()
|
await s.commit()
|
||||||
if info.type != 'text':
|
if info.type != 'text':
|
||||||
info.text = f'/select?code={code}'
|
info.text = f'/select?code={code}'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'code': 200,
|
'code': 200,
|
||||||
'msg': '取件成功,请点击"取"查看',
|
'msg': '取件成功,请点击"取"查看',
|
||||||
@@ -200,9 +193,11 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'),
|
|||||||
exp_count = -1
|
exp_count = -1
|
||||||
key = uuid.uuid4().hex
|
key = uuid.uuid4().hex
|
||||||
if file:
|
if file:
|
||||||
size, _text, _type, name = get_file_name(key, file.filename.split('.')[-1], file)
|
file_bytes = file.file.read()
|
||||||
|
size = len(file_bytes)
|
||||||
if size > settings.FILE_SIZE_LIMIT:
|
if size > settings.FILE_SIZE_LIMIT:
|
||||||
return {'code': 404, 'msg': '文件过大'}
|
return {'code': 404, 'msg': '文件过大'}
|
||||||
|
_text, _type, name = get_file_name(key, file.filename.split('.')[-1], file, file_bytes)
|
||||||
else:
|
else:
|
||||||
size, _text, _type, name = len(text), text, 'text', '文本分享'
|
size, _text, _type, name = len(text), text, 'text', '文本分享'
|
||||||
info = Codes(
|
info = Codes(
|
||||||
|
|||||||
@@ -65,7 +65,33 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v
|
|||||||
|
|
||||||
## 部署方式
|
## 部署方式
|
||||||
|
|
||||||
先拉取代码,然后修改main.py文件,将里面写了注释的可以根据需求改一下
|
为持久化,不管怎么样,先第一步,建一个文件夹,然后再下载代码
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir /opt/FileCodeBox
|
||||||
|
cd /opt/FileCodeBox
|
||||||
|
```
|
||||||
|
|
||||||
|
新建一个`.env`文件
|
||||||
|
|
||||||
|
```bash
|
||||||
|
vi .env
|
||||||
|
```
|
||||||
|
将下列字段内容替换成你自己的
|
||||||
|
|
||||||
|
```dotenv
|
||||||
|
DEBUG=False
|
||||||
|
DATABASE_URL=sqlite+aiosqlite:///database.db
|
||||||
|
DATA_ROOT=./static
|
||||||
|
STATIC_URL=/static
|
||||||
|
ERROR_COUNT=5
|
||||||
|
ERROR_MINUTE=10
|
||||||
|
ADMIN_ADDRESS=admin
|
||||||
|
FILE_SIZE_LIMIT=10
|
||||||
|
TITLE=文件快递柜
|
||||||
|
DESCRIPTION=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件
|
||||||
|
KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件
|
||||||
|
```
|
||||||
|
|
||||||
### 服务端部署
|
### 服务端部署
|
||||||
|
|
||||||
@@ -83,8 +109,12 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v
|
|||||||
### Docker部署
|
### Docker部署
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
mkdir "/opt/FileCodeBox"
|
||||||
|
cd "/opt/FileCodeBox"
|
||||||
|
wget https://github.com/vastsa/FileCodeBox/releases/download/Main/code.zip
|
||||||
|
unzip code.zip
|
||||||
docker build --file Dockerfile --tag filecodebox .
|
docker build --file Dockerfile --tag filecodebox .
|
||||||
docker run -d -p 12345:12345 --name filecodebox filecodebox
|
docker run -d -p 12345:12345 --name filecodebox --volume /opt/FileCodeBox:/app filecodebox
|
||||||
```
|
```
|
||||||
|
|
||||||
## 状态
|
## 状态
|
||||||
|
|||||||
+3
-4
@@ -1,6 +1,5 @@
|
|||||||
from starlette.config import Config
|
from starlette.config import Config
|
||||||
|
|
||||||
|
|
||||||
config = Config(".env")
|
config = Config(".env")
|
||||||
|
|
||||||
DEBUG = config('DEBUG', cast=bool, default=False)
|
DEBUG = config('DEBUG', cast=bool, default=False)
|
||||||
@@ -19,10 +18,10 @@ ADMIN_ADDRESS = config('ADMIN_ADDRESS', cast=str, default="admin")
|
|||||||
|
|
||||||
ADMIN_PASSWORD = config('ADMIN_ADDRESS', cast=str, default="admin")
|
ADMIN_PASSWORD = config('ADMIN_ADDRESS', cast=str, default="admin")
|
||||||
|
|
||||||
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=1024 * 1024 * 10)
|
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10) * 1024 * 1024
|
||||||
|
|
||||||
TITLE = config('TITLE', cast=str, default="文件快递柜")
|
TITLE = config('TITLE', cast=str, default="文件快递柜")
|
||||||
|
|
||||||
DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件")
|
DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件")
|
||||||
|
|
||||||
KEYWORDS = config('TITLE', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件")
|
KEYWORDS = config('KEYWORDS', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件")
|
||||||
|
|||||||
Reference in New Issue
Block a user