Skip to content

Commit f653942

Browse files
author
Puremeo
committed
4
1 parent 175ca20 commit f653942

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

functions/upload/chunkUpload.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ export async function initializeChunkedUpload(context) {
2121
return createResponse('Error: Missing initialization parameters', { status: 400 });
2222
}
2323

24+
// 检查分块数量限制(200个分块 × 20MB = 4GB)
25+
if (totalChunks > 200) {
26+
const maxSizeGB = (200 * 20 / 1024).toFixed(1);
27+
const fileSizeMB = (totalChunks * 20).toFixed(2);
28+
console.log(`[分块上传初始化] 文件过大: ${fileSizeMB}MB (${totalChunks}个分块),超过最大限制 ${maxSizeGB}GB`);
29+
return createResponse(`Error: File too large (exceeds ${maxSizeGB}GB limit, current: ${totalChunks} chunks, estimated size: ${fileSizeMB}MB)`, { status: 413 });
30+
}
31+
32+
// 记录初始化信息用于调试
33+
const fileSizeMB = (totalChunks * 20).toFixed(2);
34+
console.log(`[分块上传初始化] 文件: ${originalFileName}, 预计大小: ${fileSizeMB}MB, 分块数: ${totalChunks}`);
35+
2436
// 生成唯一的 uploadId
2537
const timestamp = Date.now();
2638
const random = Math.random().toString(36).slice(2, 11);

js/folder-upload.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@
9696
e.preventDefault();
9797
e.stopPropagation();
9898

99-
// 移除拖拽样式
100-
if (e.currentTarget) {
101-
e.currentTarget.classList.remove('drag-over');
99+
// 移除拖拽样式(安全地处理)
100+
try {
101+
const target = e.currentTarget || e.target;
102+
if (target && target.classList && typeof target.classList.remove === 'function') {
103+
target.classList.remove('drag-over');
104+
}
105+
} catch (error) {
106+
// 忽略样式移除错误
102107
}
103108
}
104109

@@ -161,9 +166,15 @@
161166
e.stopPropagation();
162167
e.stopImmediatePropagation();
163168

