X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f2b031de2183439f8aade2f290cd0e3f95f6438c..ffc9666a04b0cef18a15571c1a0f4fdc41e87b75:/src/views-components/context-menu/actions/download-action.tsx diff --git a/src/views-components/context-menu/actions/download-action.tsx b/src/views-components/context-menu/actions/download-action.tsx index 1f6979d0..7468954f 100644 --- a/src/views-components/context-menu/actions/download-action.tsx +++ b/src/views-components/context-menu/actions/download-action.tsx @@ -2,28 +2,63 @@ // // SPDX-License-Identifier: AGPL-3.0 -import * as React from "react"; -import { ListItemIcon, ListItemText, Button, ListItem } from "@material-ui/core"; -import { DownloadIcon } from "../../../components/icon/icon"; +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 axios from 'axios'; -export const DownloadAction = (props: { href?: string, download?: string, onClick?: () => void }) => { - const targetProps = props.download ? {} : { target: '_blank' }; +export const DownloadAction = (props: { href?: any, download?: any, onClick?: () => void, kind?: string, currentCollectionUuid?: string; }) => { const downloadProps = props.download ? { download: props.download } : {}; - return props.href + + const createZip = (fileUrls: string[], download: string[]) => { + let id = 1; + const zip = new JSZip(); + const filteredFileUrls = fileUrls + .filter((href: string) => { + const letter = href.split('').pop(); + return letter !== '/'; + }); + + filteredFileUrls + .map((href: string) => { + axios.get(href).then(response => response).then(({ data }: any) => { + const splittedByDot = href.split('.'); + if (splittedByDot[splittedByDot.length - 1] !== 'json') { + 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.generateAsync({ type: 'blob' }); + } + } else { + zip.file(download[id - 1], JSON.stringify(data)); + zip.generateAsync({ type: 'blob' }); + } + id++; + }); + }); + }; + + return props.href || props.kind === 'files' ? - - - - + props.kind === 'files' ? createZip(props.href, props.download) : undefined}> + {props.kind !== 'files' ? + + + : } - Download - + {props.kind === 'files' ? 'Download selected' : 'Download'} + - + : null; }; \ No newline at end of file