Skip to content

Commit

Permalink
feat:新增分享服务器本地文件,传到/opt/filecodebox/local里面
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan committed Sep 24, 2024
1 parent 8359895 commit d988d5b
Show file tree
Hide file tree
Showing 56 changed files with 752 additions and 436 deletions.
92 changes: 90 additions & 2 deletions apps/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
# @File : views.py
# @Software: PyCharm
import math
import os
import time

from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Form
from pydantic import BaseModel

from apps.admin.depends import admin_required
from apps.admin.pydantics import IDData
from apps.base.models import FileCodes, KeyValue
from apps.base.utils import get_expire_info, get_file_path_name
from core.response import APIResponse
from core.settings import settings
from core.settings import settings, data_root
from core.storage import FileStorageInterface, storages

admin_api = APIRouter(
Expand Down Expand Up @@ -91,3 +95,87 @@ async def file_download(id: int):
return APIResponse(detail=file_code.text)
else:
return await file_storage.get_file_response(file_code)


class LocalFileClass:
def __init__(self, file):
self.file = file
self.path = data_root / 'local' / file
self.ctime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(self.path)))
self.size = os.path.getsize(self.path)

async def read(self):
return open(self.path, 'rb')

async def write(self, data):
with open(self.path, 'w') as f:
f.write(data)

async def delete(self):
os.remove(self.path)

async def exists(self):
return os.path.exists(self.path)


@admin_api.get('/local/lists', dependencies=[Depends(admin_required)])
async def get_local_lists():
files = []
for file in os.listdir(data_root / 'local'):
files.append(LocalFileClass(file))
return APIResponse(detail=files)


class DeleteItem(BaseModel):
filename: str


@admin_api.delete('/local/delete', dependencies=[Depends(admin_required)])
async def delete_local_file(item: DeleteItem):
file = LocalFileClass(item.filename)
if await file.exists():
await file.delete()
return APIResponse(detail='删除成功')
return APIResponse(code=404, detail='文件不存在')


class ShareItem(BaseModel):
expire_value: int
expire_style: str = 'day'
filename: str


class File:
def __init__(self, file):
self.file = file


@admin_api.post('/local/share', dependencies=[Depends(admin_required)])
async def share_local_file(item: ShareItem):
file = LocalFileClass(item.filename)
if not await file.exists():
return APIResponse(code=404, detail='文件不存在')
text = File(await file.read())
expired_at, expired_count, used_count, code = await get_expire_info(item.expire_value, item.expire_style)
# 获取文件路径和名称
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(item)
# 保存文件
file_storage: FileStorageInterface = storages[settings.file_storage]()
await file_storage.save_file(text, save_path)
# 创建一个新的FileCodes实例
await FileCodes.create(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=uuid_file_name,
file_path=path,
size=file.size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)
# 返回API响应
return APIResponse(detail={
'code': code,
'name': file.file,
})
1 change: 1 addition & 0 deletions fcb-fronted/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module 'vue' {
ElContainer: typeof import('element-plus/es')['ElContainer']
ElDialog: typeof import('element-plus/es')['ElDialog']
ElDrawer: typeof import('element-plus/es')['ElDrawer']
ElEmpty: typeof import('element-plus/es')['ElEmpty']
ElForm: typeof import('element-plus/es')['ElForm']
ElFormItem: typeof import('element-plus/es')['ElFormItem']
ElHeader: typeof import('element-plus/es')['ElHeader']
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fcb-fronted/dist/assets/AdminView-CL7mmOac.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion fcb-fronted/dist/assets/AdminView-DCdW5fL2.js

This file was deleted.

1 change: 1 addition & 0 deletions fcb-fronted/dist/assets/CardTools-4F6WeaAR.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions fcb-fronted/dist/assets/FileView-DedH_wkX.js

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions fcb-fronted/dist/assets/FileView-e5CSuy2N.js

This file was deleted.

1 change: 1 addition & 0 deletions fcb-fronted/dist/assets/HomeView-4TAzKov5.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion fcb-fronted/dist/assets/HomeView-A33iSQLm.js

This file was deleted.

1 change: 1 addition & 0 deletions fcb-fronted/dist/assets/LocalView-D4c33CZV.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fcb-fronted/dist/assets/LocalView-DvcLks3c.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion fcb-fronted/dist/assets/SendView-Bv3Zu6X5.js

This file was deleted.

Loading

0 comments on commit d988d5b

Please sign in to comment.