add:后台分享本地文件
This commit is contained in:
@@ -6,7 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import HTMLResponse, FileResponse
|
from starlette.responses import HTMLResponse, FileResponse
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
|
import os, shutil
|
||||||
from core.utils import error_ip_limit, upload_ip_limit, get_code, storage
|
from core.utils import error_ip_limit, upload_ip_limit, get_code, storage
|
||||||
from core.depends import admin_required
|
from core.depends import admin_required
|
||||||
from settings import settings
|
from settings import settings
|
||||||
@@ -18,7 +18,7 @@ from core.database import init_models, Options, Codes, get_session
|
|||||||
app = FastAPI(debug=settings.DEBUG, docs_url=None, redoc_url=None)
|
app = FastAPI(debug=settings.DEBUG, docs_url=None, redoc_url=None)
|
||||||
|
|
||||||
# 数据存储文件夹
|
# 数据存储文件夹
|
||||||
DATA_ROOT = Path(settings.DATA_ROOT)
|
DATA_ROOT = Path(settings.DATA_ROOT + '/files')
|
||||||
if not DATA_ROOT.exists():
|
if not DATA_ROOT.exists():
|
||||||
DATA_ROOT.mkdir(parents=True)
|
DATA_ROOT.mkdir(parents=True)
|
||||||
|
|
||||||
@@ -54,6 +54,39 @@ async def admin():
|
|||||||
return HTMLResponse(admin_html)
|
return HTMLResponse(admin_html)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(f'/{settings.ADMIN_ADDRESS}/files', dependencies=[Depends(admin_required)])
|
||||||
|
async def get_files():
|
||||||
|
files = []
|
||||||
|
for i, j, k in os.walk(f'{DATA_ROOT}'):
|
||||||
|
files.extend([{'name': _.split('/')[-1], 'path': i} for _ in k])
|
||||||
|
return {'data': files}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(f'/{settings.ADMIN_ADDRESS}/files', dependencies=[Depends(admin_required)])
|
||||||
|
async def share_file(name=Form(...), path=File(...), s: AsyncSession = Depends(get_session)):
|
||||||
|
file_path = path + f'/{name}'
|
||||||
|
key = uuid.uuid4().hex
|
||||||
|
code = await get_code(s)
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
path = f"{settings.DATA_ROOT}/upload/{now.year}/{now.month}/{now.day}/"
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
text = f"{path}/{f'{key}{name}'}"
|
||||||
|
exp_time = datetime.datetime.now() + datetime.timedelta(days=1)
|
||||||
|
s.add(Codes(code=code, text=text.replace(f'{settings.DATA_ROOT}/', ''), type='file', name=name, count=-1,
|
||||||
|
exp_time=exp_time, key=key))
|
||||||
|
await s.commit()
|
||||||
|
shutil.move(file_path, text)
|
||||||
|
return {
|
||||||
|
'detail': '分享成功!请在24小时内使用分享使用!',
|
||||||
|
'data': {
|
||||||
|
'code': code,
|
||||||
|
'text': text,
|
||||||
|
'name': name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.post(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='查询数据库列表')
|
@app.post(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='查询数据库列表')
|
||||||
async def admin_post(page: int = Form(default=1), size: int = Form(default=10), s: AsyncSession = Depends(get_session)):
|
async def admin_post(page: int = Form(default=1), size: int = Form(default=10), s: AsyncSession = Depends(get_session)):
|
||||||
infos = (await s.execute(select(Codes).offset((page - 1) * size).limit(size))).scalars().all()
|
infos = (await s.execute(select(Codes).offset((page - 1) * size).limit(size))).scalars().all()
|
||||||
@@ -75,17 +108,6 @@ async def admin_post(page: int = Form(default=1), size: int = Form(default=10),
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
|
|
||||||
@app.patch(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='修改数据库数据')
|
|
||||||
async def admin_patch(request: Request, s: AsyncSession = Depends(get_session)):
|
|
||||||
data = await request.json()
|
|
||||||
data.pop('INSTALL')
|
|
||||||
for key, value in data.items():
|
|
||||||
await s.execute(update(Options).where(Options.key == key).values(value=value))
|
|
||||||
await settings.update(key, value)
|
|
||||||
await s.commit()
|
|
||||||
return {'detail': '修改成功'}
|
|
||||||
|
|
||||||
|
|
||||||
@app.delete(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='删除数据库记录')
|
@app.delete(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='删除数据库记录')
|
||||||
async def admin_delete(code: str, s: AsyncSession = Depends(get_session)):
|
async def admin_delete(code: str, s: AsyncSession = Depends(get_session)):
|
||||||
# 找到相应记录
|
# 找到相应记录
|
||||||
@@ -113,10 +135,20 @@ async def config(s: AsyncSession = Depends(get_session)):
|
|||||||
{'key': 'WEBSITE', 'name': '网站设置'},
|
{'key': 'WEBSITE', 'name': '网站设置'},
|
||||||
{'key': 'SHARE', 'name': '分享设置'},
|
{'key': 'SHARE', 'name': '分享设置'},
|
||||||
{'key': 'BANNERS', 'name': 'Banner'},
|
{'key': 'BANNERS', 'name': 'Banner'},
|
||||||
{'key': 'WEBSITE', 'name': '网站设置'},
|
|
||||||
]}
|
]}
|
||||||
|
|
||||||
|
|
||||||
|
@app.patch(f'/{settings.ADMIN_ADDRESS}', dependencies=[Depends(admin_required)], description='修改数据库数据')
|
||||||
|
async def admin_patch(request: Request, s: AsyncSession = Depends(get_session)):
|
||||||
|
data = await request.json()
|
||||||
|
data.pop('INSTALL')
|
||||||
|
for key, value in data.items():
|
||||||
|
await s.execute(update(Options).where(Options.key == key).values(value=value))
|
||||||
|
await settings.update(key, value)
|
||||||
|
await s.commit()
|
||||||
|
return {'detail': '修改成功'}
|
||||||
|
|
||||||
|
|
||||||
@app.get('/')
|
@app.get('/')
|
||||||
async def index():
|
async def index():
|
||||||
return HTMLResponse(index_html)
|
return HTMLResponse(index_html)
|
||||||
|
|||||||
@@ -101,6 +101,24 @@
|
|||||||
>
|
>
|
||||||
</el-pagination>
|
</el-pagination>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
<el-col v-if="activeIndex === '3'">
|
||||||
|
<el-card style="height: 80vh;overflow: scroll;margin-bottom: 1rem">
|
||||||
|
<el-empty v-if="locals.length === 0" description="暂时还没有文件"></el-empty>
|
||||||
|
<el-card v-for="file in locals" :key="file.code">
|
||||||
|
<el-row>
|
||||||
|
<el-col :span="20">
|
||||||
|
<div style="cursor: pointer;text-align: left">
|
||||||
|
<div>文件名:${ file.name }</div>
|
||||||
|
<div>路径:${ file.path }</div>
|
||||||
|
</div>
|
||||||
|
</el-col>
|
||||||
|
<el-col :span="4">
|
||||||
|
<el-button type="primary" @click="shareFile(file)">分享</el-button>
|
||||||
|
</el-col>
|
||||||
|
</el-row>
|
||||||
|
</el-card>
|
||||||
|
</el-card>
|
||||||
|
</el-col>
|
||||||
<el-col v-if="activeIndex === '4'">
|
<el-col v-if="activeIndex === '4'">
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-col :span="5" style="height: 88vh;overflow: scroll" :gutter="10">
|
<el-col :span="5" style="height: 88vh;overflow: scroll" :gutter="10">
|
||||||
@@ -215,6 +233,7 @@
|
|||||||
<el-button slot="append" type="primary" @click="loginAdmin">登录</el-button>
|
<el-button slot="append" type="primary" @click="loginAdmin">登录</el-button>
|
||||||
</el-input>
|
</el-input>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
@@ -234,6 +253,7 @@
|
|||||||
menus: [],
|
menus: [],
|
||||||
updateTab: 'INSTALL',
|
updateTab: 'INSTALL',
|
||||||
files: [],
|
files: [],
|
||||||
|
locals: [],
|
||||||
paginate: {
|
paginate: {
|
||||||
page: 1,
|
page: 1,
|
||||||
size: 10,
|
size: 10,
|
||||||
@@ -255,6 +275,7 @@
|
|||||||
this.menus = res.data.menus;
|
this.menus = res.data.menus;
|
||||||
});
|
});
|
||||||
this.loginAdmin();
|
this.loginAdmin();
|
||||||
|
this.getLocalFiles();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@@ -317,6 +338,30 @@
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
getLocalFiles: function () {
|
||||||
|
axios.get(window.location.href + '/files', {
|
||||||
|
'headers': {
|
||||||
|
'pwd': this.pwd,
|
||||||
|
'Content-Type': 'multipart/form-data'
|
||||||
|
}
|
||||||
|
}).then(res => {
|
||||||
|
this.locals = res.data.data;
|
||||||
|
})
|
||||||
|
},
|
||||||
|
shareFile: function (file) {
|
||||||
|
axios.post(window.location.href + '/files', file, {
|
||||||
|
'headers': {
|
||||||
|
'pwd': this.pwd,
|
||||||
|
'Content-Type': 'multipart/form-data'
|
||||||
|
}
|
||||||
|
}).then(res => {
|
||||||
|
this.getLocalFiles()
|
||||||
|
this.$alert(`请在24小时内使用:${res.data.data.code}`, '分享成功', {
|
||||||
|
confirmButtonText: '确定',
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user