Skip to content

Commit 24200a5

Browse files
committed
update:切片上传
1 parent aaaef09 commit 24200a5

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

main.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession =
129129
raise HTTPException(status_code=404, detail="取件码已失效,请联系寄件人")
130130
await s.execute(update(Codes).where(Codes.id == info.id).values(count=info.count - 1))
131131
await s.commit()
132+
print(info.type)
132133
if info.type != 'text':
133134
info.text = f'/select?code={info.code}&token={await get_token(code, ip)}'
134135
return {
@@ -191,8 +192,9 @@ async def merge_chunks(file_key: str, file_name: str, total_chunks: int):
191192

192193

193194
@app.post('/share', dependencies=[Depends(admin_required)], description='分享文件')
194-
async def share(background_tasks: BackgroundTasks, text: str = Form(default=None),
195-
style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(default=None),
195+
async def share(text: str = Form(default=None), size: int = Form(default=0), file_key: str = Form(default=None),
196+
_type: str = Form(default=None), name: str = Form(default='text'),
197+
style: str = Form(default='2'), value: int = Form(default=1), is_file: int = Form(default=True),
196198
ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)):
197199
if style == '2':
198200
if value > settings.MAX_DAYS:
@@ -211,14 +213,16 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None
211213
else:
212214
exp_time = datetime.datetime.now() + datetime.timedelta(days=1)
213215
exp_count = -1
214-
key = uuid.uuid4().hex
215-
if file:
216-
size = await storage.get_size(file)
217-
if size > settings.FILE_SIZE_LIMIT:
218-
raise HTTPException(status_code=400, detail="文件过大")
219-
_text, _type, name = await storage.get_text(file, key), file.content_type, file.filename
220-
background_tasks.add_task(storage.save_file, file, _text)
216+
print(is_file)
217+
if is_file:
218+
key = file_key
219+
# size = await storage.get_size(file)
220+
# if size > settings.FILE_SIZE_LIMIT:
221+
# raise HTTPException(status_code=400, detail="文件过大")
222+
_text, size, name = text, size, name
223+
# background_tasks.add_task(storage.save_file, file, _text)
221224
else:
225+
key = uuid.uuid4().hex
222226
size, _text, _type, name = len(text), text, 'text', '文本分享'
223227
code = await get_code(s)
224228
s.add(Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key))

templates/index.html

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@
150150
<el-button v-else slot="append" disabled></el-button>
151151
</el-input>
152152
</el-tooltip>
153-
<el-radio-group style="margin-left: 18px" v-model="uploadData.type">
153+
<el-radio-group style="margin-left: 18px" v-model="uploadData.is_file">
154154
<el-radio label="1">
155155
文件
156156
</el-radio>
157-
<el-radio label="2">
157+
<el-radio label="0">
158158
文本
159159
</el-radio>
160160
</el-radio-group>
161161
</el-row>
162162
<el-upload
163-
v-if="uploadData.type === '1'"
163+
v-if="uploadData.is_file === '1'"
164164
drag
165165
action="/share"
166166
multiple
@@ -193,7 +193,7 @@
193193
<div class="el-icon-takeaway-box"></div>
194194
我的文件
195195
</el-button>
196-
<el-button round v-if="uploadData.type === '2'" @click="toUploadData">
196+
<el-button round v-if="uploadData.is_file === '0'" @click="toUploadData">
197197
<div class="el-icon-upload2"></div>
198198
存入
199199
</el-button>
@@ -280,9 +280,10 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
280280
enableUpload: false,
281281
uploadData: {
282282
style: '2',
283-
type: '1',
283+
type: 'text',
284284
value: 1,
285285
file: null,
286+
is_file: '1',
286287
text: ''
287288
},
288289
};
@@ -372,24 +373,48 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
372373
await this.http('post', `/file/upload/${file_key}/`, formData)
373374
},
374375
uploadFile: async function (e) {
375-
this.http('post', '/file/create/').then(async res => {
376-
const file = e.file;
377-
let chunk_index = 0;
378-
const shardSize = 1024 * 1024 * 5;
379-
const {name, size} = file;
380-
const total_chunks = Math.ceil(size / shardSize);
381-
while (chunk_index < total_chunks) {
382-
const start = chunk_index * shardSize
383-
const end = Math.min(start + shardSize, size)
384-
chunk_index += 1;
385-
await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
386-
}
387-
this.http('get', `/file/merge/${res.data}/?file_name=${file.name}&total_chunks=${total_chunks}`).then(res => {
388-
console.log(res)
389-
})
390-
console.log(chunk_index, name, size, total_chunks);
391-
})
392-
376+
if (this.checkFile(e.file)) {
377+
this.http('post', '/file/create/').then(async res => {
378+
const file = e.file;
379+
let chunk_index = 0;
380+
const shardSize = 1024 * 1024 * 5;
381+
const {name, size, type} = file;
382+
const total_chunks = Math.ceil(size / shardSize);
383+
while (chunk_index < total_chunks) {
384+
const start = chunk_index * shardSize
385+
const end = Math.min(start + shardSize, size)
386+
chunk_index += 1;
387+
await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
388+
}
389+
this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
390+
this.http('post', '/share', {
391+
file_key: res.data,
392+
text: text.data,
393+
size: size,
394+
style: this.uploadData.style,
395+
type: type,
396+
name: name,
397+
value: this.uploadData.value,
398+
pwd: this.pwd
399+
}, {
400+
headers: {
401+
'Content-Type': 'multipart/form-data',
402+
'pwd': this.pwd
403+
}
404+
}).then(res => {
405+
console.log(res.data)
406+
this.jiFiles.unshift(res.data);
407+
this.jiDrawer = true;
408+
this.uploadData.text = '';
409+
this.uploadData.file = null;
410+
this.$message({
411+
message: '上传成功',
412+
type: 'success'
413+
});
414+
})
415+
})
416+
});
417+
}
393418
},
394419
copyText: function (value, style = 1) {
395420
if (style === 0) {

0 commit comments

Comments
 (0)