feat: complete uploads
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { mediaService } from '@/api/mediaService';
|
import { mediaService } from '@/api/mediaService';
|
||||||
|
import Badge from 'primevue/badge';
|
||||||
import Button from 'primevue/button';
|
import Button from 'primevue/button';
|
||||||
import Toast from 'primevue/toast';
|
import Card from 'primevue/card';
|
||||||
import { useToast } from 'primevue/usetoast';
|
import { useToast } from 'primevue/usetoast';
|
||||||
import { computed, onMounted, ref } from 'vue';
|
import { computed, onMounted, ref } from 'vue';
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router';
|
||||||
@@ -76,8 +77,64 @@ const getOssToken = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add pending uploads state
|
||||||
|
const pendingFiles = ref([]);
|
||||||
|
const uploadQueue = ref([]);
|
||||||
|
const currentUploadIndex = ref(-1);
|
||||||
|
|
||||||
|
// Add sequential upload management
|
||||||
|
const addToUploadQueue = (files) => {
|
||||||
|
const newFiles = Array.from(files).map(file => ({
|
||||||
|
id: Date.now() + Math.random(),
|
||||||
|
file,
|
||||||
|
progress: 0,
|
||||||
|
status: 'pending',
|
||||||
|
name: file.name,
|
||||||
|
size: file.size,
|
||||||
|
type: file.type,
|
||||||
|
uploadTime: new Date().toISOString()
|
||||||
|
}));
|
||||||
|
uploadQueue.value.push(...newFiles);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleFiles = async (files) => {
|
||||||
|
addToUploadQueue(files);
|
||||||
|
if (!isUploading.value) {
|
||||||
|
processNextUpload();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const processNextUpload = async () => {
|
||||||
|
if (uploadQueue.value.length === 0) {
|
||||||
|
currentUploadIndex.value = -1;
|
||||||
|
isUploading.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextFile = uploadQueue.value[0];
|
||||||
|
currentUploadIndex.value = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await uploadFile(nextFile.file);
|
||||||
|
uploadQueue.value.shift();
|
||||||
|
addToHistory(nextFile.file, 'completed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to upload file:', nextFile.file.name, error);
|
||||||
|
uploadQueue.value.shift();
|
||||||
|
addToHistory(nextFile.file, 'error');
|
||||||
|
}
|
||||||
|
|
||||||
|
processNextUpload();
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateQueueItemProgress = (index, progress) => {
|
||||||
|
if (uploadQueue.value[index]) {
|
||||||
|
uploadQueue.value[index].progress = progress;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Modify uploadFile function
|
||||||
const uploadFile = async (file) => {
|
const uploadFile = async (file) => {
|
||||||
const historyItem = addToHistory(file);
|
|
||||||
try {
|
try {
|
||||||
currentFile.value = file;
|
currentFile.value = file;
|
||||||
isUploading.value = true;
|
isUploading.value = true;
|
||||||
@@ -106,7 +163,7 @@ const uploadFile = async (file) => {
|
|||||||
if (e.lengthComputable) {
|
if (e.lengthComputable) {
|
||||||
const progress = (e.loaded / e.total) * 100;
|
const progress = (e.loaded / e.total) * 100;
|
||||||
uploadProgress.value = progress;
|
uploadProgress.value = progress;
|
||||||
updateHistoryItem(historyItem.id, { progress });
|
updateQueueItemProgress(currentUploadIndex.value, progress);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -124,25 +181,10 @@ const uploadFile = async (file) => {
|
|||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
});
|
});
|
||||||
|
|
||||||
updateHistoryItem(historyItem.id, { status: 'completed', progress: 100 });
|
|
||||||
toast.add({ severity: 'success', summary: '成功', detail: '文件上传成功', life: 3000 });
|
toast.add({ severity: 'success', summary: '成功', detail: '文件上传成功', life: 3000 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
updateHistoryItem(historyItem.id, { status: 'error', error: error.message });
|
|
||||||
toast.add({ severity: 'error', summary: '错误', detail: '文件上传失败', life: 3000 });
|
toast.add({ severity: 'error', summary: '错误', detail: '文件上传失败', life: 3000 });
|
||||||
throw error;
|
throw error;
|
||||||
} finally {
|
|
||||||
isUploading.value = false;
|
|
||||||
currentFile.value = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleFiles = async (files) => {
|
|
||||||
for (const file of files) {
|
|
||||||
try {
|
|
||||||
await uploadFile(file);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to upload file:', file.name, error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -180,11 +222,10 @@ const onFileInputChange = (e) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const goBack = () => {
|
const goBack = () => {
|
||||||
router.push('/media');
|
router.push('/medias');
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
getOssToken();
|
|
||||||
loadUploadHistory();
|
loadUploadHistory();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@@ -199,14 +240,22 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card p-6">
|
<div class="grid grid-cols-2 gap-6">
|
||||||
<input ref="fileInput" type="file" multiple accept="image/*,video/*,audio/*,.pdf,.doc,.docx" class="hidden"
|
<!-- Upload Area - Left Column -->
|
||||||
@change="onFileInputChange">
|
<Card>
|
||||||
|
<template #header>
|
||||||
|
<div class="flex justify-between items-center bg-gray-50 p-4 rounded-t-lg">
|
||||||
|
<h3 class="text-lg font-medium">上传</h3>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template #content>
|
||||||
|
<input ref="fileInput" type="file" multiple accept="image/*,video/*,audio/*,.pdf,.doc,.docx"
|
||||||
|
class="hidden" @change="onFileInputChange">
|
||||||
|
|
||||||
<div ref="dropZone" @drop="onDrop" @dragover="onDragOver" @dragleave="onDragLeave" @click="onClick"
|
<div ref="dropZone" @drop="onDrop" @dragover="onDragOver" @dragleave="onDragLeave" @click="onClick"
|
||||||
class="border-2 border-dashed border-gray-300 rounded-lg transition-all duration-200 hover:border-blue-500 hover:bg-blue-50 cursor-pointer">
|
class="border-2 border-dashed border-gray-300 rounded-lg transition-all duration-200 hover:border-blue-500 hover:bg-blue-50 cursor-pointer">
|
||||||
<div class="flex flex-col items-center justify-center p-8">
|
<div class="flex flex-col items-center justify-center p-8">
|
||||||
<i class="pi pi-cloud-upload text-5xl text-gray-500 mb-4"></i>
|
<i class="pi pi-cloud-upload text-6xl! text-gray-500 mb-4"></i>
|
||||||
<p class="text-gray-600 text-center mb-2">拖拽文件到此处或点击上传</p>
|
<p class="text-gray-600 text-center mb-2">拖拽文件到此处或点击上传</p>
|
||||||
<p class="text-gray-400 text-sm text-center">支持: MP4, JPG, PNG, GIF, MP4, PDF, DOC</p>
|
<p class="text-gray-400 text-sm text-center">支持: MP4, JPG, PNG, GIF, MP4, PDF, DOC</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -221,16 +270,37 @@ onMounted(() => {
|
|||||||
<div class="text-sm mt-2">{{ Math.round(uploadProgress) }}%</div>
|
<div class="text-sm mt-2">{{ Math.round(uploadProgress) }}%</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Upload History -->
|
<!-- Upload Queue -->
|
||||||
<div class="mt-6">
|
<div v-if="uploadQueue.length > (isUploading.value ? 1 : 0)" class="mt-6">
|
||||||
<div class="flex justify-between items-center mb-4">
|
<h3 class="text-lg font-medium mb-4">等待上传 ({{ uploadQueue.length - (isUploading.value ? 1 : 0)
|
||||||
|
}})
|
||||||
|
</h3>
|
||||||
|
<div class="space-y-4">
|
||||||
|
<div v-for="item in uploadQueue.slice(isUploading.value ? 1 : 0)" :key="item.id"
|
||||||
|
class="p-4 bg-gray-50 rounded-lg">
|
||||||
|
<div class="flex justify-between items-center mb-2">
|
||||||
|
<span class="text-sm font-medium">{{ item.name }}</span>
|
||||||
|
<Badge value="等待中" severity="warning" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<!-- Upload History - Right Column -->
|
||||||
|
<Card>
|
||||||
|
<template #header>
|
||||||
|
<div class="flex justify-between items-center bg-gray-50 p-4 rounded-t-lg">
|
||||||
<h3 class="text-lg font-medium">上传历史</h3>
|
<h3 class="text-lg font-medium">上传历史</h3>
|
||||||
<Button v-if="completedUploads.length" icon="pi pi-trash" text @click="clearCompleted"
|
<Button v-if="completedUploads.length" icon="pi pi-trash" text @click="clearCompleted"
|
||||||
label="清除已完成" />
|
label="清除已完成" />
|
||||||
</div>
|
</div>
|
||||||
|
</template>
|
||||||
<div class="space-y-4">
|
<template #content>
|
||||||
<div v-for="item in uploadHistory" :key="item.id" class="p-4 bg-gray-50 rounded-lg">
|
<div class="space-y-4 max-h-[calc(100vh-200px)] overflow-y-auto">
|
||||||
|
<div v-for="item in uploadHistory.filter(i => i.status !== 'uploading')" :key="item.id"
|
||||||
|
class="p-4 bg-gray-50 rounded-lg">
|
||||||
<div class="flex justify-between items-center mb-2">
|
<div class="flex justify-between items-center mb-2">
|
||||||
<span class="text-sm font-medium">{{ item.name }}</span>
|
<span class="text-sm font-medium">{{ item.name }}</span>
|
||||||
<Badge :value="item.status === 'completed' ? '已完成' :
|
<Badge :value="item.status === 'completed' ? '已完成' :
|
||||||
@@ -246,22 +316,13 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</template>
|
||||||
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.p-fileupload {
|
|
||||||
border: 2px dashed #cbd5e0;
|
|
||||||
transition: all 0.3s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.p-fileupload:hover {
|
|
||||||
border-color: #4299e1;
|
|
||||||
background-color: rgba(66, 153, 225, 0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
.custom-upload :deep(.p-fileupload-buttonbar) {
|
.custom-upload :deep(.p-fileupload-buttonbar) {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user