BlueGit

timfa filemanager

fix transfer bar

timfa Tim Falken · pushed by timfa

Authored 2026-05-30 · 5 months ago

Hash
d5e89988f5b17f0fb8f71fe0791150fa261f7a65
Parent
7e6dfc52
2 files changed +261 −43
public/app.js +218 −29
@@ -5056,11 +5056,8 @@ async function uploadFiles (fileList)
5056 5056 const files = Array.from(fileList || []);
5057 5057 if (!files.length) return;
5058 5058
5059 // Show upload bar
5060 const wrap = document.createElement('div');
5061 wrap.className = 'upload-bar-wrap';
5062 wrap.innerHTML = `<span>Preparing upload…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5063 document.body.appendChild(wrap);
5059 const uploadUi = createUploadProgressBar('Preparing upload...');
5060 const { wrap, abortSignal } = uploadUi;
5064 5061 STATE.uploadInProgress = true;
5065 5062
5066 5063 try
@@ -5068,20 +5065,25 @@ async function uploadFiles (fileList)
5068 5065 const existingNames = await listDirNameSet(STATE.currentDir);
5069 5066 let done = 0;
5070 5067 let uploaded = 0;
5068 let uploadedBytes = 0;
5069 const totalBytes = getTotalUploadBytes(files);
5070 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5071 5071
5072 5072 for (const original of files)
5073 5073 {
5074 throwIfUploadAborted(abortSignal);
5074 5075 let fileToUpload = original;
5075 5076
5076 5077 if (existingNames.has(original.name))
5077 5078 {
5078 5079 const suggested = nextAvailableName(original.name, existingNames);
5079 5080 const decision = await showFileConflictChoice(original.name, suggested, 'upload');
5081 throwIfUploadAborted(abortSignal);
5080 5082 if (decision === 'cancel')
5081 5083 {
5082 5084 done++;
5083 wrap.querySelector('span').textContent = `Uploading ${done} / ${files.length} file(s)…`;
5084 wrap.querySelector('.upload-bar-fill').style.width = (done / files.length * 100) + '%';
5085 uploadedBytes += getUploadFileSize(original);
5086 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5085 5087 continue;
5086 5088 }
5087 5089
@@ -5091,18 +5093,32 @@ async function uploadFiles (fileList)
5091 5093 }
5092 5094 }
5093 5095
5094 await uploadSingleFile(fileToUpload, STATE.currentDir);
5096 await uploadSingleFile(fileToUpload, STATE.currentDir, (fileBytes) =>
5097 {
5098 updateUploadProgressBar(wrap, {
5099 done,
5100 total: files.length,
5101 uploadedBytes: uploadedBytes + fileBytes,
5102 totalBytes,
5103 });
5104 }, abortSignal);
5095 5105 existingNames.add(fileToUpload.name);
5096 5106 done++;
5097 5107 uploaded++;
5098 wrap.querySelector('span').textContent = `Uploading ${done} / ${files.length} file(s)…`;
5099 wrap.querySelector('.upload-bar-fill').style.width = (done / files.length * 100) + '%';
5108 uploadedBytes += getUploadFileSize(fileToUpload);
5109 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5100 5110 }
5101 5111
5102 5112 toast(`Uploaded ${uploaded} file(s).`, 'ok');
5103 5113 await navigateTo(STATE.currentDir, STATE.currentRoot);
5104 5114 } catch (err)
5105 5115 {
5116 if (isUploadAbortError(err))
5117 {
5118 toast('Upload cancelled.', 'ok');
5119 await navigateTo(STATE.currentDir, STATE.currentRoot);
5120 return;
5121 }
5106 5122 toast(err.message, 'err');
5107 5123 } finally
5108 5124 {
@@ -5111,26 +5127,167 @@ async function uploadFiles (fileList)
5111 5127 }
5112 5128 }
5113 5129
5114 async function uploadSingleFile (file, targetDir)
5130 function createUploadProgressBar (text)
5131 {
5132 const abortSignal = createUploadAbortSignal();
5133 const wrap = document.createElement('div');
5134 wrap.className = 'upload-bar-wrap';
5135 wrap.innerHTML = `
5136 <div class="upload-bar-row">
5137 <span>${escapeHtml(text)}</span>
5138 <button type="button" class="upload-abort-btn">Abort</button>
5139 </div>
5140 <div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>
5141 `;
5142 wrap.querySelector('.upload-abort-btn').addEventListener('click', () =>
5143 {
5144 abortSignal.abort();
5145 wrap.querySelector('.upload-abort-btn').disabled = true;
5146 wrap.querySelector('span').textContent = 'Cancelling upload...';
5147 });
5148 document.body.appendChild(wrap);
5149 return { wrap, abortSignal };
5150 }
5151
5152 function createUploadAbortSignal ()
5153 {
5154 return {
5155 aborted: false,
5156 activeXhr: null,
5157 abort ()
5158 {
5159 if (this.aborted) return;
5160 this.aborted = true;
5161 if (this.activeXhr) this.activeXhr.abort();
5162 },
5163 };
5164 }
5165
5166 function createUploadAbortError ()
5167 {
5168 const err = new Error('Upload cancelled.');
5169 err.name = 'AbortError';
5170 return err;
5171 }
5172
5173 function isUploadAbortError (err)
5174 {
5175 return err && (err.name === 'AbortError' || /upload cancelled|abort/i.test(String(err.message || '')));
5176 }
5177
5178 function throwIfUploadAborted (abortSignal)
5179 {
5180 if (abortSignal && abortSignal.aborted) throw createUploadAbortError();
5181 }
5182
5183 function getUploadFileSize (file)
5184 {
5185 const size = Number(file && file.size);
5186 return Number.isFinite(size) && size > 0 ? size : 0;
5187 }
5188
5189 function getTotalUploadBytes (files)
5190 {
5191 return (files || []).reduce((sum, file) => sum + getUploadFileSize(file), 0);
5192 }
5193
5194 function uploadProgressPercent (done, total, uploadedBytes, totalBytes)
5195 {
5196 if (totalBytes > 0) return Math.max(0, Math.min(100, (uploadedBytes / totalBytes) * 100));
5197 if (total > 0) return Math.max(0, Math.min(100, (done / total) * 100));
5198 return 0;
5199 }
5200
5201 function updateUploadProgressBar (wrap, { done, total, uploadedBytes, totalBytes })
5202 {
5203 const percent = uploadProgressPercent(done, total, uploadedBytes, totalBytes);
5204 const active = Math.min(total, done < total ? done + 1 : done);
5205 wrap.querySelector('span').textContent = `Uploading ${active} / ${total} file(s)... ${Math.round(percent)}%`;
5206 wrap.querySelector('.upload-bar-fill').style.width = `${percent}%`;
5207 }
5208
5209 async function uploadSingleFile (file, targetDir, onProgress, abortSignal)
5115 5210 {
5116 5211 const formData = new FormData();
5117 5212 formData.append('files', file);
5118 5213
5119 5214 await new Promise((resolve, reject) =>
5120 5215 {
5216 if (abortSignal && abortSignal.aborted)
5217 {
5218 reject(createUploadAbortError());
5219 return;
5220 }
5221
5121 5222 const xhr = new XMLHttpRequest();
5223 const fileSize = getUploadFileSize(file);
5224 let settled = false;
5225
5226 const cleanup = () =>
5227 {
5228 if (abortSignal && abortSignal.activeXhr === xhr) abortSignal.activeXhr = null;
5229 };
5230
5231 const finishResolve = () =>
5232 {
5233 if (settled) return;
5234 settled = true;
5235 cleanup();
5236 resolve();
5237 };
5238
5239 const finishReject = (err) =>
5240 {
5241 if (settled) return;
5242 settled = true;
5243 cleanup();
5244 reject(err);
5245 };
5246
5122 5247 xhr.open('POST', `/api/files/upload?dir=${encodeURIComponent(targetDir)}`);
5123 5248 xhr.setRequestHeader('Authorization', 'Bearer ' + STATE.token);
5124 5249 xhr.setRequestHeader('X-Fp', getFingerprint());
5125 5250 xhr.setRequestHeader('X-Upload-Size', String(file.size || 0));
5126 xhr.onload = () => xhr.status === 200
5127 ? resolve()
5128 : reject(new Error((JSON.parse(xhr.responseText) || {}).error || 'Upload failed'));
5129 xhr.onerror = () => reject(new Error('Network error'));
5251 if (abortSignal) abortSignal.activeXhr = xhr;
5252 if (xhr.upload)
5253 {
5254 xhr.upload.onprogress = (event) =>
5255 {
5256 if (typeof onProgress !== 'function') return;
5257 if (event.lengthComputable && event.total > 0 && fileSize > 0)
5258 {
5259 onProgress(Math.min(fileSize, fileSize * (event.loaded / event.total)));
5260 }
5261 };
5262 }
5263 xhr.onload = () =>
5264 {
5265 if (xhr.status === 200)
5266 {
5267 if (typeof onProgress === 'function') onProgress(fileSize);
5268 finishResolve();
5269 return;
5270 }
5271
5272 finishReject(new Error(parseUploadError(xhr.responseText)));
5273 };
5274 xhr.onerror = () => finishReject(new Error('Network error'));
5275 xhr.onabort = () => finishReject(createUploadAbortError());
5130 5276 xhr.send(formData);
5131 5277 });
5132 5278 }
5133 5279
5280 function parseUploadError (responseText)
5281 {
5282 try
5283 {
5284 return (JSON.parse(responseText) || {}).error || 'Upload failed';
5285 } catch
5286 {
5287 return 'Upload failed';
5288 }
5289 }
5290
5134 5291 async function ensureServerDir (baseDir, relativeDir)
5135 5292 {
5136 5293 const parts = String(relativeDir || '').split(/[\\/]/).filter(Boolean);
@@ -5159,20 +5316,22 @@ async function uploadFolderInputFiles (fileList)
5159 5316 const files = Array.from(fileList);
5160 5317 const total = files.length;
5161 5318 let done = 0;
5319 let uploadedBytes = 0;
5320 const totalBytes = getTotalUploadBytes(files);
5162 5321
5163 const wrap = document.createElement('div');
5164 wrap.className = 'upload-bar-wrap';
5165 wrap.innerHTML = `<span>Preparing folder upload…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5166 document.body.appendChild(wrap);
5322 const uploadUi = createUploadProgressBar('Preparing folder upload...');
5323 const { wrap, abortSignal } = uploadUi;
5167 5324 STATE.uploadInProgress = true;
5168 5325
5169 5326 try
5170 5327 {
5171 5328 const dirCache = new Map();
5172 5329 dirCache.set('', STATE.currentDir);
5330 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5173 5331
5174 5332 for (const file of files)
5175 5333 {
5334 throwIfUploadAborted(abortSignal);
5176 5335 const rel = file.webkitRelativePath || file.name;
5177 5336 const relParts = rel.split(/[\\/]/).filter(Boolean);
5178 5337 const relDir = relParts.slice(0, -1).join('/');
@@ -5195,16 +5354,30 @@ async function uploadFolderInputFiles (fileList)
5195 5354 }
5196 5355
5197 5356 const targetDir = dirCache.get(relDir) || STATE.currentDir;
5198 await uploadSingleFile(file, targetDir);
5357 await uploadSingleFile(file, targetDir, (fileBytes) =>
5358 {
5359 updateUploadProgressBar(wrap, {
5360 done,
5361 total,
5362 uploadedBytes: uploadedBytes + fileBytes,
5363 totalBytes,
5364 });
5365 }, abortSignal);
5199 5366 done++;
5200 wrap.querySelector('span').textContent = `Uploading ${done} / ${total} file(s)…`;
5201 wrap.querySelector('.upload-bar-fill').style.width = (done / total * 100) + '%';
5367 uploadedBytes += getUploadFileSize(file);
5368 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5202 5369 }
5203 5370
5204 5371 toast(`Uploaded ${total} file(s).`, 'ok');
5205 5372 await navigateTo(STATE.currentDir, STATE.currentRoot);
5206 5373 } catch (err)
5207 5374 {
5375 if (isUploadAbortError(err))
5376 {
5377 toast('Upload cancelled.', 'ok');
5378 await navigateTo(STATE.currentDir, STATE.currentRoot);
5379 return;
5380 }
5208 5381 toast(err.message, 'err');
5209 5382 } finally
5210 5383 {
@@ -5281,10 +5454,8 @@ async function uploadEntries (entries)
5281 5454 if (!STATE.currentDir) { toast('Open a folder first.', 'err'); return; }
5282 5455 if (!canWrite()) { toast('No write access.', 'err'); return; }
5283 5456
5284 const wrap = document.createElement('div');
5285 wrap.className = 'upload-bar-wrap';
5286 wrap.innerHTML = `<span>Scanning…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5287 document.body.appendChild(wrap);
5457 const uploadUi = createUploadProgressBar('Scanning...');
5458 const { wrap, abortSignal } = uploadUi;
5288 5459 STATE.uploadInProgress = true;
5289 5460
5290 5461 try
@@ -5292,6 +5463,7 @@ async function uploadEntries (entries)
5292 5463 const toUpload = [];
5293 5464 for (const entry of entries)
5294 5465 {
5466 throwIfUploadAborted(abortSignal);
5295 5467 await collectEntries(entry, STATE.currentDir, toUpload);
5296 5468 }
5297 5469
@@ -5299,17 +5471,28 @@ async function uploadEntries (entries)
5299 5471
5300 5472 let done = 0;
5301 5473 const total = toUpload.length;
5474 let uploadedBytes = 0;
5475 const totalBytes = getTotalUploadBytes(toUpload.map(({ file }) => file));
5302 5476 const updateBar = () =>
5303 5477 {
5304 wrap.querySelector('span').textContent = `Uploading ${done} / ${total} file(s)…`;
5305 wrap.querySelector('.upload-bar-fill').style.width = (done / total * 100) + '%';
5478 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5306 5479 };
5307 5480 updateBar();
5308 5481
5309 5482 for (const { file, targetDir } of toUpload)
5310 5483 {
5311 await uploadSingleFile(file, targetDir);
5484 throwIfUploadAborted(abortSignal);
5485 await uploadSingleFile(file, targetDir, (fileBytes) =>
5486 {
5487 updateUploadProgressBar(wrap, {
5488 done,
5489 total,
5490 uploadedBytes: uploadedBytes + fileBytes,
5491 totalBytes,
5492 });
5493 }, abortSignal);
5312 5494 done++;
5495 uploadedBytes += getUploadFileSize(file);
5313 5496 updateBar();
5314 5497 }
5315 5498
@@ -5317,6 +5500,12 @@ async function uploadEntries (entries)
5317 5500 await navigateTo(STATE.currentDir, STATE.currentRoot);
5318 5501 } catch (err)
5319 5502 {
5503 if (isUploadAbortError(err))
5504 {
5505 toast('Upload cancelled.', 'ok');
5506 await navigateTo(STATE.currentDir, STATE.currentRoot);
5507 return;
5508 }
5320 5509 toast(err.message, 'err');
5321 5510 } finally
5322 5511 {
@@ -5056,11 +5056,8 @@ async function uploadFiles (fileList)
5056 const files = Array.from(fileList || []);
5056 const files = Array.from(fileList || []);
5057 if (!files.length) return;
5057 if (!files.length) return;
5058
5058
5059 // Show upload bar
5059 const uploadUi = createUploadProgressBar('Preparing upload...');
5060 const wrap = document.createElement('div');
5060 const { wrap, abortSignal } = uploadUi;
5061 wrap.className = 'upload-bar-wrap';
5062 wrap.innerHTML = `<span>Preparing upload…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5063 document.body.appendChild(wrap);
5064 STATE.uploadInProgress = true;
5061 STATE.uploadInProgress = true;
5065
5062
5066 try
5063 try
@@ -5068,20 +5065,25 @@ async function uploadFiles (fileList)
5068 const existingNames = await listDirNameSet(STATE.currentDir);
5065 const existingNames = await listDirNameSet(STATE.currentDir);
5069 let done = 0;
5066 let done = 0;
5070 let uploaded = 0;
5067 let uploaded = 0;
5068 let uploadedBytes = 0;
5069 const totalBytes = getTotalUploadBytes(files);
5070 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5071
5071
5072 for (const original of files)
5072 for (const original of files)
5073 {
5073 {
5074 throwIfUploadAborted(abortSignal);
5074 let fileToUpload = original;
5075 let fileToUpload = original;
5075
5076
5076 if (existingNames.has(original.name))
5077 if (existingNames.has(original.name))
5077 {
5078 {
5078 const suggested = nextAvailableName(original.name, existingNames);
5079 const suggested = nextAvailableName(original.name, existingNames);
5079 const decision = await showFileConflictChoice(original.name, suggested, 'upload');
5080 const decision = await showFileConflictChoice(original.name, suggested, 'upload');
5081 throwIfUploadAborted(abortSignal);
5080 if (decision === 'cancel')
5082 if (decision === 'cancel')
5081 {
5083 {
5082 done++;
5084 done++;
5083 wrap.querySelector('span').textContent = `Uploading ${done} / ${files.length} file(s)…`;
5085 uploadedBytes += getUploadFileSize(original);
5084 wrap.querySelector('.upload-bar-fill').style.width = (done / files.length * 100) + '%';
5086 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5085 continue;
5087 continue;
5086 }
5088 }
5087
5089
@@ -5091,18 +5093,32 @@ async function uploadFiles (fileList)
5091 }
5093 }
5092 }
5094 }
5093
5095
5094 await uploadSingleFile(fileToUpload, STATE.currentDir);
5096 await uploadSingleFile(fileToUpload, STATE.currentDir, (fileBytes) =>
5097 {
5098 updateUploadProgressBar(wrap, {
5099 done,
5100 total: files.length,
5101 uploadedBytes: uploadedBytes + fileBytes,
5102 totalBytes,
5103 });
5104 }, abortSignal);
5095 existingNames.add(fileToUpload.name);
5105 existingNames.add(fileToUpload.name);
5096 done++;
5106 done++;
5097 uploaded++;
5107 uploaded++;
5098 wrap.querySelector('span').textContent = `Uploading ${done} / ${files.length} file(s)…`;
5108 uploadedBytes += getUploadFileSize(fileToUpload);
5099 wrap.querySelector('.upload-bar-fill').style.width = (done / files.length * 100) + '%';
5109 updateUploadProgressBar(wrap, { done, total: files.length, uploadedBytes, totalBytes });
5100 }
5110 }
5101
5111
5102 toast(`Uploaded ${uploaded} file(s).`, 'ok');
5112 toast(`Uploaded ${uploaded} file(s).`, 'ok');
5103 await navigateTo(STATE.currentDir, STATE.currentRoot);
5113 await navigateTo(STATE.currentDir, STATE.currentRoot);
5104 } catch (err)
5114 } catch (err)
5105 {
5115 {
5116 if (isUploadAbortError(err))
5117 {
5118 toast('Upload cancelled.', 'ok');
5119 await navigateTo(STATE.currentDir, STATE.currentRoot);
5120 return;
5121 }
5106 toast(err.message, 'err');
5122 toast(err.message, 'err');
5107 } finally
5123 } finally
5108 {
5124 {
@@ -5111,26 +5127,167 @@ async function uploadFiles (fileList)
5111 }
5127 }
5112}
5128}
5113
5129
5114async function uploadSingleFile (file, targetDir)
5130function createUploadProgressBar (text)
5131{
5132 const abortSignal = createUploadAbortSignal();
5133 const wrap = document.createElement('div');
5134 wrap.className = 'upload-bar-wrap';
5135 wrap.innerHTML = `
5136 <div class="upload-bar-row">
5137 <span>${escapeHtml(text)}</span>
5138 <button type="button" class="upload-abort-btn">Abort</button>
5139 </div>
5140 <div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>
5141 `;
5142 wrap.querySelector('.upload-abort-btn').addEventListener('click', () =>
5143 {
5144 abortSignal.abort();
5145 wrap.querySelector('.upload-abort-btn').disabled = true;
5146 wrap.querySelector('span').textContent = 'Cancelling upload...';
5147 });
5148 document.body.appendChild(wrap);
5149 return { wrap, abortSignal };
5150}
5151
5152function createUploadAbortSignal ()
5153{
5154 return {
5155 aborted: false,
5156 activeXhr: null,
5157 abort ()
5158 {
5159 if (this.aborted) return;
5160 this.aborted = true;
5161 if (this.activeXhr) this.activeXhr.abort();
5162 },
5163 };
5164}
5165
5166function createUploadAbortError ()
5167{
5168 const err = new Error('Upload cancelled.');
5169 err.name = 'AbortError';
5170 return err;
5171}
5172
5173function isUploadAbortError (err)
5174{
5175 return err && (err.name === 'AbortError' || /upload cancelled|abort/i.test(String(err.message || '')));
5176}
5177
5178function throwIfUploadAborted (abortSignal)
5179{
5180 if (abortSignal && abortSignal.aborted) throw createUploadAbortError();
5181}
5182
5183function getUploadFileSize (file)
5184{
5185 const size = Number(file && file.size);
5186 return Number.isFinite(size) && size > 0 ? size : 0;
5187}
5188
5189function getTotalUploadBytes (files)
5190{
5191 return (files || []).reduce((sum, file) => sum + getUploadFileSize(file), 0);
5192}
5193
5194function uploadProgressPercent (done, total, uploadedBytes, totalBytes)
5195{
5196 if (totalBytes > 0) return Math.max(0, Math.min(100, (uploadedBytes / totalBytes) * 100));
5197 if (total > 0) return Math.max(0, Math.min(100, (done / total) * 100));
5198 return 0;
5199}
5200
5201function updateUploadProgressBar (wrap, { done, total, uploadedBytes, totalBytes })
5202{
5203 const percent = uploadProgressPercent(done, total, uploadedBytes, totalBytes);
5204 const active = Math.min(total, done < total ? done + 1 : done);
5205 wrap.querySelector('span').textContent = `Uploading ${active} / ${total} file(s)... ${Math.round(percent)}%`;
5206 wrap.querySelector('.upload-bar-fill').style.width = `${percent}%`;
5207}
5208
5209async function uploadSingleFile (file, targetDir, onProgress, abortSignal)
5115{
5210{
5116 const formData = new FormData();
5211 const formData = new FormData();
5117 formData.append('files', file);
5212 formData.append('files', file);
5118
5213
5119 await new Promise((resolve, reject) =>
5214 await new Promise((resolve, reject) =>
5120 {
5215 {
5216 if (abortSignal && abortSignal.aborted)
5217 {
5218 reject(createUploadAbortError());
5219 return;
5220 }
5221
5121 const xhr = new XMLHttpRequest();
5222 const xhr = new XMLHttpRequest();
5223 const fileSize = getUploadFileSize(file);
5224 let settled = false;
5225
5226 const cleanup = () =>
5227 {
5228 if (abortSignal && abortSignal.activeXhr === xhr) abortSignal.activeXhr = null;
5229 };
5230
5231 const finishResolve = () =>
5232 {
5233 if (settled) return;
5234 settled = true;
5235 cleanup();
5236 resolve();
5237 };
5238
5239 const finishReject = (err) =>
5240 {
5241 if (settled) return;
5242 settled = true;
5243 cleanup();
5244 reject(err);
5245 };
5246
5122 xhr.open('POST', `/api/files/upload?dir=${encodeURIComponent(targetDir)}`);
5247 xhr.open('POST', `/api/files/upload?dir=${encodeURIComponent(targetDir)}`);
5123 xhr.setRequestHeader('Authorization', 'Bearer ' + STATE.token);
5248 xhr.setRequestHeader('Authorization', 'Bearer ' + STATE.token);
5124 xhr.setRequestHeader('X-Fp', getFingerprint());
5249 xhr.setRequestHeader('X-Fp', getFingerprint());
5125 xhr.setRequestHeader('X-Upload-Size', String(file.size || 0));
5250 xhr.setRequestHeader('X-Upload-Size', String(file.size || 0));
5126 xhr.onload = () => xhr.status === 200
5251 if (abortSignal) abortSignal.activeXhr = xhr;
5127 ? resolve()
5252 if (xhr.upload)
5128 : reject(new Error((JSON.parse(xhr.responseText) || {}).error || 'Upload failed'));
5253 {
5129 xhr.onerror = () => reject(new Error('Network error'));
5254 xhr.upload.onprogress = (event) =>
5255 {
5256 if (typeof onProgress !== 'function') return;
5257 if (event.lengthComputable && event.total > 0 && fileSize > 0)
5258 {
5259 onProgress(Math.min(fileSize, fileSize * (event.loaded / event.total)));
5260 }
5261 };
5262 }
5263 xhr.onload = () =>
5264 {
5265 if (xhr.status === 200)
5266 {
5267 if (typeof onProgress === 'function') onProgress(fileSize);
5268 finishResolve();
5269 return;
5270 }
5271
5272 finishReject(new Error(parseUploadError(xhr.responseText)));
5273 };
5274 xhr.onerror = () => finishReject(new Error('Network error'));
5275 xhr.onabort = () => finishReject(createUploadAbortError());
5130 xhr.send(formData);
5276 xhr.send(formData);
5131 });
5277 });
5132}
5278}
5133
5279
5280function parseUploadError (responseText)
5281{
5282 try
5283 {
5284 return (JSON.parse(responseText) || {}).error || 'Upload failed';
5285 } catch
5286 {
5287 return 'Upload failed';
5288 }
5289}
5290
5134async function ensureServerDir (baseDir, relativeDir)
5291async function ensureServerDir (baseDir, relativeDir)
5135{
5292{
5136 const parts = String(relativeDir || '').split(/[\\/]/).filter(Boolean);
5293 const parts = String(relativeDir || '').split(/[\\/]/).filter(Boolean);
@@ -5159,20 +5316,22 @@ async function uploadFolderInputFiles (fileList)
5159 const files = Array.from(fileList);
5316 const files = Array.from(fileList);
5160 const total = files.length;
5317 const total = files.length;
5161 let done = 0;
5318 let done = 0;
5319 let uploadedBytes = 0;
5320 const totalBytes = getTotalUploadBytes(files);
5162
5321
5163 const wrap = document.createElement('div');
5322 const uploadUi = createUploadProgressBar('Preparing folder upload...');
5164 wrap.className = 'upload-bar-wrap';
5323 const { wrap, abortSignal } = uploadUi;
5165 wrap.innerHTML = `<span>Preparing folder upload…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5166 document.body.appendChild(wrap);
5167 STATE.uploadInProgress = true;
5324 STATE.uploadInProgress = true;
5168
5325
5169 try
5326 try
5170 {
5327 {
5171 const dirCache = new Map();
5328 const dirCache = new Map();
5172 dirCache.set('', STATE.currentDir);
5329 dirCache.set('', STATE.currentDir);
5330 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5173
5331
5174 for (const file of files)
5332 for (const file of files)
5175 {
5333 {
5334 throwIfUploadAborted(abortSignal);
5176 const rel = file.webkitRelativePath || file.name;
5335 const rel = file.webkitRelativePath || file.name;
5177 const relParts = rel.split(/[\\/]/).filter(Boolean);
5336 const relParts = rel.split(/[\\/]/).filter(Boolean);
5178 const relDir = relParts.slice(0, -1).join('/');
5337 const relDir = relParts.slice(0, -1).join('/');
@@ -5195,16 +5354,30 @@ async function uploadFolderInputFiles (fileList)
5195 }
5354 }
5196
5355
5197 const targetDir = dirCache.get(relDir) || STATE.currentDir;
5356 const targetDir = dirCache.get(relDir) || STATE.currentDir;
5198 await uploadSingleFile(file, targetDir);
5357 await uploadSingleFile(file, targetDir, (fileBytes) =>
5358 {
5359 updateUploadProgressBar(wrap, {
5360 done,
5361 total,
5362 uploadedBytes: uploadedBytes + fileBytes,
5363 totalBytes,
5364 });
5365 }, abortSignal);
5199 done++;
5366 done++;
5200 wrap.querySelector('span').textContent = `Uploading ${done} / ${total} file(s)…`;
5367 uploadedBytes += getUploadFileSize(file);
5201 wrap.querySelector('.upload-bar-fill').style.width = (done / total * 100) + '%';
5368 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5202 }
5369 }
5203
5370
5204 toast(`Uploaded ${total} file(s).`, 'ok');
5371 toast(`Uploaded ${total} file(s).`, 'ok');
5205 await navigateTo(STATE.currentDir, STATE.currentRoot);
5372 await navigateTo(STATE.currentDir, STATE.currentRoot);
5206 } catch (err)
5373 } catch (err)
5207 {
5374 {
5375 if (isUploadAbortError(err))
5376 {
5377 toast('Upload cancelled.', 'ok');
5378 await navigateTo(STATE.currentDir, STATE.currentRoot);
5379 return;
5380 }
5208 toast(err.message, 'err');
5381 toast(err.message, 'err');
5209 } finally
5382 } finally
5210 {
5383 {
@@ -5281,10 +5454,8 @@ async function uploadEntries (entries)
5281 if (!STATE.currentDir) { toast('Open a folder first.', 'err'); return; }
5454 if (!STATE.currentDir) { toast('Open a folder first.', 'err'); return; }
5282 if (!canWrite()) { toast('No write access.', 'err'); return; }
5455 if (!canWrite()) { toast('No write access.', 'err'); return; }
5283
5456
5284 const wrap = document.createElement('div');
5457 const uploadUi = createUploadProgressBar('Scanning...');
5285 wrap.className = 'upload-bar-wrap';
5458 const { wrap, abortSignal } = uploadUi;
5286 wrap.innerHTML = `<span>Scanning…</span><div class="upload-bar-track"><div class="upload-bar-fill" style="width:0%"></div></div>`;
5287 document.body.appendChild(wrap);
5288 STATE.uploadInProgress = true;
5459 STATE.uploadInProgress = true;
5289
5460
5290 try
5461 try
@@ -5292,6 +5463,7 @@ async function uploadEntries (entries)
5292 const toUpload = [];
5463 const toUpload = [];
5293 for (const entry of entries)
5464 for (const entry of entries)
5294 {
5465 {
5466 throwIfUploadAborted(abortSignal);
5295 await collectEntries(entry, STATE.currentDir, toUpload);
5467 await collectEntries(entry, STATE.currentDir, toUpload);
5296 }
5468 }
5297
5469
@@ -5299,17 +5471,28 @@ async function uploadEntries (entries)
5299
5471
5300 let done = 0;
5472 let done = 0;
5301 const total = toUpload.length;
5473 const total = toUpload.length;
5474 let uploadedBytes = 0;
5475 const totalBytes = getTotalUploadBytes(toUpload.map(({ file }) => file));
5302 const updateBar = () =>
5476 const updateBar = () =>
5303 {
5477 {
5304 wrap.querySelector('span').textContent = `Uploading ${done} / ${total} file(s)…`;
5478 updateUploadProgressBar(wrap, { done, total, uploadedBytes, totalBytes });
5305 wrap.querySelector('.upload-bar-fill').style.width = (done / total * 100) + '%';
5306 };
5479 };
5307 updateBar();
5480 updateBar();
5308
5481
5309 for (const { file, targetDir } of toUpload)
5482 for (const { file, targetDir } of toUpload)
5310 {
5483 {
5311 await uploadSingleFile(file, targetDir);
5484 throwIfUploadAborted(abortSignal);
5485 await uploadSingleFile(file, targetDir, (fileBytes) =>
5486 {
5487 updateUploadProgressBar(wrap, {
5488 done,
5489 total,
5490 uploadedBytes: uploadedBytes + fileBytes,
5491 totalBytes,
5492 });
5493 }, abortSignal);
5312 done++;
5494 done++;
5495 uploadedBytes += getUploadFileSize(file);
5313 updateBar();
5496 updateBar();
5314 }
5497 }
5315
5498
@@ -5317,6 +5500,12 @@ async function uploadEntries (entries)
5317 await navigateTo(STATE.currentDir, STATE.currentRoot);
5500 await navigateTo(STATE.currentDir, STATE.currentRoot);
5318 } catch (err)
5501 } catch (err)
5319 {
5502 {
5503 if (isUploadAbortError(err))
5504 {
5505 toast('Upload cancelled.', 'ok');
5506 await navigateTo(STATE.currentDir, STATE.currentRoot);
5507 return;
5508 }
5320 toast(err.message, 'err');
5509 toast(err.message, 'err');
5321 } finally
5510 } finally
5322 {
5511 {
public/style.css +43 −14
@@ -1552,23 +1552,52 @@ button.danger.admin-update-highlight {
1552 1552 /* =========================================================
1553 1553 UPLOAD PROGRESS
1554 1554 ========================================================= */
1555 .upload-bar-wrap {
1556 position: fixed;
1557 bottom: 0;
1558 left: 0;
1559 right: 0;
1555 .upload-bar-wrap {
1556 position: fixed;
1557 bottom: 0;
1558 left: 0;
1559 right: 0;
1560 1560 background: var(--surface);
1561 1561 border-top: 1px solid var(--border);
1562 1562 padding: 0.5rem 1rem;
1563 1563 z-index: 80;
1564 font-size: 0.85rem;
1565 }
1566
1567 .upload-bar-track {
1568 height: 6px;
1569 background: var(--border);
1570 border-radius: 3px;
1571 margin-top: 0.3rem;
1564 font-size: 0.85rem;
1565 }
1566
1567 .upload-bar-row {
1568 display: flex;
1569 align-items: center;
1570 justify-content: space-between;
1571 gap: 0.75rem;
1572 }
1573
1574 .upload-abort-btn {
1575 flex: 0 0 auto;
1576 border: 1px solid var(--border);
1577 border-radius: 4px;
1578 background: transparent;
1579 color: var(--text);
1580 padding: 0.25rem 0.55rem;
1581 font: inherit;
1582 cursor: pointer;
1583 }
1584
1585 .upload-abort-btn:hover:not(:disabled),
1586 .upload-abort-btn:focus-visible:not(:disabled) {
1587 border-color: var(--accent);
1588 color: var(--accent);
1589 }
1590
1591 .upload-abort-btn:disabled {
1592 cursor: default;
1593 opacity: 0.55;
1594 }
1595
1596 .upload-bar-track {
1597 height: 6px;
1598 background: var(--border);
1599 border-radius: 3px;
1600 margin-top: 0.3rem;
1572 1601 }
1573 1602
1574 1603 .upload-bar-fill {
@@ -1745,4 +1774,4 @@ button.danger.admin-update-highlight {
1745 1774 .dialog-actions button.primary {
1746 1775 width: auto;
1747 1776 margin-top: 0;
1748 }
1777 }
@@ -1552,23 +1552,52 @@ button.danger.admin-update-highlight {
1552/* =========================================================
1552/* =========================================================
1553 UPLOAD PROGRESS
1553 UPLOAD PROGRESS
1554 ========================================================= */
1554 ========================================================= */
1555.upload-bar-wrap {
1555.upload-bar-wrap {
1556 position: fixed;
1556 position: fixed;
1557 bottom: 0;
1557 bottom: 0;
1558 left: 0;
1558 left: 0;
1559 right: 0;
1559 right: 0;
1560 background: var(--surface);
1560 background: var(--surface);
1561 border-top: 1px solid var(--border);
1561 border-top: 1px solid var(--border);
1562 padding: 0.5rem 1rem;
1562 padding: 0.5rem 1rem;
1563 z-index: 80;
1563 z-index: 80;
1564 font-size: 0.85rem;
1564 font-size: 0.85rem;
1565}
1565}
1566
1566
1567.upload-bar-track {
1567.upload-bar-row {
1568 height: 6px;
1568 display: flex;
1569 background: var(--border);
1569 align-items: center;
1570 border-radius: 3px;
1570 justify-content: space-between;
1571 margin-top: 0.3rem;
1571 gap: 0.75rem;
1572}
1573
1574.upload-abort-btn {
1575 flex: 0 0 auto;
1576 border: 1px solid var(--border);
1577 border-radius: 4px;
1578 background: transparent;
1579 color: var(--text);
1580 padding: 0.25rem 0.55rem;
1581 font: inherit;
1582 cursor: pointer;
1583}
1584
1585.upload-abort-btn:hover:not(:disabled),
1586.upload-abort-btn:focus-visible:not(:disabled) {
1587 border-color: var(--accent);
1588 color: var(--accent);
1589}
1590
1591.upload-abort-btn:disabled {
1592 cursor: default;
1593 opacity: 0.55;
1594}
1595
1596.upload-bar-track {
1597 height: 6px;
1598 background: var(--border);
1599 border-radius: 3px;
1600 margin-top: 0.3rem;
1572}
1601}
1573
1602
1574.upload-bar-fill {
1603.upload-bar-fill {
@@ -1745,4 +1774,4 @@ button.danger.admin-update-highlight {
1745.dialog-actions button.primary {
1774.dialog-actions button.primary {
1746 width: auto;
1775 width: auto;
1747 margin-top: 0;
1776 margin-top: 0;
1748}
1777}