| 1 | """Lets share links with the delete right delete files, as the share's owner. |
| 2 | |
| 3 | Upstream refuses every non-unpost delete inside a share because deletion runs |
| 4 | against the source volume, so this maps each share path onto its source folder. |
| 5 | """ |
| 6 | import importlib.machinery |
| 7 | import sys |
| 8 | |
| 9 | |
| 10 | def patch(httpcli): |
| 11 | original = httpcli.HttpCli.handle_rm |
| 12 | |
| 13 | def handle_rm(self, req): |
| 14 | share = self.vn |
| 15 | if not share.shr_src or "unpost" in self.uparam or self.args.no_del: |
| 16 | return original(self, req) |
| 17 | if not req: |
| 18 | req = [self.vpath] |
| 19 | elif self.is_vproxied: |
| 20 | req = [path[len(self.args.SR):] for path in req] |
| 21 | source, root = share.shr_src |
| 22 | paths = [] |
| 23 | for path in req: |
| 24 | node, rem = self.asrv.vfs.get(path.strip("/"), self.uname, False, False, False, True) |
| 25 | if node is not share or not rem or share.shr_files and rem.split("/")[0] not in share.shr_files: |
| 26 | raise httpcli.Pebkac(403, "only files and folders inside this share can be deleted") |
| 27 | paths.append("/".join(part for part in (source.vpath, root, rem) if part)) |
| 28 | limit = int(self.uparam.get("lim") or 0) |
| 29 | result = self.conn.hsrv.broker.ask( |
| 30 | "up2k.handle_rm", share.shr_owner, self.ip, paths, [limit, limit] if limit else [], False, False) |
| 31 | self.loud_reply(result.get()) |
| 32 | return True |
| 33 | |
| 34 | httpcli.HttpCli.handle_rm = handle_rm |
| 35 | |
| 36 | |
| 37 | class Finder: |
| 38 | @staticmethod |
| 39 | def find_spec(name, path, target=None): |
| 40 | if name != "copyparty.httpcli": |
| 41 | return None |
| 42 | sys.meta_path.remove(Finder) |
| 43 | spec = importlib.machinery.PathFinder.find_spec(name, path) |
| 44 | load = spec.loader.exec_module |
| 45 | spec.loader.exec_module = lambda module: (load(module), patch(module)) |
| 46 | return spec |
| 47 | |
| 48 | |
| 49 | sys.meta_path.insert(0, Finder) |