Skip to content

Instantly share code, notes, and snippets.

@frel
Created July 11, 2018 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frel/2a03339ae52175dc2c1c5125fdc17b9f to your computer and use it in GitHub Desktop.
Save frel/2a03339ae52175dc2c1c5125fdc17b9f to your computer and use it in GitHub Desktop.
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