From cda36ea8b4ed99e6c06b539515134d4a38b96f47 Mon Sep 17 00:00:00 2001 From: ed Date: Sun, 6 Jun 2021 18:47:21 +0200 Subject: [PATCH] support json replies from bput --- copyparty/httpcli.py | 23 +++++++++++++++++++++-- copyparty/th_srv.py | 2 +- scripts/make-sfx.sh | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 1554956f..41236942 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -849,14 +849,28 @@ class HttpCli(object): status = "ERROR" msg = "{} // {} bytes // {:.3f} MiB/s\n".format(status, sz_total, spd) + jmsg = {"status": status, "sz": sz_total, "mbps": round(spd, 3), "files": []} for sz, sha512, ofn, lfn in files: - vpath = self.vpath + "/" + lfn + vpath = (self.vpath + "/" if self.vpath else "") + lfn msg += 'sha512: {} // {} bytes // {}\n'.format( sha512[:56], sz, quotep(vpath), html_escape(ofn, crlf=True) ) # truncated SHA-512 prevents length extension attacks; # using SHA-512/224, optionally SHA-512/256 = :64 + jpart = { + "url": "{}://{}/{}".format( + "https" if self.tls else "http", + self.headers.get("host", "copyparty"), + vpath, + ), + "sha512": sha512[:56], + "sz": sz, + "fn": lfn, + "fn_orig": ofn, + "path": vpath, + } + jmsg["files"].append(jpart) vspd = self._spd(sz_total, False) self.log("{} {}".format(vspd, msg)) @@ -868,7 +882,12 @@ class HttpCli(object): ft = "{}\n{}\n{}\n".format(ft, msg.rstrip(), errmsg) f.write(ft.encode("utf-8")) - self.redirect(self.vpath, msg=msg, flavor="return to", click=False) + if "j" in self.uparam: + jtxt = json.dumps(jmsg, indent=2, sort_keys=True) + self.reply(jtxt.encode("utf-8", "replace"), mime="application/json") + else: + self.redirect(self.vpath, msg=msg, flavor="return to", click=False) + self.parser.drop() return True diff --git a/copyparty/th_srv.py b/copyparty/th_srv.py index 4df25ba7..75622a99 100644 --- a/copyparty/th_srv.py +++ b/copyparty/th_srv.py @@ -129,7 +129,7 @@ class ThumbSrv(object): msg = "cannot create video thumbnails because some of the required programs are not available: " msg += ", ".join(missing) - self.log(msg, c=1) + self.log(msg, c=3) t = threading.Thread(target=self.cleaner) t.daemon = True diff --git a/scripts/make-sfx.sh b/scripts/make-sfx.sh index 772b7293..4c1f3912 100755 --- a/scripts/make-sfx.sh +++ b/scripts/make-sfx.sh @@ -204,7 +204,7 @@ for d in copyparty dep-j2; do find $d -type f; done | sed -r 's/(.*)\.(.*)/\2 \1/' | LC_ALL=C sort | sed -r 's/([^ ]*) (.*)/\2.\1/' | grep -vE '/list1?$' > list1 -(grep -vE 'gz$' list1; grep -E 'gz$' list1) >list +(grep -vE '\.(gz|br)$' list1; grep -E '\.(gz|br)$' list1) >list || true echo creating tar args=(--owner=1000 --group=1000)