Skip to content

Commit 92194c3

Browse files
committed
fix(file-upload): Place file uploads directly in request body
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.
1 parent 71b46e6 commit 92194c3

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

lib/common/util/upload-file.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fetch from 'isomorphic-fetch'
2+
3+
export function uploadFile ({file, url, token}) {
4+
return fetch(url, {
5+
method: 'post',
6+
headers: {
7+
'Authorization': 'Bearer ' + token,
8+
// 'Content-Type': 'application/zip'
9+
},
10+
body: file
11+
})
12+
}

lib/gtfsplus/actions/gtfsplus.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import JSZip from 'jszip'
22
import fetch from 'isomorphic-fetch'
33
import {createAction} from 'redux-actions'
44

5-
import { secureFetch } from '../../common/actions'
6-
import { getGtfsPlusSpec } from '../../common/util/config'
5+
import {secureFetch} from '../../common/actions'
6+
import {getGtfsPlusSpec} from '../../common/util/config'
7+
import {uploadFile} from '../../common/util/upload-file'
78
// import {stopsAndRoutes, compose} from '../../gtfs/util/graphql'
8-
import { fetchFeedVersions } from '../../manager/actions/versions'
9+
import {fetchFeedVersions} from '../../manager/actions/versions'
910

1011
// EDIT ACTIVE GTFS+ ACTIONS
1112

@@ -142,17 +143,12 @@ export function uploadGtfsPlusFeed (feedVersionId, file) {
142143
return function (dispatch, getState) {
143144
dispatch(uploadingGtfsPlusFeed(feedVersionId, file))
144145
const url = `/api/manager/secure/gtfsplus/${feedVersionId}`
145-
var data = new window.FormData()
146-
data.append('file', file)
147-
148-
return fetch(url, {
149-
method: 'post',
150-
headers: { 'Authorization': 'Bearer ' + getState().user.token },
151-
body: data
152-
}).then(result => {
153-
console.log(result)
154-
return dispatch(uploadedGtfsPlusFeed(feedVersionId, file))
155-
})
146+
147+
return uploadFile({file, url, token: getState().user.token})
148+
.then(result => {
149+
console.log(result)
150+
return dispatch(uploadedGtfsPlusFeed(feedVersionId, file))
151+
})
156152
}
157153
}
158154

lib/manager/actions/versions.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import fetch from 'isomorphic-fetch'
22
import qs from 'qs'
33
import S3 from 'aws-sdk/clients/s3'
4+
import {createAction} from 'redux-actions'
45

5-
import { secureFetch } from '../../common/actions'
6-
import { getConfigProperty } from '../../common/util/config'
6+
import {secureFetch} from '../../common/actions'
77
import {GTFS_GRAPHQL_PREFIX, SECURE_API_PREFIX} from '../../common/constants'
8+
import {getConfigProperty} from '../../common/util/config'
9+
import {uploadFile} from '../../common/util/upload-file'
810
import fileDownload from '../../common/util/file-download'
9-
import { setErrorMessage, startJobMonitor } from './status'
10-
import { fetchFeedSource } from './feeds'
11+
import {setErrorMessage, startJobMonitor} from './status'
12+
import {fetchFeedSource} from './feeds'
1113

12-
export function requestingFeedVersions () {
13-
return {
14-
type: 'REQUESTING_FEEDVERSIONS'
15-
}
16-
}
14+
export const requestingFeedVersions = createAction('REQUESTING_FEEDVERSIONS')
1715

1816
export function receiveFeedVersions (feedSource, feedVersions) {
1917
return {
@@ -135,29 +133,27 @@ export function feedNotModified (feedSource, message) {
135133
export function uploadFeed (feedSource, file) {
136134
return function (dispatch, getState) {
137135
dispatch(uploadingFeed(feedSource, file))
138-
const url = `/api/manager/secure/feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}`
136+
const url = `${SECURE_API_PREFIX}feedversion?feedSourceId=${feedSource.id}&lastModified=${file.lastModified}`
139137

140-
var data = new window.FormData()
141-
data.append('file', file)
142-
143-
return fetch(url, {
144-
method: 'post',
145-
headers: { 'Authorization': 'Bearer ' + getState().user.token },
146-
body: data
147-
}).then(res => {
148-
if (res.status === 304) {
149-
dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.'))
150-
} else if (res.status >= 400) {
151-
dispatch(setErrorMessage('Error uploading feed source'))
152-
} else {
153-
dispatch(uploadedFeed(feedSource))
154-
dispatch(startJobMonitor())
155-
}
156-
console.log('uploadFeed result', res)
138+
return uploadFile({file, url, token: getState().user.token})
139+
.then(res => {
140+
// 304 halt thrown by server if uploaded feed matches the hash of the
141+
// latest version
142+
if (res.status === 304) {
143+
// Do not start job monitor if feed matches latest version. Display the
144+
// status modal with a message about the cancelled upload.
145+
dispatch(feedNotModified(feedSource, 'Feed upload cancelled because it matches latest feed version.'))
146+
} else if (res.status >= 400) {
147+
dispatch(setErrorMessage('Error uploading feed source'))
148+
} else {
149+
dispatch(uploadedFeed(feedSource))
150+
dispatch(startJobMonitor())
151+
}
152+
console.log('uploadFeed result', res)
157153

158-
// fetch feed source with versions
159-
return dispatch(fetchFeedSource(feedSource.id, true))
160-
})
154+
// fetch feed source with versions
155+
return dispatch(fetchFeedSource(feedSource.id, true))
156+
})
161157
}
162158
}
163159

0 commit comments

Comments
 (0)