164-
// 移除拖拽样式
165-
if (e.currentTarget) {
166-
e.currentTarget.classList.remove('drag-over');
169+
// 移除拖拽样式(安全地处理)
170+
try {
171+
const target = e.currentTarget || e.target;
172+
if (target && target.classList && typeof target.classList.remove === 'function') {
173+
target.classList.remove('drag-over');
174+
}
175+
} catch (error) {
176+
// 忽略样式移除错误
177+
console.warn('[文件夹上传] 移除拖拽样式时出错:', error);
167178
}
168179

169180
// 处理文件夹上传
@@ -193,9 +204,15 @@
193204
e.stopPropagation();
194205
e.stopImmediatePropagation();
195206

196-
// 移除拖拽样式
197-
if (e.currentTarget) {
198-
e.currentTarget.classList.remove('drag-over');
207+
// 移除拖拽样式(安全地处理)
208+
try {
209+
const target = e.currentTarget || e.target;
210+
if (target && target.classList && typeof target.classList.remove === 'function') {
211+
target.classList.remove('drag-over');
212+
}
213+
} catch (error) {
214+
// 忽略样式移除错误
215+
console.warn('[文件夹上传] 移除拖拽样式时出错:', error);
199216
}
200217

201218
// 使用我们的上传逻辑处理大文件
@@ -553,19 +570,28 @@
553570
const CHUNK_SIZE = 20 * 1024 * 1024; // 20MB
554571
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
555572
const fileSizeMB = (file.size / 1024 / 1024).toFixed(2);
573+
const fileSizeGB = (file.size / 1024 / 1024 / 1024).toFixed(2);
556574
const baseUrl = window.location.origin;
557575

558576
// 调试信息:检查文件大小和分块数量
559-
console.log(`[文件夹上传] 文件: ${relativePath}, 大小: ${fileSizeMB}MB, 分块数: ${totalChunks}`);
577+
console.group(`[文件夹上传] 开始分块上传`);
578+
console.log(`文件: ${relativePath}`);
579+
console.log(`大小: ${fileSizeMB}MB (${fileSizeGB}GB)`);
580+
console.log(`分块数: ${totalChunks} (每块 ${CHUNK_SIZE / 1024 / 1024}MB)`);
581+
console.log(`限制: 最大 200 个分块 (4GB)`);
560582

561583
// 前端检查:如果分块数超过200,提前提示
562584
if (totalChunks > 200) {
563585
const maxSizeGB = (200 * 20 / 1024).toFixed(1);
564-
throw new Error(`文件过大: ${fileSizeMB}MB (${totalChunks}个分块),超过最大限制 ${maxSizeGB}GB (200个分块)`);
586+
const errorMsg = `文件过大: ${fileSizeMB}MB (${totalChunks}个分块),超过最大限制 ${maxSizeGB}GB (200个分块)`;
587+
console.error(`[文件夹上传] ${errorMsg}`);
588+
console.groupEnd();
589+
throw new Error(errorMsg);
565590
}
566591

567592
try {
568593
// 1. 初始化分块上传
594+
console.log(`[文件夹上传] 步骤 1/3: 初始化分块上传...`);
569595
const initFormData = new FormData();
570596
initFormData.append('originalFileName', relativePath);
571597
initFormData.append('originalFileType', file.type || 'application/octet-stream');
@@ -576,25 +602,44 @@
576602
if (config && config.uploadChannel) {
577603
initUrl.searchParams.set('uploadChannel', config.uploadChannel);
578604
}
605+
606+
// 添加认证参数
607+
const currentUrl = new URL(window.location.href);
608+
const authCode = currentUrl.searchParams.get('authCode');
609+
if (authCode) {
610+
initUrl.searchParams.set('authCode', authCode);
611+
}
612+
613+
console.log(`[文件夹上传] 初始化请求 URL: ${initUrl.toString()}`);
614+
console.log(`[文件夹上传] 请求头:`, getUploadHeaders());
579615

580616
const initResponse = await fetch(initUrl.toString(), {
581617
method: 'POST',
582618
body: initFormData,
583619
headers: getUploadHeaders()
584620
});
585621

622+
console.log(`[文件夹上传] 初始化响应状态: ${initResponse.status} ${initResponse.statusText}`);
623+
586624
if (!initResponse.ok) {
587625
const errorText = await initResponse.text();
588-
throw new Error(`初始化分块上传失败: ${errorText}`);
626+
console.error(`[文件夹上传] 初始化失败响应:`, errorText);
627+
console.groupEnd();
628+
throw new Error(`初始化分块上传失败 (${initResponse.status}): ${errorText}`);
589629
}
590630

591631
const initResult = await initResponse.json();
632+
console.log(`[文件夹上传] 初始化结果:`, initResult);
592633
const uploadId = initResult.uploadId;
593634

594635
if (!uploadId) {
636+
console.error(`[文件夹上传] 初始化失败: 未返回 uploadId`);
637+
console.groupEnd();
595638
throw new Error('初始化分块上传失败: 未返回 uploadId');
596639
}
597640

641+
console.log(`[文件夹上传] 步骤 2/3: 上传分块 (uploadId: ${uploadId})...`);
642+
598643
// 2. 上传所有分块
599644
const uploadPromises = [];
600645
let completedChunks = 0;
@@ -647,9 +692,11 @@
647692
}
648693

649694
// 等待所有分块上传完成
695+
console.log(`[文件夹上传] 所有分块上传完成 (${totalChunks}/${totalChunks})`);
650696
await Promise.all(uploadPromises);
651697

652698
// 3. 合并分块
699+
console.log(`[文件夹上传] 步骤 3/3: 合并分块...`);
653700
if (progressCallback) {
654701
progressCallback({ current: totalChunks, total: totalChunks, merging: true });
655702
}
@@ -679,30 +726,43 @@
679726
mergeUrl.searchParams.set('uploadChannel', config.uploadChannel);
680727
}
681728

729+
console.log(`[文件夹上传] 合并请求 URL: ${mergeUrl.toString()}`);
682730
const mergeResponse = await fetch(mergeUrl.toString(), {
683731
method: 'POST',
684732
body: mergeFormData,
685733
headers: getUploadHeaders()
686734
});
687735

736+
console.log(`[文件夹上传] 合并响应状态: ${mergeResponse.status} ${mergeResponse.statusText}`);
737+
688738
if (!mergeResponse.ok) {
689739
const errorText = await mergeResponse.text();
690-
throw new Error(`合并分块失败: ${errorText}`);
740+
console.error(`[文件夹上传] 合并失败响应:`, errorText);
741+
console.groupEnd();
742+
throw new Error(`合并分块失败 (${mergeResponse.status}): ${errorText}`);
691743
}
692744

693745
const mergeResult = await mergeResponse.json();
746+
console.log(`[文件夹上传] 合并结果:`, mergeResult);
694747

695748
// 如果返回的是异步处理状态,需要轮询检查状态
696749
if (mergeResult.status === 'processing' || mergeResult.status === 'merging') {
750+
console.log(`[文件夹上传] 合并处理中,等待完成...`);
697751
if (progressCallback) {
698752
progressCallback({ current: totalChunks, total: totalChunks, merging: true, waiting: true });
699753
}
700-
return await waitForMergeCompletion(uploadId, baseUrl);
754+
const finalResult = await waitForMergeCompletion(uploadId, baseUrl);
755+
console.log(`[文件夹上传] 分块上传完成!`);
756+
console.groupEnd();
757+
return finalResult;
701758
}
702759

760+
console.log(`[文件夹上传] 分块上传完成!`);
761+
console.groupEnd();
703762
return mergeResult;
704763
} catch (error) {
705-
console.error('分块上传失败:', error);
764+
console.error('[文件夹上传] 分块上传失败:', error);
765+
console.groupEnd();
706766
throw error;
707767
}
708768
}

0 commit comments

Comments
 (0)