Skip to content

Instantly share code, notes, and snippets.

@byurhannurula
Last active April 8, 2021 12:01
Show Gist options
  • Save byurhannurula/0c08e610f782ad9567a26792d33e1002 to your computer and use it in GitHub Desktop.
Save byurhannurula/0c08e610f782ad9567a26792d33e1002 to your computer and use it in GitHub Desktop.
function handleFiles(files) {
let file = files[0];
if (file === undefined) return;
let sizeDone = 0;
let counter = 0;
let dataHash = null;
let dataOriginalHash = null;
let chunkCount = parseInt(Math.ceil(fileSize / chunkSize));
// chunkCount < 2 ? 2 : chunkCount
// Math.ceil(fileSize / chunkCount)
let percentDone = 0;
let timeStart = new Date().getTime();
let timeEnd = null;
let offset = 0;
let reader = new FileReader();
callbackOnReaderStart();
uploadFile();
function uploadFile(shouldGetOnlyHash = true) {
let chunkData = file.slice(offset, offset + chunkSize);
counter += 1;
reader.onloadend = function(event) {
if (event.target.readyState !== FileReader.DONE) return;
offset += chunkSize
sizeDone += event.target.result.byteLength;
dataHash = RECHECK.getUpdatedHashObj(event.target.result, dataHash);
percentDone = Math.floor(((sizeDone / file.size) * 100) / 2).toFixed(0);
$(".percent").css({ width: percentDone + "%" }).text(percentDone + "%");
console.log("Progress:", percentDone + "%", "counter", counter, "sizeDone", sizeDone, "chunkCount", chunkCount);
if (shouldGetOnlyHash) {
if (offset < file.size) {
console.log('Reading next chunk!');
return uploadFile();
} else {
console.log('Read last chunk!', 'Start reading and sending data');
dataHash = RECHECK.getHashFromHashObject(dataHash);
dataOriginalHash = dataHash;
dataHash = null;
// sizeDone = 0;
offset = 0;
return uploadFile(false);
}
} else {
console.log('Now reading and sending data!!');
$.ajax({
url: "/data/created",
type: 'GET',
cache: false,
dataType: 'json',
success: function(data) {
if (offset < file.size) {
return uploadFile(false);
} else {
$(".percent").css({ width: "100%" }).text("100%");
timeEnd = new Date().getTime();
dataHash = RECHECK.getHashFromHashObject(dataHash);
console.log('Completed!', "Final hash", "first hash", dataOriginalHash, "last hash", dataHash);
console.log("Elapsed time:", (timeEnd - timeStart) / 1000+' sec');
}
}
});
}
};
reader.readAsArrayBuffer(chunkData);
}
}
function handleFiles1(files) {
let file = files[0];
if (file === undefined) return;
const reorderMethod = "memory";
let dataHash = null;
let dataOriginalHash = null;
let lastOffset = 0;
let chunkReorder = 0;
let chunkTotal = 0;
let chunkCount = 0;
let counter = 0;
let index = 0;
let timeStart = new Date().getTime();
let timeEnd = null;
const onProgress = (chunkData, shouldGetOnlyHash) => {
counter += chunkData.byteLength;
dataHash = RECHECK.getUpdatedHashObj(chunkData, dataHash);
if (!shouldGetOnlyHash) {
console.log('Second read');
// TODO: Send chunkData/chunkHash/chunkCount/dataOriginalHash
}
console.log("progress", (((counter / file.size) * 100).toFixed(0) / 2).toFixed(0) + '%');
}
const onFinish = (shouldGetOnlyHash) => {
$(".percent").css({ width: "100%" }).text("100%");
timeEnd = new Date().getTime();
dataHash = RECHECK.getHashFromHashObject(dataHash);
dataOriginalHash = dataHash;
// STORE DATA
console.log("final", "100%");
console.log("final hash", dataOriginalHash);
console.log("Elapsed time:", (timeEnd - timeStart) / 1000+' sec');
console.log("chunkCount", chunkCount, "chunkReorder", chunkReorder);
console.log("shouldGetOnlyHash", shouldGetOnlyHash);
if (shouldGetOnlyHash) {
dataHash = null;
lastOffset = 0;
counter = 0;
index = 0;
// return readFile(selectedFile, onProgress, onFinish, false);
} else {
// All send
console.log("final hash", dataOriginalHash);
console.log('All send!!!');
}
alert.notify('success', 'Data uploaded successfully!');
}
readFile(selectedFile, onProgress, onFinish, true);
function readFile(file, callbackProgress, callbackFinal, shouldGetOnlyHash = false) {
let size = chunkSize;
let offset = 0;
let partial;
if (file.size === 0) callbackFinal(shouldGetOnlyHash);
chunkCount = (file.size / chunkSize).toFixed(0);
console.log("Method: ", reorderMethod);
// TODO: Limit reader count
while (offset < file.size) {
partial = file.slice(offset, offset + size);
let reader = new FileReader();
reader.size = chunkSize;
reader.offset = offset;
reader.index = index;
reader.onloadstart = function (evt) {
callbackOnReaderStart(evt);
};
reader.onprogress = function (evt) {
callbackOnReaderProgress(evt);
};
reader.onload = function (evt) {
callbackRead(this, file, evt, callbackProgress, callbackFinal, shouldGetOnlyHash);
};
reader.readAsArrayBuffer(partial);
offset += chunkSize;
index += 1;
}
}
let previous = [];
function callbackRead(reader, file, evt, callbackProgress, callbackFinal, shouldGetOnlyHash) {
if (reorderMethod === "time") {
return timeShifting()
}
return memory();
function timeShifting() {
if (lastOffset === reader.offset) {
lastOffset = reader.offset + reader.size;
callbackProgress(evt.target.result, shouldGetOnlyHash);
if (reader.offset + reader.size >= file.size) {
lastOffset = 0;
callbackFinal(shouldGetOnlyHash);
}
chunkTotal++;
} else {
setTimeout(function () {
callbackRead(reader, file, evt, callbackProgress, callbackFinal, shouldGetOnlyHash);
}, 10);
chunkReorder++;
}
}
function memory() {
chunkTotal++;
// out of order
if (lastOffset !== reader.offset){
previous.push({ offset: reader.offset, size: reader.size, result: reader.result});
chunkReorder++;
return;
}
function parseResult(offset, size, result) {
lastOffset = offset + size;
callbackProgress(result, shouldGetOnlyHash);
if (offset + size >= file.size) {
lastOffset = 0;
callbackFinal(shouldGetOnlyHash);
}
}
// in order
parseResult(reader.offset, reader.size, reader.result);
// resolve previous buffered
let buffered = [{}]
while (buffered.length > 0) {
buffered = previous.filter(function (item) {
return item.offset === lastOffset;
});
buffered.forEach(function (item) {
parseResult(item.offset, item.size, item.result);
previous.remove(item);
})
}
}
}
Array.prototype.remove = Array.prototype.remove || function(val){
let i = this.length;
while(i--){
if (this[i] === val){
this.splice(i,1);
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment