Skip to content

Commit 802b8a0

Browse files
author
lan-air
committed
完善文档,以及docker新增文件夹映射
1 parent ef89de6 commit 802b8a0

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

database.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import datetime
32

43
from sqlalchemy import Boolean, Column, Integer, String, DateTime

main.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
@app.on_event('startup')
3131
async def startup():
3232
await init_models()
33-
3433
asyncio.create_task(delete_expire_files())
3534

35+
3636
index_html = open('templates/index.html', 'r', encoding='utf-8').read() \
3737
.replace('{{title}}', settings.TITLE) \
3838
.replace('{{description}}', settings.DESCRIPTION) \
@@ -48,7 +48,7 @@ async def startup():
4848
def delete_file(files):
4949
for file in files:
5050
if file['type'] != 'text':
51-
os.remove(DATA_ROOT / file['text'].lstrip(STATIC_URL+'/'))
51+
os.remove(DATA_ROOT / file['text'].lstrip(STATIC_URL + '/'))
5252

5353

5454
async def delete_expire_files():
@@ -57,12 +57,10 @@ async def delete_expire_files():
5757
query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0))
5858
exps = (await s.execute(query)).scalars().all()
5959
await asyncio.to_thread(delete_file, [{'type': old.type, 'text': old.text} for old in exps])
60-
6160
exps_ids = [exp.id for exp in exps]
6261
query = delete(Codes).where(Codes.id.in_(exps_ids))
6362
await s.execute(query)
6463
await s.commit()
65-
6664
await asyncio.sleep(random.randint(60, 300))
6765

6866

@@ -73,20 +71,16 @@ async def get_code(s: AsyncSession):
7371
return str(code)
7472

7573

76-
def get_file_name(key, ext, file):
74+
def get_file_name(key, ext, file, file_bytes):
7775
now = datetime.datetime.now()
78-
file_bytes = file.file.read()
79-
size = len(file_bytes)
80-
if size > settings.FILE_SIZE_LIMIT:
81-
return size, '', '', ''
8276
path = DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
8377
name = f'{key}.{ext}'
8478
if not path.exists():
8579
path.mkdir(parents=True)
8680
filepath = path / name
8781
with open(filepath, 'wb') as f:
8882
f.write(file_bytes)
89-
return size, f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename
83+
return f"{STATIC_URL}/{filepath.relative_to(DATA_ROOT)}", file.content_type, file.filename
9084

9185

9286
@app.get(f'/{settings.ADMIN_ADDRESS}')
@@ -148,7 +142,7 @@ async def get_file(code: str, s: AsyncSession = Depends(get_session)):
148142
if info.type == 'text':
149143
return {'code': code, 'msg': '查询成功', 'data': info.text}
150144
else:
151-
return FileResponse(DATA_ROOT / info.text.lstrip(STATIC_URL+'/'), filename=info.name)
145+
return FileResponse(DATA_ROOT / info.text.lstrip(STATIC_URL + '/'), filename=info.name)
152146
else:
153147
return {'code': 404, 'msg': '口令不存在'}
154148

@@ -173,7 +167,6 @@ async def index(request: Request, code: str, s: AsyncSession = Depends(get_sessi
173167
await s.commit()
174168
if info.type != 'text':
175169
info.text = f'/select?code={code}'
176-
177170
return {
178171
'code': 200,
179172
'msg': '取件成功,请点击"取"查看',
@@ -200,9 +193,11 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'),
200193
exp_count = -1
201194
key = uuid.uuid4().hex
202195
if file:
203-
size, _text, _type, name = get_file_name(key, file.filename.split('.')[-1], file)
196+
file_bytes = file.file.read()
197+
size = len(file_bytes)
204198
if size > settings.FILE_SIZE_LIMIT:
205199
return {'code': 404, 'msg': '文件过大'}
200+
_text, _type, name = get_file_name(key, file.filename.split('.')[-1], file, file_bytes)
206201
else:
207202
size, _text, _type, name = len(text), text, 'text', '文本分享'
208203
info = Codes(

readme.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,33 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v
6565

6666
## 部署方式
6767

68-
先拉取代码,然后修改main.py文件,将里面写了注释的可以根据需求改一下
68+
为持久化,不管怎么样,先第一步,建一个文件夹,然后再下载代码
69+
70+
```bash
71+
mkdir /opt/FileCodeBox
72+
cd /opt/FileCodeBox
73+
```
74+
75+
新建一个`.env`文件
76+
77+
```bash
78+
vi .env
79+
```
80+
将下列字段内容替换成你自己的
81+
82+
```dotenv
83+
DEBUG=False
84+
DATABASE_URL=sqlite+aiosqlite:///database.db
85+
DATA_ROOT=./static
86+
STATIC_URL=/static
87+
ERROR_COUNT=5
88+
ERROR_MINUTE=10
89+
ADMIN_ADDRESS=admin
90+
FILE_SIZE_LIMIT=10
91+
TITLE=文件快递柜
92+
DESCRIPTION=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件
93+
KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件
94+
```
6995

7096
### 服务端部署
7197

@@ -83,8 +109,12 @@ https://www.yuque.com/lxyo/work/zd0kvzy7fofx6w7v
83109
### Docker部署
84110

85111
```bash
112+
mkdir "/opt/FileCodeBox"
113+
cd "/opt/FileCodeBox"
114+
wget https://github.com/vastsa/FileCodeBox/releases/download/Main/code.zip
115+
unzip code.zip
86116
docker build --file Dockerfile --tag filecodebox .
87-
docker run -d -p 12345:12345 --name filecodebox filecodebox
117+
docker run -d -p 12345:12345 --name filecodebox --volume /opt/FileCodeBox:/app filecodebox
88118
```
89119

90120
## 状态

settings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from starlette.config import Config
22

3-
43
config = Config(".env")
54

65
DEBUG = config('DEBUG', cast=bool, default=False)
@@ -19,10 +18,10 @@
1918

2019
ADMIN_PASSWORD = config('ADMIN_ADDRESS', cast=str, default="admin")
2120

22-
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=1024 * 1024 * 10)
21+
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10) * 1024 * 1024
2322

2423
TITLE = config('TITLE', cast=str, default="文件快递柜")
2524

26-
DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件")
25+
DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件")
2726

28-
KEYWORDS = config('TITLE', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件")
27+
KEYWORDS = config('KEYWORDS', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件")

0 commit comments

Comments
 (0)