From 003e47a751a2d37ee3b72ea3fa76a2fbb6644782 Mon Sep 17 00:00:00 2001 From: Samir Gondzetovic Date: Sun, 28 Jan 2018 12:00:57 +0000 Subject: [PATCH 1/2] update attachment download logic --- src/api/projectAttachments.js | 7 ++++++ src/api/projects.js | 6 +++++- src/components/FileDownload.jsx | 38 +++++++++++++++++++++++++++++++++ src/config/constants.js | 2 +- src/projects/routes.jsx | 3 +++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/components/FileDownload.jsx 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 ( +
+ {!this.state.loaded ? 'Loading...':this.state.error} +
+ ) + } +} + +export default withRouter(FileDownload) \ No newline at end of file diff --git a/src/config/constants.js b/src/config/constants.js index 3008fcbd4..a7ef692d4 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -311,7 +311,7 @@ export const MAINTENANCE_MODE = false export const LS_INCOMPLETE_PROJECT = 'incompleteProject' -export const PROJECTS_API_URL = process.env.PROJECTS_API_URL || TC_API_URL +export const PROJECTS_API_URL = 'http://local.topcoder-dev.com:8001' export const CONNECT_MESSAGE_API_URL = process.env.CONNECT_MESSAGE_API_URL || TC_API_URL export const NEW_PROJECT_PATH = '/new-project' diff --git a/src/projects/routes.jsx b/src/projects/routes.jsx index b4918ef6a..b4d4aa63b 100644 --- a/src/projects/routes.jsx +++ b/src/projects/routes.jsx @@ -7,6 +7,7 @@ import Projects from './list/components/Projects/Projects' import TopBarContainer from '../components/TopBar/TopBarContainer' import ProjectsToolBar from '../components/TopBar/ProjectsToolBar' import ProjectToolBar from '../components/TopBar/ProjectToolBar' +import FileDownload from '../components/FileDownload' import ProjectDetail from './detail/ProjectDetail' import Dashboard from './detail/Dashboard' import ProjectMessages from './detail/Messages' @@ -14,6 +15,7 @@ import SpecificationContainer from './detail/containers/SpecificationContainer' import { requiresAuthentication } from '../components/AuthenticatedComponent' const ProjectLayoutWithAuth = requiresAuthentication(ProjectLayout) +const FileDownloadWithAuth = requiresAuthentication(FileDownload) // NOTE: // we cannot move up ProjectDetail component @@ -36,6 +38,7 @@ const projectRoutes = ( path="/projects" render={() => ( + , null)} /> , )} /> , )} /> From adbe21f7619f7a66b32625d656c61650f07d2545 Mon Sep 17 00:00:00 2001 From: Samir Gondzetovic Date: Sun, 28 Jan 2018 12:27:08 +0000 Subject: [PATCH 2/2] revert config change --- src/config/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/constants.js b/src/config/constants.js index a7ef692d4..3008fcbd4 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -311,7 +311,7 @@ export const MAINTENANCE_MODE = false export const LS_INCOMPLETE_PROJECT = 'incompleteProject' -export const PROJECTS_API_URL = 'http://local.topcoder-dev.com:8001' +export const PROJECTS_API_URL = process.env.PROJECTS_API_URL || TC_API_URL export const CONNECT_MESSAGE_API_URL = process.env.CONNECT_MESSAGE_API_URL || TC_API_URL export const NEW_PROJECT_PATH = '/new-project'