Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const fileUpload = (file, name, size, type, dimensions, uploadStack, contactData) => {
- return dispatch => {
- // extraindo a extensão do arquivo separando o conteúdo das barras -> contentType "image/jpeg"
- if (type) {
- var ext = type.split('/')[1];
- (ext === 'jpeg') ? ext = 'jpg' : false
- }
- // Gerando um nome único para o arquivo e combinando com a extensão extraÃda (se houver)
- const unique = uuid.v1();
- let storageName;
- (type) ? storageName = `${unique}.${ext}` : storageName = unique
- // definindo o caminho do arquivo no storage / definindo
- // os atributos 'meta' da imagem como tipo, tamanho e nome
- let directoryPath = type ? type.split('/')[0] : 'misc';
- if (!directoryPath) {
- directoryPath = 'misc';
- }
- const refPath = `chat/${contactData.userId}/${contactData.recipientId}/${contactData.companyId}/${directoryPath}/${storageName}`;
- const metadata = { contentType: type, size, name };
- // inicializando um arquivo no local com apenas 1 byte
- var bytes = new Uint8Array([0x48]);
- firebase.storage().ref(refPath).put(bytes).then(function(snapshot) {
- console.log('Initialized filesystem');
- });
- var fileUID = uuid.v1();
- // informações do upload a ser feito na pilha de upload
- const deepItem = {
- fileUID,
- file,
- name,
- size,
- type,
- progress: 0,
- state: 'waiting',
- recipientId: contactData.recipientId,
- companyId: contactData.companyId
- }
- uploadStack.push(deepItem);
- var uploadToken = '';
- uploadToken = uuid.v1();
- dispatch({ type: UPLOAD_START, payload: uploadStack, uploadToken });
- // método de upload para o fbase storage com listener de progresso
- var finish = false;
- var index = -1;
- fbase
- .storage()
- .ref(refPath)
- .putFile(file, metadata)
- .on(
- fbase.storage.TaskEvent.STATE_CHANGED,
- snapshot => {
- var progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
- index = _.findIndex(
- uploadStack, {
- recipientId: contactData.recipientId,
- companyId: contactData.companyId,
- fileUID
- });
- if (uploadStack) {
- uploadStack[index].progress = progress;
- uploadStack[index].state = snapshot.state;
- }
- uploadToken = uuid.v1();
- dispatch({ type: UPLOAD_PROGRESS, payload: uploadStack, uploadToken });
- if (snapshot.state === fbase.storage.TaskState.SUCCESS && !finish) {
- finish = true;
- uploadToken = uuid.v1();
- dispatch({ type: UPLOAD_SUCCESS, payload: uploadStack, uploadToken });
- const { userId, recipientId, serviceOrderId, companyId } = contactData;
- var isFile = true;
- if (dimensions) {
- isFile = {
- width: dimensions.width,
- height: dimensions.height,
- name,
- size
- }
- } else {
- isFile = { name, size }
- }
- sendMessage(
- dispatch,
- userId,
- recipientId,
- serviceOrderId,
- snapshot.downloadURL,
- companyId,
- isFile,
- type,
- );
- }
- },
- error => {
- unsubscribe();
- index = _.findIndex(
- uploadStack, {
- recipientId: contactData.recipientId,
- companyId: contactData.companyId,
- fileUID
- });
- if (uploadStack) {
- uploadStack[index].progress = -1;
- uploadStack[index].state = error;
- }
- uploadToken = uuid.v1();
- dispatch({ type: UPLOAD_ERROR, payload: uploadStack, uploadToken });
- alert('Ocorreu um problema ao enviar', 'Por favor, tente novamente');
- }
- );
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement