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[]) => {
17 const zip = new JSZip();
18 const filteredFileUrls = fileUrls
19 .filter((href: string) => {
20 const letter = href.split('').pop();
21 return letter !== '/';
25 .map((href: string) => {
26 axios.get(href).then(response => response).then(({ data }: any) => {
27 const splittedByDot = href.split('.');
28 if (splittedByDot[splittedByDot.length - 1] !== 'json') {
29 if (filteredFileUrls.length === id) {
30 zip.file(download[id - 1], data);
31 zip.generateAsync({ type: 'blob' }).then((content) => {
32 FileSaver.saveAs(content, `download-${props.currentCollectionUuid}.zip`);
35 zip.file(download[id - 1], data);
36 zip.generateAsync({ type: 'blob' });
39 zip.file(download[id - 1], JSON.stringify(data));
40 zip.generateAsync({ type: 'blob' });
47 return props.href || props.kind === 'files'
49 style={{ textDecoration: 'none' }}
50 href={props.kind === 'files' ? undefined : `${props.href}&disposition=attachment`}
51 onClick={props.onClick}
53 <ListItem button onClick={() => props.kind === 'files' ? createZip(props.href, props.download) : undefined}>
54 {props.kind !== 'files' ?
57 </ListItemIcon> : <span />}
59 {props.kind === 'files' ? 'Download selected' : 'Download'}