From 972a56e738c4d9239994f552d5de84bde2a5238a Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 11 Jun 2021 01:45:28 +0200 Subject: [PATCH] fix stuff --- copyparty/authsrv.py | 4 +- copyparty/httpcli.py | 35 ++++++++++++--- copyparty/th_srv.py | 2 +- copyparty/util.py | 6 ++- scripts/test/smoketest.py | 89 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 scripts/test/smoketest.py diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index 5c83b68b..f57dd137 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -60,7 +60,7 @@ class VFS(object): return self.nodes[name].add(src, dst) vn = VFS( - os.path.join(self.realpath, name), + os.path.join(self.realpath, name) if self.realpath else name, "{}/{}".format(self.vpath, name).lstrip("/"), self.uread, self.uwrite, @@ -92,7 +92,7 @@ class VFS(object): def bubble_flags(self): if self.dbv: - for k, v in self.dbv.flags: + for k, v in self.dbv.flags.items(): if k not in ["hist"]: self.flags[k] = v diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 346e1152..a7a18eae 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -258,7 +258,14 @@ class HttpCli(object): return "?" + "&".join(r) def redirect( - self, vpath, suf="", msg="aight", flavor="go to", click=True, use302=False + self, + vpath, + suf="", + msg="aight", + flavor="go to", + click=True, + status=200, + use302=False, ): html = self.j2( "msg", @@ -273,7 +280,7 @@ class HttpCli(object): h = {"Location": "/" + vpath, "Cache-Control": "no-cache"} self.reply(html, status=302, headers=h) else: - self.reply(html) + self.reply(html, status=status) def handle_get(self): if self.do_log: @@ -826,7 +833,7 @@ class HttpCli(object): raise Pebkac(400, "empty files in post") files.append([sz, sha512_hex, p_file, fname]) - dbv, vrem = vfs.get_dbv(vrem) + dbv, vrem = vfs.get_dbv(rem) self.conn.hsrv.broker.put( False, "up2k.hash_file", @@ -863,12 +870,16 @@ class HttpCli(object): status = "OK" if errmsg: self.log(errmsg) - errmsg = "ERROR: " + errmsg status = "ERROR" msg = "{} // {} bytes // {:.3f} MiB/s\n".format(status, sz_total, spd) jmsg = {"status": status, "sz": sz_total, "mbps": round(spd, 3), "files": []} + if errmsg: + msg += errmsg + "\n" + jmsg["error"] = errmsg + errmsg = "ERROR: " + errmsg + for sz, sha512, ofn, lfn in files: vpath = (self.vpath + "/" if self.vpath else "") + lfn msg += 'sha512: {} // {} bytes // {}\n'.format( @@ -900,11 +911,21 @@ class HttpCli(object): ft = "{}\n{}\n{}\n".format(ft, msg.rstrip(), errmsg) f.write(ft.encode("utf-8")) + status = 400 if errmsg else 200 if "j" in self.uparam: - jtxt = json.dumps(jmsg, indent=2, sort_keys=True) - self.reply(jtxt.encode("utf-8", "replace"), mime="application/json") + jtxt = json.dumps(jmsg, indent=2, sort_keys=True).encode("utf-8", "replace") + self.reply(jtxt, mime="application/json", status=status) else: - self.redirect(self.vpath, msg=msg, flavor="return to", click=False) + self.redirect( + self.vpath, + msg=msg, + flavor="return to", + click=False, + status=status, + ) + + if errmsg: + return False self.parser.drop() return True diff --git a/copyparty/th_srv.py b/copyparty/th_srv.py index b3540a15..4013058c 100644 --- a/copyparty/th_srv.py +++ b/copyparty/th_srv.py @@ -54,7 +54,7 @@ except: # https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html # ffmpeg -formats FMT_PIL = "bmp dib gif icns ico jpg jpeg jp2 jpx pcx png pbm pgm ppm pnm sgi tga tif tiff webp xbm dds xpm" -FMT_FF = "av1 asf avi flv m4v mkv mjpeg mjpg mpg mpeg mpg2 mpeg2 mov 3gp mp4 ts mpegts nut ogv ogm rm vob webm wmv" +FMT_FF = "av1 asf avi flv m4v mkv mjpeg mjpg mpg mpeg mpg2 mpeg2 h264 avc h265 hevc mov 3gp mp4 ts mpegts nut ogv ogm rm vob webm wmv" if HAVE_HEIF: FMT_PIL += " heif heifs heic heics" diff --git a/copyparty/util.py b/copyparty/util.py index 9dc8eb3a..5de00de5 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -579,8 +579,10 @@ def read_header(sr): else: continue - sr.unrecv(ret[ofs + 4 :]) - return ret[:ofs].decode("utf-8", "surrogateescape").split("\r\n") + if len(ret) > ofs + 4: + sr.unrecv(ret[ofs + 4 :]) + + return ret[:ofs].decode("utf-8", "surrogateescape").lstrip("\r\n").split("\r\n") def humansize(sz, terse=False): diff --git a/scripts/test/smoketest.py b/scripts/test/smoketest.py new file mode 100644 index 00000000..43d7a803 --- /dev/null +++ b/scripts/test/smoketest.py @@ -0,0 +1,89 @@ +import os +import sys +import time +import signal +import shutil +import tempfile +import requests +import threading +import subprocess as sp + + +class Cpp(object): + def __init__(self, args): + self.p = sp.Popen([sys.executable, "-m", "copyparty"] + args) + # , stdout=sp.PIPE, stderr=sp.PIPE) + + self.t = threading.Thread(target=self._run) + self.t.daemon = True + self.t.start() + + def _run(self): + self.so, self.se = self.p.communicate() + + def stop(self, wait): + # self.p.kill() + os.kill(self.p.pid, signal.SIGINT) + if wait: + self.t.join() + + +def main(): + ub = "http://127.0.0.1:4321/" + td = os.path.join("srv", "smoketest") + try: + shutil.rmtree(td) + except: + pass + + os.mkdir(td) + + vidp = os.path.join(tempfile.gettempdir(), "smoketest.h264") + if not os.path.exists(vidp): + cmd = "ffmpeg -f lavfi -i testsrc=48x32:3 -t 1 -c:v libx264 -tune animation -preset veryslow -crf 69" + sp.check_call(cmd.split(" ") + [vidp]) + + with open(vidp, "rb") as f: + ovid = f.read() + + args = ["-p", "4321"] + pdirs = [] + + for d1 in ["r", "w", "a"]: + pdirs.append("{}/{}".format(td, d1)) + for d2 in ["r", "w", "a"]: + d = os.path.join(td, d1, "j", d2) + pdirs.append(d.replace("\\", "/")) + os.makedirs(d) + + udirs = [x.split("/", 2)[2] for x in pdirs] + for pd, ud in zip(pdirs, udirs): + # args += ["-v", "{}:{}:{}".format(d.split("/", 1)[1], d, d[-1])] + args += ["-v", "{}:{}:{}".format(pd, ud, ud[-1])] + + cpp = Cpp(args) + + up = False + for n in range(30): + try: + time.sleep(0.1) + requests.get(ub + "?h", timeout=0.1) + up = True + break + except: + pass + + assert up + # for d in dirs: + # rd, fn = d.rsplit("/", 1) + # requests.post(ub + rd, files={"act": "mkdir", "name": fn}) + + for d in udirs: + vid = ovid + "\n{}".format(d).encode("utf-8") + requests.post(ub + d, data={"act": "bput"}, files={"f": ("a.h264", vid)}) + time.sleep(3) + cpp.stop(True) + + +if __name__ == "__main__": + main()