Update local frontend and backend changes
This commit is contained in:
1 parent
faca2c9a2d
commit
a27206c543
35 files changed
+1756
-334
No files matched your search
+71
@@ -252,6 +252,7 @@ class ScheduledTaskService:
|
||||
async def get_run(self, user_id: str, run_id: str) -> dict[str, Any] | None:
|
||||
run = await self.store.get_run(run_id, user_id)
|
||||
if run is not None:
|
||||
run["can_edit"] = True
|
||||
return run
|
||||
# Subscriber visibility: look up the run unfiltered, then verify the
|
||||
# caller has a subscription on the parent task.
|
||||
@@ -260,8 +261,78 @@ class ScheduledTaskService:
|
||||
return None
|
||||
if await self.store.get_subscription(candidate["task_id"], user_id) is None:
|
||||
return None
|
||||
candidate["can_edit"] = False
|
||||
return candidate
|
||||
|
||||
async def update_run_file(
|
||||
self,
|
||||
user_id: str,
|
||||
run_id: str,
|
||||
file_index: int,
|
||||
new_content: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Overwrite an archived run file in place. Owner-only.
|
||||
|
||||
Returns updated file metadata. Raises:
|
||||
- PermissionError if the caller is not the run owner (subscribers blocked)
|
||||
- FileNotFoundError if the run or file index does not exist
|
||||
- ValueError if the file is not editable (binary / not previewable)
|
||||
"""
|
||||
run = await self.store.get_run(run_id, user_id)
|
||||
if run is None:
|
||||
# Either the run doesn't exist, or the caller is a subscriber, not owner.
|
||||
candidate = await self.store.get_run_any(run_id)
|
||||
if candidate is None:
|
||||
raise FileNotFoundError(f"Run {run_id} not found")
|
||||
raise PermissionError("Only the run owner may edit result files")
|
||||
|
||||
result = run.get("result") or {}
|
||||
files = result.get("files") if isinstance(result, dict) else None
|
||||
if not isinstance(files, list) or file_index < 0 or file_index >= len(files):
|
||||
raise FileNotFoundError(f"File index {file_index} not found in run {run_id}")
|
||||
item = files[file_index]
|
||||
if not isinstance(item, dict) or not isinstance(item.get("path"), str):
|
||||
raise FileNotFoundError(f"File index {file_index} has no archive path")
|
||||
|
||||
archive_path = Path(item["path"])
|
||||
if not archive_path.exists() or not archive_path.is_file():
|
||||
raise FileNotFoundError(f"Archived file {archive_path} is no longer available")
|
||||
|
||||
# Constrain edits to text-previewable files. We mirror the same rule the
|
||||
# router uses for previews so binary files cannot be silently corrupted.
|
||||
suffix = archive_path.suffix.lower()
|
||||
mime_type = item.get("mime_type")
|
||||
is_text = (
|
||||
(isinstance(mime_type, str) and (mime_type.startswith("text/") or mime_type in {"application/json", "application/xml"}))
|
||||
or suffix in {".md", ".markdown", ".txt", ".json", ".csv", ".xml", ".yaml", ".yml", ".log"}
|
||||
)
|
||||
if not is_text:
|
||||
raise ValueError("Only text-previewable files (markdown, txt, json, etc.) can be edited")
|
||||
|
||||
# Make sure we stay inside the per-run archive root before touching disk.
|
||||
archive_root = (get_paths().base_dir / "scheduled-task-runs" / run_id).resolve()
|
||||
try:
|
||||
archive_path.resolve().relative_to(archive_root)
|
||||
except ValueError as exc:
|
||||
raise PermissionError("Refusing to edit file outside the run archive") from exc
|
||||
|
||||
archive_path.write_text(new_content, encoding="utf-8")
|
||||
new_size = archive_path.stat().st_size
|
||||
|
||||
# Reflect the new size in the run row so subsequent list/preview calls
|
||||
# report the latest figure. Other fields stay untouched.
|
||||
files[file_index] = {**item, "size": new_size}
|
||||
new_result = {**result, "files": files}
|
||||
await self.store.update_run(run_id, {"result": new_result})
|
||||
|
||||
return {
|
||||
"index": file_index,
|
||||
"name": item.get("name") or archive_path.name,
|
||||
"size": new_size,
|
||||
"mime_type": mime_type,
|
||||
"kind": item.get("kind"),
|
||||
}
|
||||
|
||||
async def delete_run(self, user_id: str, run_id: str) -> bool:
|
||||
deleted = await self.store.delete_run(run_id, user_id)
|
||||
if deleted:
|
||||
|
||||
Reference in new issue
Block a user