diff --git a/src/api/projectAttachments.js b/src/api/projectAttachments.js index 3e0f313a8..601a34e18 100644 --- a/src/api/projectAttachments.js +++ b/src/api/projectAttachments.js @@ -7,6 +7,7 @@ export function addProjectAttachment(projectId, fileData) { fileData.s3Bucket = FILE_PICKER_SUBMISSION_CONTAINER_NAME return axios.post(`${PROJECTS_API_URL}/v4/projects/${projectId}/attachments`, { param: fileData }) .then( resp => { + resp.data.result.content.downloadUrl = `/projects/${projectId}/attachments/${resp.data.result.content.id}` return _.get(resp.data, 'result.content', {}) }) } @@ -23,3 +24,9 @@ export function removeProjectAttachment(projectId, attachmentId) { return axios.delete(`${PROJECTS_API_URL}/v4/projects/${projectId}/attachments/${attachmentId}`) .then(() => attachmentId) } + +export function getProjectAttachment(projectId, attachmentId) { + return axios.get( + `${PROJECTS_API_URL}/v4/projects/${projectId}/attachments/${attachmentId}`) + .then ( resp => resp.data.result.content.url ) +} \ No newline at end of file diff --git a/src/api/projects.js b/src/api/projects.js index 53a4980d2..f634ab5ec 100644 --- a/src/api/projects.js +++ b/src/api/projects.js @@ -52,7 +52,11 @@ export function getProjectById(projectId) { projectId = parseInt(projectId) return axios.get(`${PROJECTS_API_URL}/v4/projects/${projectId}/`) .then(resp => { - return _.get(resp.data, 'result.content', {}) + const res = _.get(resp.data, 'result.content', {}) + _.forEach(res.attachments, a => { + a.downloadUrl = `/projects/${projectId}/attachments/${a.id}` + }) + return res }) } diff --git a/src/components/FileDownload.jsx b/src/components/FileDownload.jsx new file mode 100644 index 000000000..26741cde5 --- /dev/null +++ b/src/components/FileDownload.jsx @@ -0,0 +1,38 @@ +import React from 'react' +import { getProjectAttachment } from '../api/projectAttachments' +import { withRouter } from 'react-router-dom' + +class FileDownload extends React.Component { + + constructor(props) { + super(props) + this.state={loaded:false, error:null} + + } + + componentWillMount() { + this.download() + } + + download() { + const projectId = this.props.match.params.projectId + const attachmentId = this.props.match.params.attachmentId + getProjectAttachment(projectId, attachmentId).then((url) => { + window.location = url + }).catch(() => { + this.setState({loaded:true, error:'File unavailable'}) + }) + } + + render() { + + + return ( +