Created
July 11, 2018 13:20
-
-
Save frel/2a03339ae52175dc2c1c5125fdc17b9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require("fs"); | |
var tus = require("tus-js-client"); | |
const ENDPOINT = "http://" | |
const FILE_NAME = "video.MOV" | |
const X_AUTH_KEY = "" | |
const file = fs.readFileSync( __dirname + "/"+ FILE_NAME) | |
if (file.err) { | |
throw err; | |
} | |
const size = file.byteLength | |
const humanSize = humanFileSize(size) | |
console.log(`Loaded file with size ${size}/${humanSize}.`) | |
var options = { | |
endpoint: ENDPOINT, | |
resume: true, | |
headers: { | |
"X-Auth-Key": X_AUTH_KEY | |
}, | |
metadata: { | |
filename: FILE_NAME, | |
}, | |
uploadSize: size, | |
onError: function (error) { | |
throw error; | |
}, | |
onProgress: function (bytesUploaded, bytesTotal) { | |
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2); | |
console.log(bytesUploaded, bytesTotal, percentage + "%"); | |
}, | |
onSuccess: function () { | |
console.log("Upload finished:", upload.url); | |
} | |
}; | |
var upload = new tus.Upload(file, options); | |
upload.start(); | |
// Utils | |
function humanFileSize(bytes) { | |
var thresh = 1024; | |
if(Math.abs(bytes) < thresh) { | |
return bytes + ' B'; | |
} | |
var units = ['kB','MB','GB','TB','PB','EB','ZB','YB'] | |
var u = -1; | |
do { | |
bytes /= thresh; | |
++u; | |
} while(Math.abs(bytes) >= thresh && u < units.length - 1); | |
return bytes.toFixed(1)+' '+units[u]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment