update:切片上传
This commit is contained in:
@@ -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))
|
||||
|
||||
+35
-10
@@ -150,17 +150,17 @@
|
||||
<el-button v-else slot="append" disabled>天</el-button>
|
||||
</el-input>
|
||||
</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>
|
||||
<el-radio label="2">
|
||||
<el-radio label="0">
|
||||
文本
|
||||
</el-radio>
|
||||
</el-radio-group>
|
||||
</el-row>
|
||||
<el-upload
|
||||
v-if="uploadData.type === '1'"
|
||||
v-if="uploadData.is_file === '1'"
|
||||
drag
|
||||
action="/share"
|
||||
multiple
|
||||
@@ -193,7 +193,7 @@
|
||||
<div class="el-icon-takeaway-box"></div>
|
||||
我的文件
|
||||
</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>
|
||||
存入
|
||||
</el-button>
|
||||
@@ -280,9 +280,10 @@
|
||||
enableUpload: false,
|
||||
uploadData: {
|
||||
style: '2',
|
||||
type: '1',
|
||||
type: 'text',
|
||||
value: 1,
|
||||
file: null,
|
||||
is_file: '1',
|
||||
text: ''
|
||||
},
|
||||
};
|
||||
@@ -372,11 +373,12 @@
|
||||
await this.http('post', `/file/upload/${file_key}/`, formData)
|
||||
},
|
||||
uploadFile: async function (e) {
|
||||
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} = file;
|
||||
const {name, size, type} = file;
|
||||
const total_chunks = Math.ceil(size / shardSize);
|
||||
while (chunk_index < total_chunks) {
|
||||
const start = chunk_index * shardSize
|
||||
@@ -384,12 +386,35 @@
|
||||
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)
|
||||
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'
|
||||
});
|
||||
})
|
||||
console.log(chunk_index, name, size, total_chunks);
|
||||
})
|
||||
|
||||
});
|
||||
}
|
||||
},
|
||||
copyText: function (value, style = 1) {
|
||||
if (style === 0) {
|
||||
|
||||
Reference in New Issue
Block a user