From 2f6707825a3c96de3ffd18a56c06781127956d78 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 10 Feb 2025 04:52:57 +0000 Subject: [PATCH] improve usb-eject hook: * don't crash internet explorer * support running as root * support old linuxen --- bin/hooks/usb-eject.js | 17 ++++++++++------- bin/hooks/usb-eject.py | 19 ++++++++++++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/bin/hooks/usb-eject.js b/bin/hooks/usb-eject.js index ad60e527..4deb2995 100644 --- a/bin/hooks/usb-eject.js +++ b/bin/hooks/usb-eject.js @@ -9,7 +9,7 @@ function eject_cb() { if (t.indexOf('can be safely unplugged') < 0 && t.indexOf('Device can be removed') < 0) return toast.err(30, 'usb eject failed:\n\n' + t); - toast.ok(5, esc(t.replace(/ - /g, '\n\n'))); + toast.ok(5, esc(t.replace(/ - /g, '\n\n')).trim()); usbclick(); setTimeout(usbclick, 10); }; @@ -19,12 +19,14 @@ function add_eject_2(a) { return; var v = aw[2], - k = 'umount_' + v; + k = 'umount_' + v, + o = ebi(k); + + if (o) + o.parentNode.removeChild(o); - qsr('#' + k); a.appendChild(mknod('span', k, '⏏'), a); - - var o = ebi(k); + o = ebi(k); o.style.cssText = 'position:absolute; right:1em; margin-top:-.2em; font-size:1.3em'; o.onclick = function (e) { ev(e); @@ -38,8 +40,9 @@ function add_eject_2(a) { }; function add_eject() { - for (var a of QSA('#treeul a[href^="/usb/"]')) - add_eject_2(a); + var o = QSA('#treeul a[href^="/usb/"]'); + for (var a = o.length - 1; a > 0; a--) + add_eject_2(o[a]); }; (function() { diff --git a/bin/hooks/usb-eject.py b/bin/hooks/usb-eject.py index 8ccb3b3c..cc24f0c1 100644 --- a/bin/hooks/usb-eject.py +++ b/bin/hooks/usb-eject.py @@ -14,13 +14,13 @@ remove those flashdrives, then boy howdy are you in the right place :D put usb-eject.js in the webroot (or somewhere else http-accessible) then run copyparty with these args: - -v /run/media/ed:/usb:A:c,hist=/tmp/junk + -v /run/media/egon:/usb:A:c,hist=/tmp/junk --xm=c1,bin/hooks/usb-eject.py --js-browser=/usb-eject.js which does the following respectively, - * share all of /run/media/ed as /usb with admin for everyone + * share all of /run/media/egon as /usb with admin for everyone and put the histpath somewhere it won't cause trouble * run the usb-eject hook with stdout redirect to the web-ui * add the complementary usb-eject.js to the browser @@ -31,15 +31,24 @@ which does the following respectively, def main(): try: label = sys.argv[1].split(":usb-eject:")[1].split(":")[0] - mp = "/run/media/ed/" + label + mp = "/run/media/egon/" + label # print("ejecting [%s]... " % (mp,), end="") mp = os.path.abspath(os.path.realpath(mp.encode("utf-8"))) st = os.lstat(mp) if not stat.S_ISDIR(st.st_mode): raise Exception("not a regular directory") - cmd = [b"gio", b"mount", b"-e", mp] - print(sp.check_output(cmd).decode("utf-8", "replace").strip()) + # if you're running copyparty as root (thx for the faith) + # you'll need something like this to make dbus talkative + cmd = b"sudo -u egon DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus gio mount -e" + + # but if copyparty and the ui-session is running + # as the same user (good) then this is plenty + cmd = b"gio mount -e" + + cmd = cmd.split(b" ") + [mp] + ret = sp.check_output(cmd).decode("utf-8", "replace") + print(ret.strip() or (label + " can be safely unplugged")) except Exception as ex: print("unmount failed: %r" % (ex,))