]> OzVa Git service - ozva-cloud/commitdiff
feat: display upload speed and time left (#34)
authorsigoden <sigoden@gmail.com>
Sat, 11 Jun 2022 04:34:03 +0000 (12:34 +0800)
committerGitHub <noreply@github.com>
Sat, 11 Jun 2022 04:34:03 +0000 (12:34 +0800)
assets/index.css
assets/index.html
assets/index.js

index bbc9f8e90cb8ea267c17526560dc51c4eb6fe58e..3e566349f3a30efb492b7c84ddd5483d5c32adc5 100644 (file)
@@ -93,6 +93,11 @@ body {
   display: none;
 }
 
+.upload-status span {
+  width: 70px;
+  display: inline-block;
+}
+
 .main {
   padding: 0 1em;
 }
index 39caf9ccd9c7ea5fec7b4e1915fccf9e15e06997..edadb84fe8ce8a153c77407beba501a4a79d5394 100644 (file)
@@ -36,7 +36,7 @@
       <thead>
         <tr>
           <th class="cell-name">Name</th>
-          <th class="cell-status">Status</th>
+          <th class="cell-status">Speed - Progress - Time Left</th>
         </tr>
       </thead>
     </table>
index a60286e4890bac88952ab3a8a0e2ee49f50788d8..ec8d49a0deb6ed67f65a1e1142456308eba0dc58 100644 (file)
@@ -52,6 +52,14 @@ class Uploader {
    * @type Element
    */
   $uploadStatus;
+  /**
+   * @type number
+   */
+  uploaded = 0;
+  /**
+   * @type number
+   */
+  lastUptime = 0;
   static globalIdx = 0;
   constructor(file, dirs) {
     this.name = [...dirs, file.name].join("/");
@@ -68,11 +76,12 @@ class Uploader {
       <div>${getSvg("File")}</div>
       <a href="${url}">${name}</a>
     </td>
-    <td class="cell-status" id="uploadStatus${idx}"></td>
+    <td class="cell-status upload-status" id="uploadStatus${idx}"></td>
   </tr>`);
     $uploadersTable.classList.remove("hidden");
     $emptyFolder.classList.add("hidden");
     this.$uploadStatus = document.getElementById(`uploadStatus${idx}`);
+    this.lastUptime = Date.now();
 
     const ajax = new XMLHttpRequest();
     ajax.upload.addEventListener("progress", e => this.progress(e), false);
@@ -92,8 +101,15 @@ class Uploader {
   }
 
   progress(event) {
-    const percent = (event.loaded / event.total) * 100;
-    this.$uploadStatus.innerHTML = `${percent.toFixed(2)}%`;
+    let now = Date.now();
+    let speed = (event.loaded - this.uploaded) / (now - this.lastUptime) * 1000;
+    let [speedValue, speedUnit] = formatSize(speed);
+    const speedText = `${speedValue}${speedUnit.toLowerCase()}/s`;
+    const progress = formatPercent((event.loaded / event.total) * 100);
+    const duration = formatDuration((event.total - event.loaded) / speed)
+    this.$uploadStatus.innerHTML = `<span>${speedText}</span><span>${progress}</span><span>${duration}</span>`;
+    this.uploaded = event.loaded;
+    this.lastUptime = now;
   }
 
   complete() {
@@ -174,7 +190,7 @@ function addPath(file, index) {
     <a href="${url}" title="${file.name}">${file.name}</a>
   </td>
   <td class="cell-mtime">${formatMtime(file.mtime)}</td>
-  <td class="cell-size">${formatSize(file.size)}</td>
+  <td class="cell-size">${formatSize(file.size).join(" ")}</td>
   ${actionCell}
 </tr>`)
 }
@@ -284,11 +300,27 @@ function padZero(value, size) {
 }
 
 function formatSize(size) {
-  if (!size) return ""
+  if (!size) return []
   const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
-  if (size == 0) return '0 Byte';
+  if (size == 0) return [0, "Byte"];
   const i = parseInt(Math.floor(Math.log(size) / Math.log(1024)));
-  return Math.round(size / Math.pow(1024, i), 2) + ' ' + sizes[i];
+  return [Math.round(size / Math.pow(1024, i), 2), sizes[i]];
+}
+
+function formatDuration(seconds) {
+  seconds = Math.ceil(seconds);
+  let h = Math.floor(seconds / 3600);
+  let m = Math.floor((seconds - h * 3600) / 60);
+  let s = seconds - h * 3600 - m * 60
+  return `${padZero(h, 2)}:${padZero(m, 2)}:${padZero(s, 2)}`;
+}
+
+function formatPercent(precent) {
+  if (precent > 10) {
+    return precent.toFixed(1) + "%";
+  } else {
+    return precent.toFixed(2) + "%";
+  }
 }
 
 function ready() {