(self, kind: Literal["image", "audio", "video"], media_url: str)
| 10632 | return self.video_max_bytes |
| 10633 | |
| 10634 | def _load_media_file(self, kind: Literal["image", "audio", "video"], media_url: str) -> bytes: |
| 10635 | if self.allowed_local_media_path is None: |
| 10636 | raise CompletionRequestValidationError("local media path is not allowed") |
| 10637 | parsed = urllib.parse.urlsplit(media_url) |
| 10638 | if parsed.netloc not in {"", "localhost"}: |
| 10639 | raise CompletionRequestValidationError("local media path is not allowed") |
| 10640 | path = Path(urllib.parse.unquote(parsed.path)).expanduser().resolve() |
| 10641 | try: |
| 10642 | path.relative_to(self.allowed_local_media_path) |
| 10643 | except ValueError as exc: |
| 10644 | raise CompletionRequestValidationError("local media path is not allowed") from exc |
| 10645 | max_bytes = self._max_bytes_for_media(kind) |
| 10646 | try: |
| 10647 | if path.stat().st_size > max_bytes: |
| 10648 | raise CompletionRequestValidationError(f"{kind} exceeds model.mtmd.{kind}_max_bytes") |
| 10649 | data = path.read_bytes() |
| 10650 | except OSError as exc: |
| 10651 | raise CompletionRequestValidationError(f"failed to read local {kind}: {exc}") from exc |
| 10652 | if len(data) > max_bytes: |
| 10653 | raise CompletionRequestValidationError(f"{kind} exceeds model.mtmd.{kind}_max_bytes") |
| 10654 | return data |
| 10655 | |
| 10656 | def _validate_remote_media_url(self, media_url: str) -> str: |
| 10657 | parsed = urllib.parse.urlsplit(media_url) |
no test coverage detected