-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVEMENT] Use Rest API for file upload (#1005)
* removed rn-fetch-blob and use native XMLHttpRequest instead * removed unnessary changes * fix android bug * fix android bug * added tmid support * fix bug * fixed isssue with cacel model * fix problems with audio * done requested changes * fix bug with android
- Loading branch information
1 parent
3b43cb3
commit 4814582
Showing
8 changed files
with
110 additions
and
156 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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) => { | ||
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.