-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[IMPROVEMENT] Use Rest API for file upload #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegolmello
merged 14 commits into
RocketChat:develop
from
pranavpandey1998official:fetchApi
Jun 28, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8de1ccf
removed rn-fetch-blob and use native XMLHttpRequest instead
pranavpandey1998official b3b1959
removed unnessary changes
pranavpandey1998official 5c05332
fix android bug
pranavpandey1998official 9c3bb66
fix android bug
pranavpandey1998official 8e83173
added tmid support
pranavpandey1998official b05aa22
fix bug
pranavpandey1998official dd3e88d
fixed isssue with cacel model
pranavpandey1998official cacccc1
Merge branch 'develop' into fetchApi
diegolmello 9a3e587
fix problems with audio
pranavpandey1998official a14c7e7
Merge branch 'fetchApi' of https://github.com/pranavpandey1998officia…
pranavpandey1998official 5b9e9e0
fix conflict
pranavpandey1998official ca85020
done requested changes
pranavpandey1998official beac1f2
fix bug with android
pranavpandey1998official 79ed44d
Merge branch 'develop' into fetchApi
diegolmello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,111 +1,129 @@ | ||
| import RNFetchBlob from 'rn-fetch-blob'; | ||
|
|
||
| import reduxStore from '../createStore'; | ||
| import database from '../realm'; | ||
| import log from '../../utils/log'; | ||
|
|
||
| const promises = {}; | ||
| const uploadQueue = {}; | ||
|
|
||
| function _ufsCreate(fileInfo) { | ||
| return this.sdk.methodCall('ufsCreate', fileInfo); | ||
| export function isUploadActive(path) { | ||
| return !!uploadQueue[path]; | ||
| } | ||
|
|
||
| function _ufsComplete(fileId, store, token) { | ||
| return this.sdk.methodCall('ufsComplete', fileId, store, token); | ||
| export function cancelUpload(path) { | ||
| if (uploadQueue[path]) { | ||
| uploadQueue[path].abort(); | ||
| database.write(() => { | ||
| const upload = database.objects('uploads').filtered('path = $0', path); | ||
| try { | ||
| database.delete(upload); | ||
| } catch (e) { | ||
| log('err_send_file_message_delete_upload', e); | ||
| } | ||
| }); | ||
| delete uploadQueue[path]; | ||
| } | ||
| } | ||
|
|
||
| function _sendFileMessage(rid, data, msg = {}) { | ||
| // RC 0.22.0 | ||
| return this.sdk.methodCall('sendFileMessage', rid, null, data, msg); | ||
| } | ||
| export function sendFileMessage(rid, fileInfo, tmid) { | ||
| return new Promise((resolve, reject) => { | ||
|
pranavpandey1998official marked this conversation as resolved.
|
||
| try { | ||
| const { FileUpload_MaxFileSize, Site_Url } = reduxStore.getState().settings; | ||
| const { id, token } = reduxStore.getState().login.user; | ||
|
|
||
| export function isUploadActive(path) { | ||
| return !!promises[path]; | ||
| } | ||
| // -1 maxFileSize means there is no limit | ||
| if (FileUpload_MaxFileSize > -1 && fileInfo.size > FileUpload_MaxFileSize) { | ||
| return reject({ error: 'error-file-too-large' }); // eslint-disable-line | ||
| } | ||
|
|
||
| export async function cancelUpload(path) { | ||
| if (promises[path]) { | ||
| await promises[path].cancel(); | ||
| } | ||
| } | ||
| const uploadUrl = `${ Site_Url }/api/v1/rooms.upload/${ rid }`; | ||
|
|
||
| export async function sendFileMessage(rid, fileInfo, tmid) { | ||
| try { | ||
| const data = await RNFetchBlob.wrap(fileInfo.path); | ||
| if (!fileInfo.size) { | ||
| const fileStat = await RNFetchBlob.fs.stat(fileInfo.path); | ||
| fileInfo.size = fileStat.size; | ||
| fileInfo.name = fileStat.filename; | ||
| } | ||
| const xhr = new XMLHttpRequest(); | ||
| const formData = new FormData(); | ||
|
|
||
| const { FileUpload_MaxFileSize } = reduxStore.getState().settings; | ||
| fileInfo.rid = rid; | ||
|
|
||
| // -1 maxFileSize means there is no limit | ||
| if (FileUpload_MaxFileSize > -1 && fileInfo.size > FileUpload_MaxFileSize) { | ||
| return Promise.reject({ error: 'error-file-too-large' }); // eslint-disable-line | ||
| } | ||
| database.write(() => { | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| } catch (e) { | ||
| return log('err_send_file_message_create_upload_1', e); | ||
| } | ||
| }); | ||
|
|
||
| fileInfo.rid = rid; | ||
| uploadQueue[fileInfo.path] = xhr; | ||
| xhr.open('POST', uploadUrl); | ||
|
|
||
| database.write(() => { | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| } catch (e) { | ||
| return log('err_send_file_message_create_upload_1', e); | ||
| formData.append('file', { | ||
| uri: fileInfo.path, | ||
| type: fileInfo.type, | ||
| name: fileInfo.name || 'fileMessage' | ||
| }); | ||
|
|
||
| if (fileInfo.description) { | ||
| formData.append('description', fileInfo.description); | ||
| } | ||
|
|
||
| if (tmid) { | ||
| formData.append('tmid', tmid); | ||
| } | ||
| }); | ||
|
|
||
| const result = await _ufsCreate.call(this, fileInfo); | ||
| xhr.setRequestHeader('X-Auth-Token', token); | ||
| xhr.setRequestHeader('X-User-Id', id); | ||
|
|
||
| xhr.upload.onprogress = ({ total, loaded }) => { | ||
| database.write(() => { | ||
| fileInfo.progress = Math.floor((loaded / total) * 100); | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| } catch (e) { | ||
| return log('err_send_file_message_create_upload_2', e); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| promises[fileInfo.path] = RNFetchBlob.fetch('POST', result.url, { | ||
| 'Content-Type': 'octet-stream' | ||
| }, data); | ||
| // Workaround for https://github.com/joltup/rn-fetch-blob/issues/96 | ||
| setTimeout(() => { | ||
| if (promises[fileInfo.path] && promises[fileInfo.path].uploadProgress) { | ||
| promises[fileInfo.path].uploadProgress((loaded, total) => { | ||
| xhr.onload = () => { | ||
| if (xhr.status >= 200 && xhr.status < 400) { // If response is all good... | ||
| database.write(() => { | ||
| fileInfo.progress = Math.floor((loaded / total) * 100); | ||
| const upload = database.objects('uploads').filtered('path = $0', fileInfo.path); | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| database.delete(upload); | ||
| const response = JSON.parse(xhr.response); | ||
| resolve(response); | ||
| } catch (e) { | ||
| return log('err_send_file_message_create_upload_2', e); | ||
| reject(e); | ||
| log('err_send_file_message_delete_upload', e); | ||
| } | ||
| }); | ||
| } else { | ||
| database.write(() => { | ||
| fileInfo.error = true; | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| const response = JSON.parse(xhr.response); | ||
| reject(response); | ||
| } catch (err) { | ||
| reject(err); | ||
| log('err_send_file_message_create_upload_3', err); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| xhr.onerror = (e) => { | ||
| database.write(() => { | ||
| fileInfo.error = true; | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| reject(e); | ||
| } catch (err) { | ||
| reject(err); | ||
| log('err_send_file_message_create_upload_3', err); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| await promises[fileInfo.path]; | ||
|
|
||
| const completeResult = await _ufsComplete.call(this, result.fileId, fileInfo.store, result.token); | ||
|
|
||
| await _sendFileMessage.call(this, completeResult.rid, { | ||
| _id: completeResult._id, | ||
| type: completeResult.type, | ||
| size: completeResult.size, | ||
| name: completeResult.name, | ||
| description: completeResult.description, | ||
| url: completeResult.path | ||
| }, { | ||
| tmid | ||
| }); | ||
| }; | ||
|
|
||
| database.write(() => { | ||
| const upload = database.objects('uploads').filtered('path = $0', fileInfo.path); | ||
| try { | ||
| database.delete(upload); | ||
| } catch (e) { | ||
| log('err_send_file_message_delete_upload', e); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| database.write(() => { | ||
| fileInfo.error = true; | ||
| try { | ||
| database.create('uploads', fileInfo, true); | ||
| } catch (err) { | ||
| log('err_send_file_message_create_upload_3', err); | ||
| } | ||
| }); | ||
| } | ||
| xhr.send(formData); | ||
| } catch (err) { | ||
| log('err_send_file_message_create_upload_4', err); | ||
| } | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.