From db6064330ea57d3ae08ff8c59e225440e03f1d82 Mon Sep 17 00:00:00 2001 From: lan Date: Tue, 28 Feb 2023 18:34:15 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=88=87=E7=89=87=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 22 ++++++++------ templates/index.html | 71 ++++++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/main.py b/main.py index 97ba907..cd34985 100644 --- a/main.py +++ b/main.py @@ -129,6 +129,7 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession = raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) await s.commit() + print(info.type) if info.type != 'text': info.text = f'/select?code={info.code}&token={await get_token(code, ip)}' return { @@ -191,8 +192,9 @@ async def merge_chunks(file_key: str, file_name: str, total_chunks: int): @app.post('/share', dependencies=[Depends(admin_required)], description='分享文件') -async def share(background_tasks: BackgroundTasks, text: str = Form(default=None), - style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(default=None), +async def share(text: str = Form(default=None), size: int = Form(default=0), file_key: str = Form(default=None), + _type: str = Form(default=None), name: str = Form(default='text'), + style: str = Form(default='2'), value: int = Form(default=1), is_file: int = Form(default=True), ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)): if style == '2': if value > settings.MAX_DAYS: @@ -211,14 +213,16 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None else: exp_time = datetime.datetime.now() + datetime.timedelta(days=1) exp_count = -1 - key = uuid.uuid4().hex - if file: - size = await storage.get_size(file) - if size > settings.FILE_SIZE_LIMIT: - raise HTTPException(status_code=400, detail="文件过大") - _text, _type, name = await storage.get_text(file, key), file.content_type, file.filename - background_tasks.add_task(storage.save_file, file, _text) + print(is_file) + if is_file: + key = file_key + # size = await storage.get_size(file) + # if size > settings.FILE_SIZE_LIMIT: + # raise HTTPException(status_code=400, detail="文件过大") + _text, size, name = text, size, name + # background_tasks.add_task(storage.save_file, file, _text) else: + key = uuid.uuid4().hex size, _text, _type, name = len(text), text, 'text', '文本分享' code = await get_code(s) s.add(Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key)) diff --git a/templates/index.html b/templates/index.html index 41dd01b..a0b74eb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -150,17 +150,17 @@ - + 文件 - + 文本 我的文件 - +
存入
@@ -280,9 +280,10 @@ enableUpload: false, uploadData: { style: '2', - type: '1', + type: 'text', value: 1, file: null, + is_file: '1', text: '' }, }; @@ -372,24 +373,48 @@ await this.http('post', `/file/upload/${file_key}/`, formData) }, uploadFile: async function (e) { - this.http('post', '/file/create/').then(async res => { - const file = e.file; - let chunk_index = 0; - const shardSize = 1024 * 1024 * 5; - const {name, size} = file; - const total_chunks = Math.ceil(size / shardSize); - while (chunk_index < total_chunks) { - const start = chunk_index * shardSize - const end = Math.min(start + shardSize, size) - chunk_index += 1; - await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks); - } - this.http('get', `/file/merge/${res.data}/?file_name=${file.name}&total_chunks=${total_chunks}`).then(res => { - console.log(res) - }) - console.log(chunk_index, name, size, total_chunks); - }) - + if (this.checkFile(e.file)) { + this.http('post', '/file/create/').then(async res => { + const file = e.file; + let chunk_index = 0; + const shardSize = 1024 * 1024 * 5; + const {name, size, type} = file; + const total_chunks = Math.ceil(size / shardSize); + while (chunk_index < total_chunks) { + const start = chunk_index * shardSize + const end = Math.min(start + shardSize, size) + chunk_index += 1; + await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks); + } + this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => { + this.http('post', '/share', { + file_key: res.data, + text: text.data, + size: size, + style: this.uploadData.style, + type: type, + name: name, + value: this.uploadData.value, + pwd: this.pwd + }, { + headers: { + 'Content-Type': 'multipart/form-data', + 'pwd': this.pwd + } + }).then(res => { + console.log(res.data) + this.jiFiles.unshift(res.data); + this.jiDrawer = true; + this.uploadData.text = ''; + this.uploadData.file = null; + this.$message({ + message: '上传成功', + type: 'success' + }); + }) + }) + }); + } }, copyText: function (value, style = 1) { if (style === 0) {