Skip to content

Commit

Permalink
fix a bunch of dirkey stuff:
Browse files Browse the repository at this point in the history
* breadcrumb navigation
* tree generation in `recvls`
* dirkeys in initial tree
  • Loading branch information
9001 committed Mar 27, 2024
1 parent 20870fd commit 32c912b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
8 changes: 6 additions & 2 deletions copyparty/httpcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,7 @@ def _chk_lastmod(self, file_ts: int) -> tuple[str, bool]:

return file_lastmod, True

def _use_dirkey(self, ap: str = "") -> bool:
def _use_dirkey(self, ap: str = "", throw: bool = False) -> bool:
if self.can_read or not self.can_get:
return False

Expand All @@ -2894,6 +2894,9 @@ def _use_dirkey(self, ap: str = "") -> bool:

t = "wrong dirkey, want %s, got %s\n vp: %s\n ap: %s"
self.log(t % (zs, req, self.req, ap), 6)
if throw:
raise Pebkac(403)

return False

def _expand(self, txt: str, phs: list[str]) -> str:
Expand Down Expand Up @@ -3605,7 +3608,8 @@ def gen_tree(self, top: str, target: str, dk: str) -> dict[str, Any]:
dk_sz = False
if dk:
vn, rem = vfs.get(top, self.uname, False, False)
if vn.flags.get("dks") and self._use_dirkey(vn.canonical(rem)):
if vn.flags.get("dks"):
self._use_dirkey(vn.canonical(rem), True)
dk_sz = vn.flags.get("dk")

dots = False
Expand Down
58 changes: 53 additions & 5 deletions copyparty/web/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,8 @@ var ACtx = !IPHONE && (window.AudioContext || window.webkitAudioContext),
ACB = sread('au_cbv') || 1,
noih = /[?&]v\b/.exec('' + location),
hash0 = location.hash,
ldks = [],
dks = {},
dk, mp;


Expand Down Expand Up @@ -5723,6 +5725,14 @@ var treectl = (function () {
};
r.nvis = r.lim;

ldks = jread('dks', []);
for (var a = ldks.length - 1; a >= 0; a--) {
var s = ldks[a],
o = s.lastIndexOf('?');

dks[s.slice(0, o)] = s.slice(o + 1);
}

function setwrap(v) {
clmod(ebi('tree'), 'nowrap', !v);
reload_tree();
Expand Down Expand Up @@ -5972,7 +5982,7 @@ var treectl = (function () {
}
ebi('treeul').setAttribute('ts', ts);

var top = top0 == '.' ? dst : top0,
var top = (top0 == '.' ? dst : top0).split('?')[0],
name = uricom_dec(top.split('/').slice(-2)[0]),
rtop = top.replace(/^\/+/, ""),
html = parsetree(res, rtop);
Expand All @@ -5989,7 +5999,7 @@ var treectl = (function () {

var links = QSA('#treeul a+a');
for (var a = 0, aa = links.length; a < aa; a++) {
if (links[a].getAttribute('href') == top) {
if (links[a].getAttribute('href').split('?')[0] == top) {
var o = links[a].parentNode;
if (!o.getElementsByTagName('li').length)
o.innerHTML = html;
Expand Down Expand Up @@ -6218,8 +6228,16 @@ var treectl = (function () {

if (!this.back) {
var dirs = [];
for (var a = 0; a < res.dirs.length; a++)
dirs.push(res.dirs[a].href.split('/')[0].split('?')[0]);
for (var a = 0; a < res.dirs.length; a++) {
var dh = res.dirs[a].href,
dn = dh.split('/')[0].split('?')[0],
m = /[?&](k=[^&]+)/.exec(dh);

if (m)
dn += '?' + m[1];

dirs.push(dn);
}

rendertree({ "a": dirs }, this.ts, ".", get_evpath() + (dk ? '?k=' + dk : ''));
}
Expand Down Expand Up @@ -6281,6 +6299,10 @@ var treectl = (function () {
if (ae = ae.querySelector('a[id]'))
cid = ae.getAttribute('id');

var m = /[?&]k=([^&]+)/.exec(location.search);
if (m)
memo_dk(top, m[1]);

r.lsc = res;
if (res.unlist) {
var ptn = new RegExp(res.unlist);
Expand Down Expand Up @@ -6430,6 +6452,30 @@ var treectl = (function () {
setTimeout(eval_hash, 1);
};

function memo_dk(vp, k) {
dks[vp] = k;
var lv = vp + "?" + k;
if (has(ldks, lv))
return;

ldks.unshift(lv);
if (ldks.length > 32) {
var keep = [], evp = get_evpath();
for (var a = 0; a < ldks.length; a++) {
var s = ldks[a];
if (evp.startsWith(s.replace(/\?[^?]+$/, '')))
keep.push(s);
}
var lim = 32 - keep.length;
for (var a = 0; a < lim; a++) {
if (!has(keep, ldks[a]))
keep.push(ldks[a])
}
ldks = keep;
}
jwrite('dks', ldks);
}

r.setlazy = function (plain) {
var html = ['<div id="plazy">', esc(plain.join(' ')), '</div>'],
all = r.lsc.files.length + r.lsc.dirs.length,
Expand Down Expand Up @@ -8207,8 +8253,10 @@ function reload_browser() {

for (var a = 0; a < parts.length - 1; a++) {
link += parts[a] + '/';
var link2 = dks[link] ? addq(link, 'k=') + dks[link] : link;

o = mknod('a');
o.setAttribute('href', link);
o.setAttribute('href', link2);
o.textContent = uricom_dec(parts[a]) || '/';
ebi('path').appendChild(mknod('i'));
ebi('path').appendChild(o);
Expand Down

0 comments on commit 32c912b

Please sign in to comment.