diff options
author | 2022-11-21 11:25:50 -0500 | |
---|---|---|
committer | 2022-11-21 11:25:50 -0500 | |
commit | 4787a9497be4f460e028031ddad4cd9d8388b84f (patch) | |
tree | baf055b400d6fcd1e39b332f34d72c8cab73ed85 | |
parent | Remove bracket in file logging (diff) |
Refactor request file parsing
- Removed zlib compression in favor of base64 for all responses and requests
- Renamed `attachment` to `files`
- Renamed `name` to `path`
-rw-r--r-- | snekbox/api/resources/eval.py | 19 | ||||
-rw-r--r-- | snekbox/nsjail.py | 5 | ||||
-rw-r--r-- | snekbox/process.py | 2 | ||||
-rw-r--r-- | snekbox/snekio.py | 25 |
4 files changed, 18 insertions, 33 deletions
diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py index af843d2..d6549ab 100644 --- a/snekbox/api/resources/eval.py +++ b/snekbox/api/resources/eval.py @@ -33,11 +33,10 @@ class EvalResource: "items": { "type": "object", "properties": { - "name": {"type": "string"}, - "content-encoding": {"type": "string"}, + "path": {"type": "string"}, "content": {"type": "string"}, }, - "required": ["name"], + "required": ["path"], }, }, }, @@ -73,8 +72,8 @@ class EvalResource: ... "args": ["main.py"], ... "files": [ ... { - ... "name": "main.py", - ... "content": "print(1)" + ... "path": "main.py", + ... "content": "SGVsbG8...=" # Base64 ... } ... ] ... } @@ -84,13 +83,11 @@ class EvalResource: >>> { ... "stdout": "10000 loops, best of 5: 23.8 usec per loop", ... "returncode": 0, - ... "attachments": [ + ... "files": [ ... { - ... "name": "output.png", - ... "mime": "image/png", + ... "path": "output.png", ... "size": 57344, - ... "compression": "zlib", - ... "content": "eJzzSM3...=" # Base64-encoded + ... "content": "eJzzSM3...=" # Base64 ... } ... ] ... } @@ -118,5 +115,5 @@ class EvalResource: resp.media = { "stdout": result.stdout, "returncode": result.returncode, - "attachments": [atc.to_dict() for atc in result.attachments], + "files": [f.to_dict() for f in result.files], } diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index d60e511..4e9cf4d 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -31,7 +31,7 @@ def parse_files( files_pattern: str, ) -> list[FileAttachment]: """Parse files in a MemFS.""" - return sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.name) + return sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.path) class NsJail: @@ -152,7 +152,6 @@ class NsJail: self, py_args: Iterable[str], files: Iterable[FileAttachment] = (), - *, nsjail_args: Iterable[str] = (), ) -> EvalResult: """ @@ -213,7 +212,7 @@ class NsJail: # Write files if any for file in files: file.save_to(fs.home) - log.info(f"Created file at {(fs.home / file.name)!r}.") + log.info(f"Created file at {(fs.home / file.path)!r}.") msg = "Executing code..." if DEBUG: diff --git a/snekbox/process.py b/snekbox/process.py index d6d25c0..6740909 100644 --- a/snekbox/process.py +++ b/snekbox/process.py @@ -29,4 +29,4 @@ class EvalResult(CompletedProcess[_T]): ) -> None: """Create an evaluation result.""" super().__init__(args, returncode, stdout, stderr) - self.attachments: list[FileAttachment] = attachments or [] + self.files: list[FileAttachment] = attachments or [] diff --git a/snekbox/snekio.py b/snekbox/snekio.py index d3b3dce..9f2c3ca 100644 --- a/snekbox/snekio.py +++ b/snekbox/snekio.py @@ -1,7 +1,6 @@ """I/O Operations for sending / receiving files from the sandbox.""" from __future__ import annotations -import zlib from base64 import b64decode, b64encode from dataclasses import dataclass from pathlib import Path @@ -26,11 +25,11 @@ class IllegalPathError(AttachmentError): class FileAttachment(Generic[T]): """A file attachment.""" - name: str + path: str content: T @classmethod - def from_dict(cls, data: dict[str, str]) -> FileAttachment: + def from_dict(cls, data: dict[str, str]) -> FileAttachment[bytes]: """Convert a dict to an attachment.""" name = data["name"] path = Path(name) @@ -42,14 +41,7 @@ class FileAttachment(Generic[T]): if any(set(part) == {"."} for part in parts): raise IllegalPathError(f"File path '{name}' may not use traversal ('..')") - match data.get("content-encoding"): - case "base64": - content = b64decode(data["content"]) - case None | "utf-8" | "": - content = data["content"].encode("utf-8") - case _: - raise ParsingError(f"Unknown content encoding '{data['content-encoding']}'") - + content = b64decode(data.get("content", "")) return cls(name, content) @classmethod @@ -68,9 +60,9 @@ class FileAttachment(Generic[T]): return self.content return self.content.encode("utf-8") - def save_to(self, directory: Path) -> None: + def save_to(self, directory: Path | str) -> None: """Save the attachment to a path directory.""" - file = Path(directory, self.name) + file = Path(directory, self.path) # Create directories if they don't exist file.parent.mkdir(parents=True, exist_ok=True) if isinstance(self.content, str): @@ -80,12 +72,9 @@ class FileAttachment(Generic[T]): def to_dict(self) -> dict[str, str]: """Convert the attachment to a dict.""" - comp = zlib.compress(self.as_bytes()) - content = b64encode(comp).decode("ascii") - + content = b64encode(self.as_bytes()).decode("ascii") return { - "name": self.name, + "path": self.path, "size": self.size, - "content-encoding": "base64+zlib", "content": content, } |