Merge remote-tracking branch 'origin/main' into 18692-frozen-projects-workbench-support
[arvados-workbench2.git] / src / views-components / context-menu / actions / download-action.tsx
index 2ec7fe1c735a6496861a4b4acd8c872453342ea4..352f84bd58049863f5b7e5ab55b46afcaf86b250 100644 (file)
@@ -2,34 +2,40 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from "react";
-import { ListItemIcon, ListItemText, ListItem } from "@material-ui/core";
-import { DownloadIcon } from "../../../components/icon/icon";
-import * as JSZip from "jszip";
-import * as FileSaver from 'file-saver';
+import React from 'react';
+import { ListItemIcon, ListItemText, ListItem } from '@material-ui/core';
+import { DownloadIcon } from '../../../components/icon/icon';
+import JSZip from 'jszip';
+import FileSaver from 'file-saver';
 import axios from 'axios';
 
 export const DownloadAction = (props: { href?: any, download?: any, onClick?: () => void, kind?: string, currentCollectionUuid?: string; }) => {
     const downloadProps = props.download ? { download: props.download } : {};
 
     const createZip = (fileUrls: string[], download: string[]) => {
-        const zip = new JSZip();
         let id = 1;
-        fileUrls.map((href: string) => {
+        const zip = new JSZip();
+        const filteredFileUrls = fileUrls
+            .filter((href: string) => {
+                const letter = href.split('').pop();
+                return letter !== '/';
+            });
+
+        filteredFileUrls.forEach((href: string) => {
             axios.get(href).then(response => response).then(({ data }: any) => {
                 const splittedByDot = href.split('.');
                 if (splittedByDot[splittedByDot.length - 1] !== 'json') {
-                    if (fileUrls.length === id) {
-                        zip.file(download[id-1], data);
+                    if (filteredFileUrls.length === id) {
+                        zip.file(download[id - 1], data);
                         zip.generateAsync({ type: 'blob' }).then((content) => {
                             FileSaver.saveAs(content, `download-${props.currentCollectionUuid}.zip`);
                         });
                     } else {
-                        zip.file(download[id-1], data);
+                        zip.file(download[id - 1], data);
                         zip.generateAsync({ type: 'blob' });
                     }
                 } else {
-                    zip.file(download[id-1], JSON.stringify(data));
+                    zip.file(download[id - 1], JSON.stringify(data));
                     zip.generateAsync({ type: 'blob' });
                 }
                 id++;
@@ -40,7 +46,7 @@ export const DownloadAction = (props: { href?: any, download?: any, onClick?: ()
     return props.href || props.kind === 'files'
         ? <a
             style={{ textDecoration: 'none' }}
-            href={props.kind === 'files' ? null : props.href}
+            href={props.kind === 'files' ? undefined : `${props.href}&disposition=attachment`}
             onClick={props.onClick}
             {...downloadProps}>
             <ListItem button onClick={() => props.kind === 'files' ? createZip(props.href, props.download) : undefined}>