update:切片上传

This commit is contained in:
lan
2023-02-28 18:34:15 +08:00
parent d8de60a1db
commit db6064330e
2 changed files with 61 additions and 32 deletions
+13 -9
View File
@@ -129,6 +129,7 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession =
raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人") raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人")
await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1)) await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1))
await s.commit() await s.commit()
print(info.type)
if info.type != 'text': if info.type != 'text':
info.text = f'/select?code={info.code}&token={await get_token(code, ip)}' info.text = f'/select?code={info.code}&token={await get_token(code, ip)}'
return { 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='分享文件') @app.post('/share', dependencies=[Depends(admin_required)], description='分享文件')
async def share(background_tasks: BackgroundTasks, text: str = Form(default=None), async def share(text: str = Form(default=None), size: int = Form(default=0), file_key: str = Form(default=None),
style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(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)): ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)):
if style == '2': if style == '2':
if value > settings.MAX_DAYS: if value > settings.MAX_DAYS:
@@ -211,14 +213,16 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None
else: else:
exp_time = datetime.datetime.now() + datetime.timedelta(days=1) exp_time = datetime.datetime.now() + datetime.timedelta(days=1)
exp_count = -1 exp_count = -1
key = uuid.uuid4().hex print(is_file)
if file: if is_file:
size = await storage.get_size(file) key = file_key
if size > settings.FILE_SIZE_LIMIT: # size = await storage.get_size(file)
raise HTTPException(status_code=400, detail="文件过大") # if size > settings.FILE_SIZE_LIMIT:
_text, _type, name = await storage.get_text(file, key), file.content_type, file.filename # raise HTTPException(status_code=400, detail="文件过大")
background_tasks.add_task(storage.save_file, file, _text) _text, size, name = text, size, name
# background_tasks.add_task(storage.save_file, file, _text)
else: else:
key = uuid.uuid4().hex
size, _text, _type, name = len(text), text, 'text', '文本分享' size, _text, _type, name = len(text), text, 'text', '文本分享'
code = await get_code(s) 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)) s.add(Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key))
+35 -10
View File
@@ -150,17 +150,17 @@
<el-button v-else slot="append" disabled></el-button> <el-button v-else slot="append" disabled></el-button>
</el-input> </el-input>
</el-tooltip> </el-tooltip>
<el-radio-group style="margin-left: 18px" v-model="uploadData.type"> <el-radio-group style="margin-left: 18px" v-model="uploadData.is_file">
<el-radio label="1"> <el-radio label="1">
文件 文件
</el-radio> </el-radio>
<el-radio label="2"> <el-radio label="0">
文本 文本
</el-radio> </el-radio>
</el-radio-group> </el-radio-group>
</el-row> </el-row>
<el-upload <el-upload
v-if="uploadData.type === '1'" v-if="uploadData.is_file === '1'"
drag drag
action="/share" action="/share"
multiple multiple
@@ -193,7 +193,7 @@
<div class="el-icon-takeaway-box"></div> <div class="el-icon-takeaway-box"></div>
我的文件 我的文件
</el-button> </el-button>
<el-button round v-if="uploadData.type === '2'" @click="toUploadData"> <el-button round v-if="uploadData.is_file === '0'" @click="toUploadData">
<div class="el-icon-upload2"></div> <div class="el-icon-upload2"></div>
存入 存入
</el-button> </el-button>
@@ -280,9 +280,10 @@
enableUpload: false, enableUpload: false,
uploadData: { uploadData: {
style: '2', style: '2',
type: '1', type: 'text',
value: 1, value: 1,
file: null, file: null,
is_file: '1',
text: '' text: ''
}, },
}; };
@@ -372,11 +373,12 @@
await this.http('post', `/file/upload/${file_key}/`, formData) await this.http('post', `/file/upload/${file_key}/`, formData)
}, },
uploadFile: async function (e) { uploadFile: async function (e) {
if (this.checkFile(e.file)) {
this.http('post', '/file/create/').then(async res => { this.http('post', '/file/create/').then(async res => {
const file = e.file; const file = e.file;
let chunk_index = 0; let chunk_index = 0;
const shardSize = 1024 * 1024 * 5; const shardSize = 1024 * 1024 * 5;
const {name, size} = file; const {name, size, type} = file;
const total_chunks = Math.ceil(size / shardSize); const total_chunks = Math.ceil(size / shardSize);
while (chunk_index < total_chunks) { while (chunk_index < total_chunks) {
const start = chunk_index * shardSize const start = chunk_index * shardSize
@@ -384,12 +386,35 @@
chunk_index += 1; chunk_index += 1;
await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks); 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 => { this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
console.log(res) 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'
});
}) })
console.log(chunk_index, name, size, total_chunks);
}) })
});
}
}, },
copyText: function (value, style = 1) { copyText: function (value, style = 1) {
if (style === 0) { if (style === 0) {