Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * Read the file's content into a buffer.
  3.      * @param {Buffer} buffer - The buffer to write onto.
  4.      * @param {number} length - The maximum number of bytes to be written.
  5.      * @param {number} position - The position to start reading from.
  6.      * @returns {number} The actual number of bytes written.
  7.      */
  8.     async read(buffer, length, position) {
  9.         const apiObject = this.getApiObject();
  10.  
  11.         if (!apiObject.blob.length) {
  12.             const connector = LynApp.getConnector();
  13.             await connector.filesets.downloadBlob({
  14.                 "_id": apiObject.fileset._id,
  15.                 "fileId": apiObject._id
  16.             });
  17.         }
  18.  
  19.         const inputBuffers = apiObject.blob;
  20.  
  21.         let outputBufferOffset = 0;
  22.  
  23.         let currentInputBufferAbsoluteBegin = 0;
  24.         let currentInputBufferAbsoluteEnd = inputBuffers[0].length;
  25.  
  26.         for (let currentInputBufferIndex = 0;
  27.             currentInputBufferIndex < inputBuffers.length;
  28.             ++currentInputBufferIndex) {
  29.  
  30.                 const currentInputBuffer = inputBuffers[currentInputBufferIndex];
  31.                
  32.                 const sliceAbsoluteBegin = Math.max(position, currentInputBufferAbsoluteBegin);
  33.                 const sliceAbsoluteEnd = Math.min(position + length, currentInputBufferAbsoluteEnd);
  34.                 const sliceLength = sliceAbsoluteEnd - sliceAbsoluteBegin;
  35.  
  36.                 if (sliceLength > 0) {
  37.                     const sliceRelativeBegin = sliceAbsoluteBegin - currentInputBufferAbsoluteBegin;
  38.                     const sliceRelativeEnd = sliceAbsoluteEnd - currentInputBufferAbsoluteBegin;
  39.                     const slice = currentInputBuffer.slice(sliceRelativeBegin, sliceRelativeEnd);
  40.                    
  41.                     buffer.set(slice, outputBufferOffset);
  42.                     outputBufferOffset += sliceLength;
  43.                 }
  44.  
  45.                 currentInputBufferAbsoluteBegin = currentInputBufferAbsoluteEnd;
  46.                 currentInputBufferAbsoluteEnd += currentInputBuffer.length;
  47.         }
  48.  
  49.         return outputBufferOffset;
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement