Skip to content

Commit 85ecf1a

Browse files
committed
feat(sandbox): add file extension attribute to SandboxFileNode and update related logic
1 parent 046aff9 commit 85ecf1a

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

api/controllers/console/sandbox_files.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SandboxFileDownloadRequest(BaseModel):
3636
"is_dir": fields.Boolean,
3737
"size": fields.Raw,
3838
"mtime": fields.Raw,
39+
"extension": fields.String,
3940
}
4041

4142

@@ -47,9 +48,7 @@ class SandboxFileDownloadRequest(BaseModel):
4748

4849

4950
sandbox_file_node_model = console_ns.model("SandboxFileNode", SANDBOX_FILE_NODE_FIELDS)
50-
sandbox_file_download_ticket_model = console_ns.model(
51-
"SandboxFileDownloadTicket", SANDBOX_FILE_DOWNLOAD_TICKET_FIELDS
52-
)
51+
sandbox_file_download_ticket_model = console_ns.model("SandboxFileDownloadTicket", SANDBOX_FILE_DOWNLOAD_TICKET_FIELDS)
5352

5453

5554
@console_ns.route("/sandboxes/<string:sandbox_id>/files")

api/core/sandbox/entities/files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class SandboxFileNode:
99
is_dir: bool
1010
size: int | None
1111
mtime: int | None
12+
extension: str | None
1213

1314

1415
@dataclass(frozen=True)

api/core/sandbox/inspector.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,19 @@ def stat_entry(full_path: str, rel_path: str) -> dict:
115115

116116
entries: list[SandboxFileNode] = []
117117
for item in raw:
118+
item_path = str(item.get("path"))
119+
item_is_dir = bool(item.get("is_dir"))
120+
extension = None
121+
if not item_is_dir:
122+
ext = os.path.splitext(item_path)[1]
123+
extension = ext or None
118124
entries.append(
119125
SandboxFileNode(
120-
path=str(item.get("path")),
121-
is_dir=bool(item.get("is_dir")),
126+
path=item_path,
127+
is_dir=item_is_dir,
122128
size=item.get("size"),
123129
mtime=item.get("mtime"),
130+
extension=extension,
124131
)
125132
)
126133
return entries
@@ -243,7 +250,9 @@ def add_dir(dir_path: str) -> None:
243250
if dir_path in ("", "."):
244251
return
245252
if dir_path not in entries_by_path:
246-
entries_by_path[dir_path] = SandboxFileNode(path=dir_path, is_dir=True, size=None, mtime=None)
253+
entries_by_path[dir_path] = SandboxFileNode(
254+
path=dir_path, is_dir=True, size=None, mtime=None, extension=None
255+
)
247256

248257
def clean(member_name: str) -> str:
249258
name = member_name.lstrip("./")
@@ -283,11 +292,16 @@ def clean(member_name: str) -> str:
283292
parent = os.path.dirname(parent)
284293

285294
is_dir = m.isdir()
295+
extension = None
296+
if not is_dir:
297+
ext = os.path.splitext(mp)[1]
298+
extension = ext or None
286299
entries_by_path[mp] = SandboxFileNode(
287300
path=mp,
288301
is_dir=is_dir,
289302
size=None if is_dir else int(m.size),
290303
mtime=int(m.mtime) if m.mtime else None,
304+
extension=extension,
291305
)
292306

293307
return sorted(entries_by_path.values(), key=lambda e: e.path)

0 commit comments

Comments
 (0)