feat(api): add v2 internal files endpoint

Adds a new V2 endpoint for listing files with structured JSON response including file size and modification time.
This commit is contained in:
Benjamin Lu 2025-12-20 11:33:15 -08:00
parent 138241b406
commit 99a1484574
2 changed files with 42 additions and 1 deletions

View File

@ -76,6 +76,45 @@ class InternalRoutes:
return web.json_response([entry.name for entry in sorted_files], status=200)
def get_app(self):
if self._app is None:
self._app = web.Application()
self.setup_routes()
self._app.add_routes(self.routes)
return self._app
class InternalRoutesV2:
'''
V2 internal routes with structured responses.
'''
def __init__(self, prompt_server):
self.routes: web.RouteTableDef = web.RouteTableDef()
self._app: Optional[web.Application] = None
self.prompt_server = prompt_server
def setup_routes(self):
@self.routes.get('/files/{directory_type}')
async def get_files(request: web.Request) -> web.Response:
directory_type = request.match_info['directory_type']
directory = get_directory_for_type(directory_type)
if directory is None:
return web.json_response({"error": "Invalid directory type"}, status=400)
files = []
for entry in os.scandir(directory):
if not is_visible_file(entry):
continue
stat = entry.stat()
files.append({
"name": entry.name,
"modified": stat.st_mtime,
"size": stat.st_size
})
files.sort(key=lambda item: -item["modified"])
return web.json_response(files, status=200)
def get_app(self):
if self._app is None:
self._app = web.Application()

View File

@ -39,7 +39,7 @@ from app.model_manager import ModelFileManager
from app.custom_node_manager import CustomNodeManager
from app.subgraph_manager import SubgraphManager
from typing import Optional, Union
from api_server.routes.internal.internal_routes import InternalRoutes
from api_server.routes.internal.internal_routes import InternalRoutes, InternalRoutesV2
from protocol import BinaryEventTypes
# Import cache control middleware
@ -203,6 +203,7 @@ class PromptServer():
self.custom_node_manager = CustomNodeManager()
self.subgraph_manager = SubgraphManager()
self.internal_routes = InternalRoutes(self)
self.internal_routes_v2 = InternalRoutesV2(self)
self.supports = ["custom_nodes_from_web"]
self.prompt_queue = execution.PromptQueue(self)
self.loop = loop
@ -984,6 +985,7 @@ class PromptServer():
self.custom_node_manager.add_routes(self.routes, self.app, nodes.LOADED_MODULE_DIRS.items())
self.subgraph_manager.add_routes(self.routes, nodes.LOADED_MODULE_DIRS.items())
self.app.add_subapp('/internal', self.internal_routes.get_app())
self.app.add_subapp('/v2/internal', self.internal_routes_v2.get_app())
# Prefix every route with /api for easier matching for delegation.
# This is very useful for frontend dev server, which need to forward