Skip to content

Commit

Permalink
fix(file-upload): Place file uploads directly in request body
Browse files Browse the repository at this point in the history
This change removes the use of multipart form data uploads in favor of placing the file directly in
the request body. This change is in response to catalogueglobal/datatools-server#57.
  • Loading branch information
landonreed committed Nov 8, 2017
1 parent 71b46e6 commit 92194c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
12 changes: 12 additions & 0 deletions lib/common/util/upload-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fetch from 'isomorphic-fetch'

export function uploadFile ({file, url, token}) {
return fetch(url, {
method: 'post',
headers: {
'Authorization': 'Bearer ' + token,
// 'Content-Type': 'application/zip'
},
body: file
})
}
24 changes: 10 additions & 14 deletions lib/gtfsplus/actions/gtfsplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import JSZip from 'jszip'
import fetch from 'isomorphic-fetch'
import {createAction} from 'redux-actions'

import { secureFetch } from '../../common/actions'
import { getGtfsPlusSpec } from '../../common/util/config'
import {secureFetch} from '../../common/actions'
import {getGtfsPlusSpec} from '../../common/util/config'
import {uploadFile} from '../../common/util/upload-file'
// import {stopsAndRoutes, compose} from '../../gtfs/util/graphql'
import { fetchFeedVersions } from '../../manager/actions/versions'
import {fetchFeedVersions} from '../../manager/actions/versions'

// EDIT ACTIVE GTFS+ ACTIONS

Expand Down Expand Up @@ -142,17 +143,12 @@ export function uploadGtfsPlusFeed (feedVersionId, file) {
return function (dispatch, getState) {
dispatch(uploadingGtfsPlusFeed(feedVersionId, file))
const url = `/api/manager/secure/gtfsplus/${feedVersionId}`
var data = new window.FormData()
data.append('file', file)

return fetch(url, {
method: 'post',
headers: { 'Authorization': 'Bearer ' + getState().user.token },
body: data
}).then(result => {
console.log(result)
return dispatch(uploadedGtfsPlusFeed(feedVersionId, file))
})

return uploadFile({file, url, token: getState().user.token})
.then(result => {
console.log(result)
return dispatch(uploadedGtfsPlusFeed(feedVersionId, file))
})
}
}

Expand Down
56 changes: 26 additions & 30 deletions lib/manager/actions/versions.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import fetch from 'isomorphic-fetch'
import qs from 'qs'
import S3 from 'aws-sdk/clients/s3'
import {createAction} from 'redux-actions'

import { secureFetch } from '../../common/actions'
import { getConfigProperty } from '../../common/util/config'
import {secureFetch} from '../../common/actions'
import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
import {getConfigProperty} from '../../common/util/config'
import {uploadFile} from '../../common/util/upload-file'
import fileDownload from '../../common/util/file-download'
import { setErrorMessage, startJobMonitor } from './status'
import { fetchFeedSource } from './feeds'
import {setErrorMessage, startJobMonitor} from './status'
import {fetchFeedSource} from './feeds'

export function requestingFeedVersions () {
return {
type: 'REQUESTING_FEEDVERSIONS'
}
}
export const requestingFeedVersions = createAction('REQUESTING_FEEDVERSIONS')

export function receiveFeedVersions (feedSource, feedVersions) {
return {
Expand Down Expand Up @@ -135,29 +133,27 @@ export function feedNotModified (feedSource, message) {
export function uploadFeed (feedSource, file) {
return function (dispatch, getState) {
dispatch(uploadingFeed(feedSource, file))
const url = `/api/manager/secure/feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}`
const url = `${SECURE_API_PREFIX}feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}`

var data = new window.FormData()
data.append('file', file)

return fetch(url, {
method: 'post',
headers: { 'Authorization': 'Bearer ' + getState().user.token },
body: data
}).then(res => {
if (res.status === 304) {
dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.'))
} else if (res.status >= 400) {
dispatch(setErrorMessage('Error uploading feed source'))
} else {
dispatch(uploadedFeed(feedSource))
dispatch(startJobMonitor())
}
console.log('uploadFeed result', res)
return uploadFile({file, url, token: getState().user.token})
.then(res => {
// 304 halt thrown by server if uploaded feed matches the hash of the
// latest version
if (res.status === 304) {
// Do not start job monitor if feed matches latest version. Display the
// status modal with a message about the cancelled upload.
dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.'))
} else if (res.status >= 400) {
dispatch(setErrorMessage('Error uploading feed source'))
} else {
dispatch(uploadedFeed(feedSource))
dispatch(startJobMonitor())
}
console.log('uploadFeed result', res)

// fetch feed source with versions
return dispatch(fetchFeedSource(feedSource.id, true))
})
// fetch feed source with versions
return dispatch(fetchFeedSource(feedSource.id, true))
})
}
}

Expand Down

0 comments on commit 92194c3

Please sign in to comment.