Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 5cb94e6

Browse files
committed
fix missing file data while uploading
1 parent ed74721 commit 5cb94e6

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/actions/uploadFile.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const READ_FILE_SUCCESS = 'READ_FILE_SUCCESS'
88
export const READ_FILE_FAILURE = 'READ_FILE_FAILURE'
99
export const UPLOAD_FILE_REQUEST = 'UPLOAD_FILE_REQUEST'
1010
export const UPLOAD_FILE_SUCCESS = 'UPLOAD_FILE_SUCCESS'
11+
export const UPLOAD_FILE_PROGRESS = 'UPLOAD_FILE_PROGRESS'
1112
export const UPLOAD_FILE_FAILURE = 'UPLOAD_FILE_FAILURE'
1213

1314
export function getTempId({ assetType, category, id, fileName }) {
@@ -32,6 +33,18 @@ export function uploadFile({ id, assetType, category, file }) {
3233

3334
let tempId = getTempId(temporaryAttachment)
3435

36+
const onprogress = (res) => {
37+
const { lengthComputable, loaded, total } = res
38+
const progress = Math.round(lengthComputable ? loaded * 100 / total : 0)
39+
40+
dispatch({
41+
type: UPLOAD_FILE_PROGRESS,
42+
attachments: {
43+
[tempId]: merge({}, temporaryAttachment, { progress })
44+
}
45+
})
46+
}
47+
3548
const postUploadUrlGo = () => {
3649
const error = res => {
3750
dispatch({
@@ -48,7 +61,7 @@ export function uploadFile({ id, assetType, category, file }) {
4861
const { filePath, preSignedURL } = res.result
4962
const signedAttachment = merge({}, temporaryAttachment, { filePath, preSignedURL })
5063

51-
uploadToS3(signedAttachment)(dispatch).then(res => {
64+
uploadToS3(file, preSignedURL, onprogress)(dispatch).then(res => {
5265
postAttachment(signedAttachment)(dispatch).then(res => {
5366
if (!res.entities) { // false positive, i.e status: 500
5467
error()
@@ -74,6 +87,7 @@ export function uploadFile({ id, assetType, category, file }) {
7487
}
7588
})
7689

90+
// TODO: move read file out
7791
const readFile = () => {
7892
let reader = new FileReader()
7993

src/actions/uploadToS3.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import { getTempId } from '../actions/uploadFile'
21
import Q from 'q'
32
export { merge } from 'lodash'
43

54
export const S3_UPLOAD_REQUEST = 'S3_UPLOAD_REQUEST'
6-
export const S3_UPLOAD_PROGRESS = 'S3_UPLOAD_PROGRESS'
75
export const S3_UPLOAD_SUCCESS = 'S3_UPLOAD_SUCCESS'
86
export const S3_UPLOAD_FAILURE = 'S3_UPLOAD_FAILURE'
97

10-
export default function uploadToS3(attachment) {
8+
export default function uploadToS3(file, preSignedURL, onprogress) {
119
return dispatch => {
12-
const { data, preSignedURL, fileType, tempId } = attachment
13-
1410
let deferred = Q.defer()
1511
let putFileToS3 = new XMLHttpRequest()
1612

@@ -31,24 +27,12 @@ export default function uploadToS3(attachment) {
3127
}
3228
}
3329

34-
putFileToS3.upload.onprogress = res => {
35-
// TODO, move this out
36-
const { lengthComputable, loaded, total } = res
37-
const tempId = getTempId(attachment)
38-
const progress = Math.round(lengthComputable ? loaded * 100 / total : 0)
39-
40-
dispatch({
41-
type: S3_UPLOAD_PROGRESS,
42-
attachments: {
43-
[tempId]: merge({}, attachment, { progress })
44-
}
45-
})
46-
}
30+
putFileToS3.upload.onprogress = onprogress
4731

4832
putFileToS3.open('PUT', preSignedURL, true)
49-
putFileToS3.setRequestHeader('Content-Type', fileType)
50-
putFileToS3.send(data)
33+
putFileToS3.setRequestHeader('Content-Type', file.type)
34+
putFileToS3.send(file)
5135

5236
return deferred.promise
5337
}
54-
}
38+
}

src/reducers/attachments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import { GET_ATTACHMENTS_SUCCESS } from '../actions/getAttachments'
44
import { POST_ATTACHMENT_SUCCESS } from '../actions/postAttachment'
55
import { DELETE_ATTACHMENT_SUCCESS } from '../actions/deleteAttachment'
6-
import { S3_UPLOAD_PROGRESS } from '../actions/uploadToS3'
76
import { POST_UPLOAD_URL_FAILURE } from '../actions/postUploadUrl'
87
import {
98
UPLOAD_FILE_REQUEST,
109
READ_FILE_SUCCESS,
1110
getTempId,
12-
UPLOAD_FILE_FAILURE
11+
UPLOAD_FILE_FAILURE,
12+
UPLOAD_FILE_PROGRESS
1313
} from '../actions/uploadFile'
1414
import { merge, mapValues, omit, map } from 'lodash'
1515

@@ -18,7 +18,7 @@ export default function attachments(state = [], action) {
1818
case GET_ATTACHMENTS_SUCCESS:
1919
case UPLOAD_FILE_REQUEST:
2020
case READ_FILE_SUCCESS:
21-
case S3_UPLOAD_PROGRESS:
21+
case UPLOAD_FILE_PROGRESS:
2222
const isImageAttachments = mapValues(action.attachments, attachment => {
2323
attachment.isImage = attachment.fileType && attachment.fileType.match('image.*')
2424

0 commit comments

Comments
 (0)