15 lines
19 KiB
JavaScript
15 lines
19 KiB
JavaScript
|
|
// Last time updated: 2017-08-27 5:48:35 AM UTC
|
||
|
|
|
||
|
|
// ________________
|
||
|
|
// FileBufferReader
|
||
|
|
|
||
|
|
// Open-Sourced: https://github.com/muaz-khan/FileBufferReader
|
||
|
|
|
||
|
|
// --------------------------------------------------
|
||
|
|
// Muaz Khan - www.MuazKhan.com
|
||
|
|
// MIT License - www.WebRTC-Experiment.com/licence
|
||
|
|
// --------------------------------------------------
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
"use strict";!function(){function FileBufferReader(){function fbrClone(from,to){if(null==from||"object"!=typeof from)return from;if(from.constructor!=Object&&from.constructor!=Array)return from;if(from.constructor==Date||from.constructor==RegExp||from.constructor==Function||from.constructor==String||from.constructor==Number||from.constructor==Boolean)return new from.constructor(from);to=to||new from.constructor;for(var name in from)to[name]="undefined"==typeof to[name]?fbrClone(from[name],null):to[name];return to}var fbr=this,fbrHelper=new FileBufferReaderHelper;fbr.chunks={},fbr.users={},fbr.readAsArrayBuffer=function(file,callback,extra){var options={file:file,earlyCallback:function(chunk){callback(fbrClone(chunk,{currentPosition:-1}))},extra:extra||{userid:0}};file.extra&&Object.keys(file.extra).length&&Object.keys(file.extra).forEach(function(key){options.extra[key]=file.extra[key]}),fbrHelper.readAsArrayBuffer(fbr,options)},fbr.getNextChunk=function(fileUUID,callback,userid){var currentPosition;"undefined"!=typeof fileUUID.currentPosition&&(currentPosition=fileUUID.currentPosition,fileUUID=fileUUID.uuid);var allFileChunks=fbr.chunks[fileUUID];if(allFileChunks){"undefined"!=typeof userid?(fbr.users[userid+""]||(fbr.users[userid+""]={fileUUID:fileUUID,userid:userid,currentPosition:-1}),"undefined"!=typeof currentPosition&&(fbr.users[userid+""].currentPosition=currentPosition),fbr.users[userid+""].currentPosition++,currentPosition=fbr.users[userid+""].currentPosition):("undefined"!=typeof currentPosition&&(fbr.chunks[fileUUID].currentPosition=currentPosition),fbr.chunks[fileUUID].currentPosition++,currentPosition=fbr.chunks[fileUUID].currentPosition);var nextChunk=allFileChunks[currentPosition];if(!nextChunk)return delete fbr.chunks[fileUUID],void fbr.convertToArrayBuffer({chunkMissing:!0,currentPosition:currentPosition,uuid:fileUUID},callback);nextChunk=fbrClone(nextChunk),"undefined"!=typeof userid&&(nextChunk.remoteUserId=userid+""),nextChunk.start&&fbr.onBegin(nextChunk),nextChunk.end&&fbr.onEnd(nextChunk),fbr.onProgress(nextChunk),fbr.convertToArrayBuffer(nextChunk,function(buffer){return nextChunk.currentPosition==nextChunk.maxChunks?void callback(buffer,!0):void callback(buffer,!1)})}};var fbReceiver=new FileBufferReceiver(fbr);fbr.addChunk=function(chunk,callback){chunk&&fbReceiver.receive(chunk,function(chunk){fbr.convertToArrayBuffer({readyForNextChunk:!0,currentPosition:chunk.currentPosition,uuid:chunk.uuid},callback)})},fbr.chunkMissing=function(chunk){delete fbReceiver.chunks[chunk.uuid],delete fbReceiver.chunksWaiters[chunk.uuid]},fbr.onBegin=function(){},fbr.onEnd=function(){},fbr.onProgress=function(){},fbr.convertToObject=FileConverter.ConvertToObject,fbr.convertToArrayBuffer=FileConverter.ConvertToArrayBuffer,fbr.setMultipleUsers=function(){}}function FileBufferReaderHelper(){function fileReaderWrapper(options,callback){function addChunks(fileName,binarySlice,addChunkCallback){numOfChunksInSlice=Math.ceil(binarySlice.byteLength/chunkSize);for(var i=0;i<numOfChunksInSlice;i++){var start=i*chunkSize;chunks[currentPosition]=binarySlice.slice(start,Math.min(start+chunkSize,binarySlice.byteLength)),callback({uuid:file.uuid,buffer:chunks[currentPosition],currentPosition:currentPosition,maxChunks:maxChunks,size:file.size,name:file.name,lastModifiedDate:(file.lastModifiedDate||new Date).toString(),type:file.type}),currentPosition++}currentPosition==maxChunks&&(hasEntireFile=!0),addChunkCallback()}callback=callback||function(chunk){postMessage(chunk)};var file=options.file;file.uuid||(file.uuid=(100*Math.random()).toString().replace(/\./g,""));var chunkSize=options.chunkSize||15e3;options.extra&&options.extra.chunkSize&&(chunkSize=options.extra.chunkSize);var sliceId=0,cacheSize=chunkSize,chunksPerSlice=Math.floor(Math.min(1e8,cacheSize)/chunkSize),sliceSize=chunksPerSlice*chunkSize,maxChunks=Math.ceil(file.size/chunkSize);file.maxChunks=maxChunks;var numOfChunksInSlice,hasEntireFile,currentPosition=0,chunks=[];callback({currentPosition:currentPosition,uuid:file.uuid,maxChunks:maxChunks,size:file.size,name:fi
|