diff --git a/main.py b/main.py index 153be65..1172008 100644 --- a/main.py +++ b/main.py @@ -127,12 +127,32 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) await s.commit() if info.type != 'text': - info.text = f'/select?code={code}' - return { - 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', - 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} - } + if settings.STORAGE_ENGINE == 'filesystem': + info.text = f'/select?code={code}' + return { + 'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除', + 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} + } + elif settings.STORAGE_ENGINE == 'aliyunsystem': + info.text = await storage.get_filepath(info.text) + return { + 'detail': f'取件成功,链接将在{settings.ACCESSTIME}秒后失效', + 'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code} + } +@app.post('/adminDownloadFile',dependencies=[Depends(admin_required)], description='管理员获取资源链接') +async def admindownloadfile(filetext: str, s: AsyncSession = Depends(get_session)): + if storage.STORAGE_ENGINE == 'aliyunsystem': + filetext = await storage.get_filepath(filetext) + return { + 'detail': f'获取文件链接成功,链接将在{settings.ACCESSTIME}秒后失效', + 'fileURL': filetext + } + elif storage.STORAGE_ENGINE == 'filesystem': + return { + 'detail': f'获取文件链接成功', + 'fileURL':filetext + } @app.get('/banner') async def banner(request: Request, s: AsyncSession = Depends(get_session)): @@ -176,7 +196,10 @@ async def get_file(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession # 如果是文件,返回文件 else: filepath = await storage.get_filepath(info.text) - return FileResponse(filepath, filename=info.name) + if settings.STORAGE_ENGINE == 'filesystem': + return FileResponse(filepath, filename=info.name) + else: + return {'detail': '查询成功', 'data': filepath} @app.post('/share', dependencies=[Depends(admin_required)], description='分享文件') @@ -206,6 +229,8 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None background_tasks.add_task(storage.save_file, file, _text) else: size, _text, _type, name = len(text), text, 'text', '文本分享' + if settings.STORAGE_ENGINE == 'aliyunsystem': + _text = f"https://{settings.BUCKET_NAME}.{settings.OSS_ENDPOINT}/"+_text info = Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key) s.add(info) await s.commit() diff --git a/readme.md b/readme.md index 2a41df0..8a856dc 100644 --- a/readme.md +++ b/readme.md @@ -21,6 +21,7 @@ - [x] 匿名分享:无需注册,无需登录 - [x] 管理面板:查看所有文件,删除文件 - [x] 一键部署:docker一键部署 +- [x] 多种存储方式:阿里云OSS、本地文件流 ## 部署方式 @@ -160,6 +161,15 @@ DESCRIPTION=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享 KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件 # 存储引擎 STORAGE_ENGINE=filesystem +# 如果使用阿里云OSS服务的话需要额外创建如下参数: +# 阿里云账号AccessKey +KeyId=阿里云账号AccessKey +# 阿里云账号AccessKeySecret +KeySecret=阿里云账号AccessKeySecret +# 阿里云OSS Bucket的地域节点 +OSS_ENDPOINT=阿里云OSS Bucket的地域节点 +# 阿里云OSS Bucket的BucketName +BUCKET_NAME=阿里云OSS Bucket的BucketName ``` ## 接口文档 diff --git a/readme_en.md b/readme_en.md index 2889504..9721b09 100644 --- a/readme_en.md +++ b/readme_en.md @@ -22,6 +22,7 @@ - [x] anonymous sharing: no registration, no login - [x] management Panel: View all files and delete them - [x] one-click deployment: docker one-click deployment +- [x] A variety of storage methods : Aliyun OSS、 local file flow ## Deployment method @@ -175,6 +176,15 @@ DESCRIPTION=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享 KEYWORDS=FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件,图片,视频,音频,压缩包等文件 # 存储引擎 STORAGE_ENGINE=filesystem +# 如果使用阿里云OSS服务的话需要额外创建如下参数: +# 阿里云账号AccessKey +KeyId=阿里云账号AccessKey +# 阿里云账号AccessKeySecret +KeySecret=阿里云账号AccessKeySecret +# 阿里云OSS Bucket的地域节点 +OSS_ENDPOINT=阿里云OSS Bucket的地域节点 +# 阿里云OSS Bucket的BucketName +BUCKET_NAME=阿里云OSS Bucket的BucketName ``` ## Status diff --git a/requirements.txt b/requirements.txt index 1f117ba..97b3b72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ SQLAlchemy==1.4.44 python-multipart==0.0.5 uvicorn==0.15.0 greenlet==2.0.1 -starlette~=0.22.0 \ No newline at end of file +starlette~=0.22.0 +oss2==2.16.0 \ No newline at end of file diff --git a/settings.py b/settings.py index 7a29893..8788f9b 100644 --- a/settings.py +++ b/settings.py @@ -48,5 +48,16 @@ TITLE = config('TITLE', cast=str, default="文件快递柜") DESCRIPTION = config('DESCRIPTION', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件") # 网站关键词 KEYWORDS = config('KEYWORDS', cast=str, default="FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件等文件") -# 存储引擎 +# 存储引擎:['aliyunsystem','filesystem'] STORAGE_ENGINE = config('STORAGE_ENGINE', cast=str, default="filesystem") +# 如果使用阿里云OSS的话需要创建如下参数 +# 阿里云账号AccessKey +KeyId = config('KeyId', cast=str, default="阿里云账号AccessKey") +# 阿里云账号AccessKeySecret +KeySecret = config('KeySecret', cast=str, default="阿里云账号AccessKeySecret") +# 阿里云OSS Bucket的地域节点 +OSS_ENDPOINT = config('BUCKET_URL', cast=str, default="阿里云OSS Bucket的地域节点") +# 阿里云OSS Bucket的BucketName +BUCKET_NAME = config('BUCKET_NAME', cast=str, default="阿里云OSS Bucket的BucketName") +# 访问文件的读取时长(s) +ACCESSTIME = config('ACCESSTIME', cast=int, default=60) diff --git a/storage.py b/storage.py index c9c587e..caf0978 100644 --- a/storage.py +++ b/storage.py @@ -3,16 +3,69 @@ import asyncio from datetime import datetime from pathlib import Path from typing import BinaryIO - from fastapi import UploadFile - import settings +import oss2 + + +class AliyunFileStore: + def __init__(self): + auth = oss2.Auth(settings.KeyId, settings.KeySecret) + self.bucket = oss2.Bucket(auth, settings.OSS_ENDPOINT, settings.BUCKET_NAME) + + def upload_file(self, upload_filepath, remote_filepath): + self.bucket.put_object_from_file(remote_filepath, upload_filepath) + + async def get_text(self, file: UploadFile, key: str): + ext = file.filename.split('.')[-1] + now = datetime.now() + path = f"FileCodeBox/upload/{now.year}/{now.month}/{now.day}" + text = f"{path}/{f'{key}.{ext}'}" + return text + + async def get_filepath(self, text: str): + text = text.strip(f"https://{settings.BUCKET_NAME}.{settings.OSS_ENDPOINT}/") + url = self.bucket.sign_url('GET', text, settings.ACCESSTIME, slash_safe=True) + return url + + @staticmethod + async def get_size(file: UploadFile): + f = file.file + f.seek(0, os.SEEK_END) + size = f.tell() + f.seek(0, os.SEEK_SET) + return size + + @staticmethod + def _save(filepath, file: BinaryIO): + with open(filepath, 'wb') as f: + chunk_size = 256 * 1024 + chunk = file.read(chunk_size) + while chunk: + f.write(chunk) + chunk = file.read(chunk_size) + + async def save_file(self, file: UploadFile, remote_filepath: str): + now = int(datetime.now().timestamp()) + upload_filepath = settings.DATA_ROOT+str(now) + await asyncio.to_thread(self._save, upload_filepath, file.file) + self.upload_file(upload_filepath,remote_filepath) + await asyncio.to_thread(os.remove,upload_filepath) + + async def delete_files(self, texts): + tasks = [self.delete_file(text) for text in texts] + await asyncio.gather(*tasks) + + async def delete_file(self, text: str): + text = text.strip(f"https://{settings.BUCKET_NAME}.{settings.OSS_ENDPOINT}/") + self.bucket.delete_object(text) class FileSystemStorage: - DATA_ROOT = Path(settings.DATA_ROOT) - STATIC_URL = settings.STATIC_URL - NAME = "filesystem" + def __init__(self): + self.DATA_ROOT = Path(settings.DATA_ROOT) + self.STATIC_URL = settings.STATIC_URL + self.NAME = "filesystem" async def get_filepath(self, text: str): return self.DATA_ROOT / text.lstrip(self.STATIC_URL + '/') @@ -68,5 +121,6 @@ class FileSystemStorage: STORAGE_ENGINE = { - "filesystem": FileSystemStorage + "filesystem": FileSystemStorage, + "aliyunsystem": AliyunFileStore } diff --git a/utils.py b/utils.py index 3e00464..6290c40 100644 --- a/utils.py +++ b/utils.py @@ -1,8 +1,6 @@ import datetime import random import asyncio - -import chardet from sqlalchemy import or_, select, delete from sqlalchemy.ext.asyncio.session import AsyncSession import settings