22import logging
33import os
44import re
5+ from hashlib import sha256
56from pathlib import Path
7+ from typing import Any , Dict
68
79from fastapi import APIRouter , Body , Depends , Header , HTTPException , status
810from starlette .responses import HTMLResponse , JSONResponse , RedirectResponse , Response
@@ -112,12 +114,12 @@ async def get_all_workflows():
112114 stat_info = json_file .stat ()
113115 try :
114116 with json_file .open ("r" , encoding = "utf-8" ) as f :
115- config_contents = json .load (f )
117+ config_contents : Dict [ str , Any ] = json .load (f )
116118 except json .JSONDecodeError as e :
117119 logger .error (f"Error decoding JSON from { json_file } : { e } " )
118120 continue
119121
120- data [json_file .stem ] = {
122+ data [config_contents . get ( "id" , json_file .stem ) ] = {
121123 "createTime" : int (stat_info .st_ctime ),
122124 "updateTime" : int (stat_info .st_mtime ),
123125 "config" : config_contents ,
@@ -139,7 +141,8 @@ async def get_workflow(workflow_id: str):
139141 if not re .match (r"^[\w\-]+$" , workflow_id ):
140142 return JSONResponse ({"error" : "invalid id" }, status_code = HTTP_400_BAD_REQUEST )
141143
142- file_path = workflow_local_dir / f"{ workflow_id } .json"
144+ workflow_hash = sha256 (workflow_id .encode ()).hexdigest ()
145+ file_path = workflow_local_dir / f"{ workflow_hash } .json"
143146 if not file_path .exists ():
144147 return JSONResponse ({"error" : "not found" }, status_code = HTTP_404_NOT_FOUND )
145148
@@ -148,7 +151,7 @@ async def get_workflow(workflow_id: str):
148151 with file_path .open ("r" , encoding = "utf-8" ) as f :
149152 config_contents = json .load (f )
150153 except json .JSONDecodeError as e :
151- logger .error (f"Error reading JSON from { file_path } : { e } " )
154+ logger .error (f"Error reading JSON for { workflow_id } from ' { file_path } ' : { e } " )
152155 return JSONResponse ({"error" : "invalid JSON" }, status_code = 500 )
153156
154157 return Response (
@@ -179,32 +182,34 @@ async def create_or_overwrite_workflow(
179182 if not re .match (r"^[\w\-]+$" , workflow_id ):
180183 return JSONResponse ({"error" : "invalid id" }, status_code = HTTP_400_BAD_REQUEST )
181184
182- file_path = workflow_local_dir / f"{ workflow_id } .json"
183185 workflow_local_dir .mkdir (parents = True , exist_ok = True )
184186
185187 # If the body claims a different ID, treat that as a "rename".
186188 if request_body .get ("id" ) and request_body .get ("id" ) != workflow_id :
187- old_id = request_body ["id" ]
189+ old_id : str = request_body ["id" ]
188190 if not re .match (r"^[\w\-]+$" , old_id ):
189191 return JSONResponse (
190192 {"error" : "invalid id" }, status_code = HTTP_400_BAD_REQUEST
191193 )
192194
193- old_file_path = workflow_local_dir / f"{ old_id } .json"
195+ old_workflow_hash = sha256 (old_id .encode ()).hexdigest ()
196+ old_file_path = workflow_local_dir / f"{ old_workflow_hash } .json"
194197 if old_file_path .exists ():
195198 try :
196199 old_file_path .unlink ()
197200 except Exception as e :
198- logger .error (f"Error deleting { old_file_path } : { e } " )
201+ logger .error (f"Error deleting { old_id } from { old_file_path } : { e } " )
199202 return JSONResponse ({"error" : "unable to delete file" }, status_code = 500 )
200203
201- request_body ["id" ] = workflow_id
204+ request_body ["id" ] = workflow_id
202205
206+ workflow_hash = sha256 (workflow_id .encode ()).hexdigest ()
207+ file_path = workflow_local_dir / f"{ workflow_hash } .json"
203208 try :
204209 with file_path .open ("w" , encoding = "utf-8" ) as f :
205210 json .dump (request_body , f , indent = 2 )
206211 except Exception as e :
207- logger .error (f"Error writing JSON to { file_path } : { e } " )
212+ logger .error (f"Error writing JSON for { workflow_id } to { file_path } : { e } " )
208213 return JSONResponse ({"error" : "unable to write file" }, status_code = 500 )
209214
210215 return JSONResponse (
@@ -223,14 +228,15 @@ async def delete_workflow(workflow_id: str):
223228 if not re .match (r"^[\w\-]+$" , workflow_id ):
224229 return JSONResponse ({"error" : "invalid id" }, status_code = HTTP_400_BAD_REQUEST )
225230
226- file_path = workflow_local_dir / f"{ workflow_id } .json"
231+ workflow_hash = sha256 (workflow_id .encode ()).hexdigest ()
232+ file_path = workflow_local_dir / f"{ workflow_hash } .json"
227233 if not file_path .exists ():
228234 return JSONResponse ({"error" : "not found" }, status_code = HTTP_404_NOT_FOUND )
229235
230236 try :
231237 file_path .unlink ()
232238 except Exception as e :
233- logger .error (f"Error deleting { file_path } : { e } " )
239+ logger .error (f"Error deleting { workflow_id } from { file_path } : { e } " )
234240 return JSONResponse ({"error" : "unable to delete file" }, status_code = 500 )
235241
236242 return JSONResponse (
@@ -253,7 +259,8 @@ async def builder_maybe_redirect(workflow_id: str):
253259 if not re .match (r"^[\w\-]+$" , workflow_id ):
254260 return RedirectResponse (url = "/build" , status_code = 302 )
255261
256- file_path = workflow_local_dir / f"{ workflow_id } .json"
262+ workflow_hash = sha256 (workflow_id .encode ()).hexdigest ()
263+ file_path = workflow_local_dir / f"{ workflow_hash } .json"
257264 if file_path .exists ():
258265 return RedirectResponse (url = f"/build/edit/{ workflow_id } " , status_code = 302 )
259266 else :
0 commit comments