Browse Source

add formated file size

Alexander Rose 3 years ago
parent
commit
945e55f8a7
2 changed files with 14 additions and 6 deletions
  1. 8 6
      src/extensions/zenodo/ui.tsx
  2. 6 0
      src/mol-util/index.ts

+ 8 - 6
src/extensions/zenodo/ui.tsx

@@ -14,6 +14,7 @@ import { Button } from '../../mol-plugin-ui/controls/common';
 import { OpenInBrowserSvg } from '../../mol-plugin-ui/controls/icons';
 import { ParameterControls } from '../../mol-plugin-ui/controls/parameters';
 import { PluginContext } from '../../mol-plugin/context';
+import { formatBytes } from '../../mol-util';
 import { ParamDefinition as PD } from '../../mol-util/param-definition';
 
 type ZenodoFile = {
@@ -75,19 +76,20 @@ function createImportParams(files: ZenodoFile[], plugin: PluginContext) {
     }
 
     for (const file of files) {
+        const label = `${file.key} (${formatBytes(file.size)})`;
         if (structureExts.has(file.type)) {
             const { format, isBinary } = structureExts.get(file.type)!;
-            modelOpts.push([`${file.links.self}|${format}|${isBinary}`, file.key]);
-            topologyOpts.push([`${file.links.self}|${format}|${isBinary}`, file.key]);
+            modelOpts.push([`${file.links.self}|${format}|${isBinary}`, label]);
+            topologyOpts.push([`${file.links.self}|${format}|${isBinary}`, label]);
         } else if (volumeExts.has(file.type)) {
             const { format, isBinary } = volumeExts.get(file.type)!;
-            volumeOpts.push([`${file.links.self}|${format}|${isBinary}`, file.key]);
+            volumeOpts.push([`${file.links.self}|${format}|${isBinary}`, label]);
         } else if (file.type === 'psf') {
-            topologyOpts.push([`${file.links.self}|${file.type}|false`, file.key]);
+            topologyOpts.push([`${file.links.self}|${file.type}|false`, label]);
         } else if (file.type === 'xtc' || file.type === 'dcd') {
-            coordinatesOpts.push([`${file.links.self}|${file.type}|true`, file.key]);
+            coordinatesOpts.push([`${file.links.self}|${file.type}|true`, label]);
         } else if (file.type === 'zip') {
-            compressedOpts.push([`${file.links.self}|${file.type}|true`, file.key]);
+            compressedOpts.push([`${file.links.self}|${file.type}|true`, label]);
         }
     }
 

+ 6 - 0
src/mol-util/index.ts

@@ -202,4 +202,10 @@ export function formatProgress(p: Progress) {
     if (tp.isIndeterminate) return tp.message;
     const x = (100 * tp.current / tp.max).toFixed(2);
     return `${tp.message} ${x}%`;
+}
+
+export function formatBytes(count: number) {
+    const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+    const i = Math.floor(Math.log(count) / Math.log(1024));
+    return `${(count / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
 }