';
var obj = mknod('tr');
obj.setAttribute('id', 'f' + nfile);
obj.innerHTML = ret;
return obj;
};
r.addrow = function (nfile) {
var tr = r.genrow(nfile);
ebi('u2tab').tBodies[0].appendChild(tr);
};
btns = QSA(btns + '>a[act]');
for (var a = 0; a < btns.length; a++) {
btns[a].onclick = function (e) {
ev(e);
var newtab = this.getAttribute('act');
function go() {
for (var b = 0; b < btns.length; b++) {
btns[b].className = (
btns[b].getAttribute('act') == newtab) ? 'act' : '';
}
r.changecard(newtab);
}
var nf = r.ctr[newtab];
if (nf === undefined)
nf = r.ctr["ok"] + r.ctr["ng"];
if (nf < 9000)
return go();
modal.confirm('about to show ' + nf + ' files\n\nthis may crash your browser, are you sure?', go, null);
};
}
r.changecard(r.act);
}
function fsearch_explain(n) {
if (n)
return toast.inf(60, 'your access to this folder is Read-Only\n\n' + (acct == '*' ? 'you are currently not logged in' : 'you are currently logged in as "' + acct + '"'));
if (bcfg_get('fsearch', false))
return toast.inf(60, 'you are currently in file-search mode\n\nswitch to upload-mode by clicking the green magnifying glass (next to the big yellow search button), and then refresh\n\nsorry');
return toast.inf(60, 'refresh the page and try again, it should work now');
}
function up2k_init(subtle) {
function showmodal(msg) {
ebi('u2notbtn').innerHTML = msg;
ebi('u2btn').style.display = 'none';
ebi('u2notbtn').style.display = 'block';
ebi('u2conf').style.opacity = '0.5';
}
function unmodal() {
ebi('u2notbtn').style.display = 'none';
ebi('u2btn').style.display = 'block';
ebi('u2conf').style.opacity = '1';
ebi('u2notbtn').innerHTML = '';
}
var suggest_up2k = 'this is the basic uploader; up2k is better';
var shame = 'your browser disables sha512 unless you use https',
is_https = (window.location + '').indexOf('https:') === 0;
if (is_https)
// chrome<37 firefox<34 edge<12 opera<24 safari<7
shame = 'your browser is impressively ancient';
var got_deps = false;
function init_deps() {
if (!got_deps && !subtle && !window.asmCrypto) {
var fn = 'sha512.' + sha_js + '.js';
showmodal('
loading ' + fn + '
since ' + shame + '
thanks chrome
');
import_js('/.cpr/deps/' + fn, unmodal);
if (is_https)
ebi('u2foot').innerHTML = shame + ' so this uploader will do like 500 KiB/s at best';
else
ebi('u2foot').innerHTML = 'seems like ' + shame + ' so do that if you want more performance (expecting 20' : '8a5">(but dont worry too much, expect 100') + ' MiB/s)';
}
got_deps = true;
}
if (perms.length && !has(perms, 'read'))
goto('up2k');
function setmsg(msg, type) {
if (msg !== undefined) {
ebi('u2err').setAttribute('class', type);
ebi('u2err').innerHTML = msg;
}
else {
ebi('u2err').setAttribute('class', '');
ebi('u2err').innerHTML = '';
}
if (msg == suggest_up2k) {
ebi('u2yea').onclick = function (e) {
ev(e);
goto('up2k');
};
}
}
function un2k(msg) {
setmsg(msg, 'err');
return false;
}
ebi('u2nope').onclick = function (e) {
ev(e);
setmsg(suggest_up2k, 'msg');
goto('bup');
};
setmsg(suggest_up2k, 'msg');
if (!String.prototype.format) {
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined' ?
args[number] : match;
});
};
}
var parallel_uploads = icfg_get('nthread'),
multitask = bcfg_get('multitask', true),
ask_up = bcfg_get('ask_up', true),
flag_en = bcfg_get('flag_en', false),
fsearch = bcfg_get('fsearch', false),
turbo = bcfg_get('u2turbo', false),
datechk = bcfg_get('u2tdate', true),
fdom_ctr = 0,
min_filebuf = 0;
var st = {
"files": [],
"todo": {
"head": [],
"hash": [],
"handshake": [],
"upload": []
},
"busy": {
"head": [],
"hash": [],
"handshake": [],
"upload": []
},
"bytes": {
"total": 0,
"hashed": 0,
"uploaded": 0,
"finished": 0
},
"time": {
"hashing": 0,
"uploading": 0,
"busy": 0
}
};
function push_t(arr, t) {
var sort = arr.length && arr[arr.length - 1].n > t.n;
arr.push(t);
if (sort)
arr.sort(function (a, b) {
return a.n < b.n ? -1 : 1;
});
}
var pvis = new U2pvis("bz", '#u2cards');
var bobslice = null;
if (window.File)
bobslice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
if (!bobslice || !window.FileReader || !window.FileList)
return un2k("this is the basic uploader; up2k needs at least chrome 21 // firefox 13 // edge 12 // opera 12 // safari 5.1");
var flag = false;
apply_flag_cfg();
set_fsearch();
function nav() {
ebi('file' + fdom_ctr).click();
}
ebi('u2btn').addEventListener('click', nav, false);
function ondrag(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
e.dataTransfer.effectAllowed = 'copy';
}
ebi('u2btn').addEventListener('dragover', ondrag, false);
ebi('u2btn').addEventListener('dragenter', ondrag, false);
function gotfile(e) {
e.stopPropagation();
e.preventDefault();
var files,
is_itemlist = false;
if (e.dataTransfer) {
if (e.dataTransfer.items) {
files = e.dataTransfer.items; // DataTransferItemList
is_itemlist = true;
}
else files = e.dataTransfer.files; // FileList
}
else files = e.target.files;
if (!files || !files.length)
return toast.err(0, 'no files selected??');
more_one_file();
var bad_files = [],
good_files = [],
dirs = [];
for (var a = 0; a < files.length; a++) {
var fobj = files[a];
if (is_itemlist) {
if (fobj.kind !== 'file')
continue;
try {
var wi = fobj.webkitGetAsEntry();
if (wi.isDirectory) {
dirs.push(wi);
continue;
}
}
catch (ex) { }
fobj = fobj.getAsFile();
}
try {
if (fobj.size < 1)
throw 1;
}
catch (ex) {
bad_files.push(fobj.name);
continue;
}
good_files.push([fobj, fobj.name]);
}
if (dirs) {
return read_dirs(null, [], dirs, good_files, bad_files);
}
}
function rd_flatten(pf, dirs) {
var ret = jcp(pf);
for (var a = 0; a < dirs.length; a++)
ret.push(dirs.fullPath || '');
ret.sort();
return ret;
}
var rd_missing_ref = [];
function read_dirs(rd, pf, dirs, good, bad, spins) {
spins = spins || 0;
if (++spins == 5)
rd_missing_ref = rd_flatten(pf, dirs);
if (spins == 200) {
var missing = rd_flatten(pf, dirs),
match = rd_missing_ref.length == missing.length,
aa = match ? missing.length : 0;
missing.sort();
for (var a = 0; a < aa; a++)
if (rd_missing_ref[a] != missing[a])
match = false;
if (match) {
var msg = ['directory iterator got stuck on the following {0} items; good chance your browser is about to spinlock:
'.format(missing.length)];
for (var a = 0; a < Math.min(20, missing.length); a++)
msg.push('
' + esc(missing[a]) + '
');
return modal.alert(msg.join('') + '
', function () {
read_dirs(rd, [], [], good, bad, spins);
});
}
spins = 0;
}
if (!dirs.length) {
if (!pf.length)
return gotallfiles(good, bad);
console.log("retry pf, " + pf.length);
setTimeout(function () {
read_dirs(rd, pf, dirs, good, bad, spins);
}, 50);
return;
}
if (!rd)
rd = dirs[0].createReader();
rd.readEntries(function (ents) {
var ngot = 0;
ents.forEach(function (dn) {
if (dn.isDirectory) {
dirs.push(dn);
}
else {
var name = dn.fullPath;
if (name.indexOf('/') === 0)
name = name.slice(1);
pf.push(name);
dn.file(function (fobj) {
apop(pf, name);
try {
if (fobj.size > 0) {
good.push([fobj, name]);
return;
}
}
catch (ex) { }
bad.push(name);
});
}
ngot += 1;
});
if (!ngot) {
dirs.shift();
rd = null;
}
return read_dirs(rd, pf, dirs, good, bad, spins);
});
}
function gotallfiles(good_files, bad_files) {
if (bad_files.length) {
var ntot = bad_files.length + good_files.length,
msg = 'These {0} files (of {1} total) were skipped because they are empty:\n'.format(bad_files.length, ntot);
for (var a = 0, aa = Math.min(20, bad_files.length); a < aa; a++)
msg += '-- ' + bad_files[a] + '\n';
if (good_files.length - bad_files.length <= 1 && ANDROID)
msg += '\nFirefox-Android has a bug which prevents selecting multiple files. Try selecting one file at a time. For more info, see firefox bug 1456557';
return modal.alert(msg, function () {
gotallfiles(good_files, []);
});
}
good_files.sort(function (a, b) {
a = a[1];
b = b[1];
return a < b ? -1 : a > b ? 1 : 0;
});
var msg = ['{0} these {1} files?
'.format(fsearch ? 'search' : 'upload', good_files.length)];
for (var a = 0, aa = Math.min(20, good_files.length); a < aa; a++)
msg.push('
' + esc(good_files[a][1]) + '
');
if (ask_up && !fsearch)
return modal.confirm(msg.join('') + '
', function () { up_them(good_files); }, null);
up_them(good_files);
}
function up_them(good_files) {
var seen = {},
evpath = get_evpath(),
draw_each = good_files.length < 50;
for (var a = 0; a < st.files.length; a++)
seen[st.files[a].name + '\n' + st.files[a].size] = 1;
for (var a = 0; a < good_files.length; a++) {
var fobj = good_files[a][0],
name = good_files[a][1],
fdir = evpath,
now = Date.now(),
lmod = fobj.lastModified || now,
ofs = name.lastIndexOf('/') + 1;
if (ofs) {
fdir += url_enc(name.slice(0, ofs));
name = name.slice(ofs);
}
var entry = {
"n": st.files.length,
"t0": now,
"fobj": fobj,
"name": name,
"size": fobj.size,
"lmod": lmod / 1000,
"purl": fdir,
"done": false,
"hash": []
},
key = entry.name + '\n' + entry.size;
if (fsearch)
entry.srch = 1;
if (seen[key])
continue;
seen[key] = 1;
pvis.addfile([
fsearch ? esc(entry.name) : linksplit(
uricom_dec(entry.purl)[0] + entry.name).join(' '),
'📐 hash',
''
], fobj.size, draw_each);
st.bytes.total += fobj.size;
st.files.push(entry);
if (turbo)
push_t(st.todo.head, entry);
else
push_t(st.todo.hash, entry);
}
if (!draw_each) {
pvis.drawcard("q");
pvis.changecard(pvis.act);
}
}
ebi('u2btn').addEventListener('drop', gotfile, false);
function more_one_file() {
fdom_ctr++;
var elm = mknod('div');
elm.innerHTML = ''.format(fdom_ctr);
ebi('u2form').appendChild(elm);
ebi('file' + fdom_ctr).addEventListener('change', gotfile, false);
}
more_one_file();
function u2cleanup(e) {
ev(e);
for (var a = 0; a < st.files.length; a++) {
var t = st.files[a];
if (t.done && t.name) {
var tr = ebi('f' + t.n);
if (!tr)
continue;
tr.parentNode.removeChild(tr);
t.name = undefined;
}
}
}
ebi('u2cleanup').onclick = u2cleanup;
var etaref = 0, etaskip = 0;
function etafun() {
var nhash = st.busy.head.length + st.busy.hash.length + st.todo.head.length + st.todo.hash.length,
nsend = st.busy.upload.length + st.todo.upload.length,
now = Date.now(),
td = (now - (etaref || now)) / 1000.0;
etaref = now;
if (td > 1.2)
td = 0.05;
//ebi('acc_info').innerHTML = humantime(st.time.busy) + ' ' + f2f(now / 1000, 1);
if (!nhash)
ebi('u2etah').innerHTML = 'Done ({0}, {1} files)'.format(humansize(st.bytes.hashed), pvis.ctr["ok"] + pvis.ctr["ng"]);
if (!nsend && !nhash)
ebi('u2etau').innerHTML = ebi('u2etat').innerHTML = (
'Done ({0}, {1} files)'.format(humansize(st.bytes.uploaded), pvis.ctr["ok"] + pvis.ctr["ng"]));
if (!st.busy.hash.length && !hashing_permitted())
nhash = 0;
if (!parallel_uploads || !(nhash + nsend) || (flag && flag.owner && !flag.ours))
return;
var t = [];
if (nhash) {
st.time.hashing += td;
t.push(['u2etah', st.bytes.hashed, st.bytes.hashed, st.time.hashing]);
if (fsearch)
t.push(['u2etat', st.bytes.hashed, st.bytes.hashed, st.time.hashing]);
}
if (nsend) {
st.time.uploading += td;
t.push(['u2etau', st.bytes.uploaded, st.bytes.finished, st.time.uploading]);
}
if ((nhash || nsend) && !fsearch) {
if (!st.bytes.finished) {
ebi('u2etat').innerHTML = '(preparing to upload)';
}
else {
st.time.busy += td;
t.push(['u2etat', st.bytes.finished, st.bytes.finished, st.time.busy]);
}
}
for (var a = 0; a < t.length; a++) {
var rem = st.bytes.total - t[a][2],
bps = t[a][1] / t[a][3],
eta = Math.floor(rem / bps);
if (t[a][1] < 1024 || t[a][3] < 0.1) {
ebi(t[a][0]).innerHTML = '(preparing to upload)';
continue;
}
if (etaskip)
continue;
ebi(t[a][0]).innerHTML = '{0}, {1}/s, {2}'.format(
humansize(rem), humansize(bps, 1), humantime(eta));
}
if (++etaskip > 2)
etaskip = 0;
}
/////
////
/// actuator
//
function handshakes_permitted() {
if (!st.todo.handshake.length)
return true;
var t = st.todo.handshake[0],
cd = t.cooldown;
if (cd && cd - Date.now() > 0)
return false;
// keepalive or verify
if (t.keepalive ||
t.t_uploaded)
return true;
if (parallel_uploads <
st.busy.handshake.length)
return false;
if (st.busy.handshake.length)
for (var n = t.n - 1; n >= t.n - parallel_uploads && n >= 0; n--)
if (st.files[n].t_uploading)
return false;
if ((multitask ? 1 : 0) <
st.todo.upload.length +
st.busy.upload.length)
return false;
return true;
}
function hashing_permitted() {
if (!parallel_uploads)
return false;
if (multitask) {
var ahead = st.bytes.hashed - st.bytes.finished;
return ahead < 1024 * 1024 * 1024 * 4 &&
st.todo.handshake.length + st.busy.handshake.length < 16;
}
return handshakes_permitted() && 0 ==
st.todo.handshake.length +
st.busy.handshake.length +
st.todo.upload.length +
st.busy.upload.length;
}
var tasker = (function () {
var running = false,
was_busy = false;
function defer() {
running = false;
}
function taskerd() {
if (running)
return;
if (crashed)
return defer();
running = true;
while (true) {
var now = Date.now(),
is_busy = 0 !=
st.todo.head.length +
st.todo.hash.length +
st.todo.handshake.length +
st.todo.upload.length +
st.busy.head.length +
st.busy.hash.length +
st.busy.handshake.length +
st.busy.upload.length;
if (was_busy != is_busy) {
was_busy = is_busy;
window[(is_busy ? "add" : "remove") +
"EventListener"]("beforeunload", warn_uploader_busy);
if (!is_busy) {
var k = fsearch ? 'searches' : 'uploads',
ks = fsearch ? 'Search' : 'Upload',
tok = fsearch ? 'successful (found on server)' : 'completed successfully',
tng = fsearch ? 'failed (NOT found on server)' : 'failed, sorry',
ok = pvis.ctr["ok"],
ng = pvis.ctr["ng"],
t = ask_up ? 0 : 10;
if (ok && ng)
toast.warn(t, 'Finished, but some {0} failed:\n{1} {2},\n{3} {4}'.format(k, ok, tok, ng, tng));
else if (ok > 1)
toast.ok(t, 'All {1} {0} {2}'.format(k, ok, tok));
else if (ok)
toast.ok(t, '{0} {1}'.format(ks, tok));
else if (ng > 1)
toast.err(t, 'All {1} {0} {2}'.format(k, ng, tng));
else if (ng)
toast.err(t, '{0} {1}'.format(ks, tng));
timer.rm(etafun);
}
else {
timer.add(etafun, false);
ebi('u2etas').style.textAlign = 'left';
}
etafun();
}
if (flag) {
if (is_busy) {
flag.take(now);
ebi('u2flagblock').style.display = flag.ours ? '' : 'block';
if (!flag.ours)
return defer();
}
else if (flag.ours) {
flag.give();
}
}
var mou_ikkai = false;
if (st.busy.handshake.length &&
st.busy.handshake[0].t_busied < now - 30 * 1000
) {
console.log("retrying stuck handshake");
var t = st.busy.handshake.shift();
st.todo.handshake.unshift(t);
}
var nprev = -1;
for (var a = 0; a < st.todo.upload.length; a++) {
var nf = st.todo.upload[a].nfile;
if (nprev == nf)
continue;
nprev = nf;
var t = st.files[nf];
if (now - t.t_busied > 1000 * 30 &&
now - t.t_handshake > 1000 * (21600 - 1800)
) {
apop(st.todo.handshake, t);
st.todo.handshake.unshift(t);
t.keepalive = true;
}
}
if (st.todo.head.length &&
st.busy.head.length < parallel_uploads) {
exec_head();
mou_ikkai = true;
}
if (handshakes_permitted() &&
st.todo.handshake.length) {
exec_handshake();
mou_ikkai = true;
}
if (st.todo.upload.length &&
st.busy.upload.length < parallel_uploads) {
exec_upload();
mou_ikkai = true;
}
if (hashing_permitted() &&
st.todo.hash.length &&
!st.busy.hash.length) {
exec_hash();
mou_ikkai = true;
}
if (!mou_ikkai || crashed)
return defer();
}
}
timer.add(taskerd, true);
return taskerd;
})();
/////
////
/// hashing
//
// https://gist.github.com/jonleighton/958841
function buf2b64(arrayBuffer) {
var base64 = '',
cset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
src = new Uint8Array(arrayBuffer),
nbytes = src.byteLength,
byteRem = nbytes % 3,
mainLen = nbytes - byteRem,
a, b, c, d, chunk;
for (var i = 0; i < mainLen; i = i + 3) {
chunk = (src[i] << 16) | (src[i + 1] << 8) | src[i + 2];
// create 8*3=24bit segment then split into 6bit segments
a = (chunk & 16515072) >> 18; // (2^6 - 1) << 18
b = (chunk & 258048) >> 12; // (2^6 - 1) << 12
c = (chunk & 4032) >> 6; // (2^6 - 1) << 6
d = chunk & 63; // 2^6 - 1
// Convert the raw binary segments to the appropriate ASCII encoding
base64 += cset[a] + cset[b] + cset[c] + cset[d];
}
if (byteRem == 1) {
chunk = src[mainLen];
a = (chunk & 252) >> 2; // (2^6 - 1) << 2
b = (chunk & 3) << 4; // 2^2 - 1 (zero 4 LSB)
base64 += cset[a] + cset[b];//+ '==';
}
else if (byteRem == 2) {
chunk = (src[mainLen] << 8) | src[mainLen + 1];
a = (chunk & 64512) >> 10; // (2^6 - 1) << 10
b = (chunk & 1008) >> 4; // (2^6 - 1) << 4
c = (chunk & 15) << 2; // 2^4 - 1 (zero 2 LSB)
base64 += cset[a] + cset[b] + cset[c];//+ '=';
}
return base64;
}
function hex2u8(txt) {
return new Uint8Array(txt.match(/.{2}/g).map(function (b) { return parseInt(b, 16); }));
}
function get_chunksize(filesize) {
var chunksize = 1024 * 1024,
stepsize = 512 * 1024;
while (true) {
for (var mul = 1; mul <= 2; mul++) {
var nchunks = Math.ceil(filesize / chunksize);
if (nchunks <= 256 || chunksize >= 32 * 1024 * 1024)
return chunksize;
chunksize += stepsize;
stepsize *= mul;
}
}
}
function exec_hash() {
var t = st.todo.hash.shift();
st.busy.hash.push(t);
t.bytes_uploaded = 0;
var bpend = 0,
nchunk = 0,
chunksize = get_chunksize(t.size),
nchunks = Math.ceil(t.size / chunksize),
hashtab = {};
pvis.setab(t.n, nchunks);
pvis.move(t.n, 'bz');
var segm_next = function () {
if (nchunk >= nchunks || (bpend > chunksize && bpend >= min_filebuf))
return false;
var reader = new FileReader(),
nch = nchunk++,
car = nch * chunksize,
cdr = car + chunksize,
t0 = Date.now();
if (cdr >= t.size)
cdr = t.size;
bpend += cdr - car;
st.bytes.hashed += cdr - car;
function orz(e) {
if (!min_filebuf && nch == 1) {
min_filebuf = 1;
var td = Date.now() - t0;
if (td > 50) {
ebi('u2foot').innerHTML += "