1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { ListItemIcon, ListItemText, ListItem } from "@material-ui/core";
7 import { DownloadIcon } from "../../../components/icon/icon";
8 import * as JSZip from "jszip";
9 import * as FileSaver from 'file-saver';
10 import axios from 'axios';
12 export const DownloadAction = (props: { href?: any, download?: any, onClick?: () => void, kind?: string, currentCollectionUuid?: string; }) => {
13 const downloadProps = props.download ? { download: props.download } : {};
15 const createZip = (fileUrls: string[], download: string[]) => {
16 const zip = new JSZip();
18 fileUrls.map((href: string) => {
19 axios.get(href).then(response => response).then(({ data }: any) => {
20 const splittedByDot = href.split('.');
21 if (splittedByDot[splittedByDot.length - 1] !== 'json') {
22 if (fileUrls.length === id) {
23 zip.file(download[id-1], data);
24 zip.generateAsync({ type: 'blob' }).then((content) => {
25 FileSaver.saveAs(content, `download-${props.currentCollectionUuid}.zip`);
28 zip.file(download[id-1], data);
29 zip.generateAsync({ type: 'blob' });
32 zip.file(download[id-1], JSON.stringify(data));
33 zip.generateAsync({ type: 'blob' });
40 return props.href || props.kind === 'files'
42 style={{ textDecoration: 'none' }}
43 href={props.kind === 'files' ? null : props.href}
44 onClick={props.onClick}
46 <ListItem button onClick={() => props.kind === 'files' ? createZip(props.href, props.download) : undefined}>
47 {props.kind !== 'files' ?
50 </ListItemIcon> : <span />}
52 {props.kind === 'files' ? 'Download selected' : 'Download'}