Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const fileUpload = (file, name, size, type, dimensions, uploadStack, contactData) => {
  2.   return dispatch => {
  3.     // extraindo a extensão do arquivo separando o conteúdo das barras -> contentType "image/jpeg"
  4.     if (type) {
  5.       var ext = type.split('/')[1];
  6.       (ext === 'jpeg') ? ext = 'jpg' : false
  7.     }
  8.  
  9.     // Gerando um nome único para o arquivo e combinando com a extensão extraída (se houver)
  10.     const unique = uuid.v1();
  11.     let storageName;
  12.     (type) ? storageName = `${unique}.${ext}` : storageName = unique
  13.  
  14.     // definindo o caminho do arquivo no storage / definindo
  15.     // os atributos 'meta' da imagem como tipo, tamanho e nome
  16.  
  17.     let directoryPath = type ? type.split('/')[0] : 'misc';
  18.     if (!directoryPath) {
  19.       directoryPath = 'misc';
  20.     }
  21.     const refPath = `chat/${contactData.userId}/${contactData.recipientId}/${contactData.companyId}/${directoryPath}/${storageName}`;
  22.     const metadata = { contentType: type, size, name };
  23.  
  24.     // inicializando um arquivo no local com apenas 1 byte
  25.     var bytes = new Uint8Array([0x48]);
  26.     firebase.storage().ref(refPath).put(bytes).then(function(snapshot) {
  27.       console.log('Initialized filesystem');
  28.     });
  29.     var fileUID = uuid.v1();
  30.     // informações do upload a ser feito na pilha de upload
  31.     const deepItem = {
  32.       fileUID,
  33.       file,
  34.       name,
  35.       size,
  36.       type,
  37.       progress: 0,
  38.       state: 'waiting',
  39.       recipientId: contactData.recipientId,
  40.       companyId: contactData.companyId
  41.     }
  42.  
  43.     uploadStack.push(deepItem);
  44.  
  45.     var uploadToken = '';
  46.     uploadToken = uuid.v1();
  47.     dispatch({ type: UPLOAD_START, payload: uploadStack, uploadToken });
  48.     // método de upload para o fbase storage com listener de progresso
  49.     var finish = false;
  50.     var index = -1;
  51.     fbase
  52.       .storage()
  53.       .ref(refPath)
  54.       .putFile(file, metadata)
  55.       .on(
  56.         fbase.storage.TaskEvent.STATE_CHANGED,
  57.         snapshot => {
  58.           var progress = Math.round((snapshot.bytesTransferred / snapshot.totalBytes) * 100);
  59.  
  60.           index = _.findIndex(
  61.             uploadStack, {
  62.               recipientId: contactData.recipientId,
  63.               companyId: contactData.companyId,
  64.               fileUID
  65.             });
  66.  
  67.           if (uploadStack) {
  68.             uploadStack[index].progress = progress;
  69.             uploadStack[index].state = snapshot.state;
  70.           }
  71.           uploadToken = uuid.v1();
  72.           dispatch({ type: UPLOAD_PROGRESS, payload: uploadStack, uploadToken });
  73.           if (snapshot.state === fbase.storage.TaskState.SUCCESS && !finish) {
  74.             finish = true;
  75.             uploadToken = uuid.v1();
  76.  
  77.             dispatch({ type: UPLOAD_SUCCESS, payload: uploadStack, uploadToken });
  78.  
  79.             const { userId, recipientId, serviceOrderId, companyId } = contactData;
  80.             var isFile = true;
  81.  
  82.             if (dimensions) {
  83.               isFile = {
  84.                 width: dimensions.width,
  85.                 height: dimensions.height,
  86.                 name,
  87.                 size
  88.               }
  89.             } else {
  90.               isFile = { name, size }
  91.             }
  92.  
  93.             sendMessage(
  94.               dispatch,
  95.               userId,
  96.               recipientId,
  97.               serviceOrderId,
  98.               snapshot.downloadURL,
  99.               companyId,
  100.               isFile,
  101.               type,
  102.             );
  103.           }
  104.         },
  105.         error => {
  106.           unsubscribe();
  107.           index = _.findIndex(
  108.             uploadStack, {
  109.               recipientId: contactData.recipientId,
  110.               companyId: contactData.companyId,
  111.               fileUID
  112.             });
  113.           if (uploadStack) {
  114.             uploadStack[index].progress = -1;
  115.             uploadStack[index].state = error;
  116.           }
  117.           uploadToken = uuid.v1();
  118.  
  119.           dispatch({ type: UPLOAD_ERROR, payload: uploadStack, uploadToken });
  120.           alert('Ocorreu um problema ao enviar', 'Por favor, tente novamente');
  121.         }
  122.       );
  123.   }
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement