@@ -4,7 +4,7 @@ import Modal from '../../Modal'
4
4
import styles from './ArtifactsListModal.module.scss'
5
5
import PropTypes from 'prop-types'
6
6
import ReactSVG from 'react-svg'
7
- import { getTopcoderReactLib } from '../../../util/topcoder-react-lib'
7
+ import { getTopcoderReactLib , isValidDownloadFile } from '../../../util/topcoder-react-lib'
8
8
import Loader from '../../Loader'
9
9
const assets = require . context ( '../../../assets/images' , false , / s v g / )
10
10
@@ -21,6 +21,17 @@ export const ArtifactsListModal = ({ onClose, submissionId, token, theme }) => {
21
21
setLoading ( false )
22
22
}
23
23
24
+ const getExtensionFromMime = ( mimeType ) => {
25
+ const mimeMap = {
26
+ 'application/zip' : 'zip' ,
27
+ 'application/pdf' : 'pdf' ,
28
+ 'image/jpeg' : 'jpg' ,
29
+ 'image/png' : 'png' ,
30
+ 'text/plain' : 'txt'
31
+ }
32
+ return mimeMap [ mimeType ] || 'zip'
33
+ }
34
+
24
35
useEffect ( ( ) => {
25
36
setLoading ( true )
26
37
getArtifacts ( )
@@ -39,7 +50,36 @@ export const ArtifactsListModal = ({ onClose, submissionId, token, theme }) => {
39
50
return (
40
51
< div className = { styles [ 'list-item' ] } >
41
52
< div className = { styles [ 'artifact-name' ] } > { item } </ div >
42
- < ReactSVG className = { styles [ 'icon-download' ] } path = { assets ( './IconSquareDownload.svg' ) } />
53
+ < ReactSVG
54
+ className = { styles [ 'icon-download' ] }
55
+ path = { assets ( './IconSquareDownload.svg' ) }
56
+ onClick = { ( ) => {
57
+ // download submission
58
+ const reactLib = getTopcoderReactLib ( )
59
+ const { getService } = reactLib . services . submissions
60
+ const submissionsService = getService ( token )
61
+ submissionsService . downloadSubmissionArtifact ( submissionId , item )
62
+ . then ( ( blob ) => {
63
+ isValidDownloadFile ( blob ) . then ( ( isValidFile ) => {
64
+ if ( isValidFile . success ) {
65
+ // eslint-disable-next-line no-undef
66
+ const blobFile = new Blob ( [ blob ] )
67
+ const url = window . URL . createObjectURL ( blobFile )
68
+ const link = document . createElement ( 'a' )
69
+ link . href = url
70
+ const extension = getExtensionFromMime ( blob . type )
71
+ const fileName = `${ submissionId } .${ extension } `
72
+ link . setAttribute ( 'download' , `${ fileName } ` )
73
+ document . body . appendChild ( link )
74
+ link . click ( )
75
+ link . parentNode . removeChild ( link )
76
+ } else {
77
+ console . log ( isValidFile , 'failed' )
78
+ }
79
+ } )
80
+ } )
81
+ } }
82
+ />
43
83
</ div >
44
84
)
45
85
} )
0 commit comments