Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c41bf69

Browse files
committedFeb 25, 2025·
feat: artifact download implementation
1 parent 8f5bca9 commit c41bf69

File tree

3 files changed

+51849
-16524
lines changed

3 files changed

+51849
-16524
lines changed
 

‎package-lock.json

Lines changed: 51806 additions & 16521 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"terser": "^3.16.1",
115115
"terser-webpack-plugin": "1.1.0",
116116
"topcoder-healthcheck-dropin": "^1.0.3",
117-
"topcoder-react-lib": "^1.2.10",
117+
"topcoder-react-lib": "^1.2.16",
118118
"url-loader": "1.1.1",
119119
"webpack": "^4.43.0",
120120
"webpack-dev-server": "^3.11.0",

‎src/components/ChallengeEditor/ArtifactsListModal/index.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Modal from '../../Modal'
44
import styles from './ArtifactsListModal.module.scss'
55
import PropTypes from 'prop-types'
66
import ReactSVG from 'react-svg'
7-
import { getTopcoderReactLib } from '../../../util/topcoder-react-lib'
7+
import { getTopcoderReactLib, isValidDownloadFile } from '../../../util/topcoder-react-lib'
88
import Loader from '../../Loader'
99
const assets = require.context('../../../assets/images', false, /svg/)
1010

@@ -21,6 +21,17 @@ export const ArtifactsListModal = ({ onClose, submissionId, token, theme }) => {
2121
setLoading(false)
2222
}
2323

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+
2435
useEffect(() => {
2536
setLoading(true)
2637
getArtifacts()
@@ -39,7 +50,36 @@ export const ArtifactsListModal = ({ onClose, submissionId, token, theme }) => {
3950
return (
4051
<div className={styles['list-item']}>
4152
<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+
/>
4383
</div>
4484
)
4585
})

0 commit comments

Comments
 (0)
Please sign in to comment.