@@ -311,26 +311,41 @@ def download_project_file(
311311 :rtype: file
312312 """
313313 project = require_project (namespace , project_name , ProjectPermissions .Read )
314- files = _project_version_files (project , version )
315- file_path = None
314+ if diff and not version :
315+ abort (400 , f"Changeset must be requested for particular file version" )
316+
317+ lookup_version = version or project .latest_version
318+ sql = text (
319+ """
320+ SELECT
321+ expanded.files ->> 'location' AS location,
322+ (expanded.files ->> 'diff')::jsonb ->> 'location' as diff_location
323+ FROM
324+ (
325+ SELECT jsonb_array_elements(pv.files::jsonb) AS files
326+ FROM project_version pv
327+ WHERE pv.name = :version AND project_id = :project_id
328+ ) AS expanded
329+ WHERE
330+ expanded.files @> :json;
331+ """
332+ )
333+ params = {
334+ "version" : lookup_version ,
335+ "project_id" : project .id ,
336+ "json" : '{"path": "' + file + '"}' ,
337+ }
338+ result = db .session .execute (sql , params ).fetchone ()
339+ if not result :
340+ abort (404 , f"File { file } not found" )
341+
316342 if diff and version :
317343 # get specific version of geodiff file modified in requested version
318- file_obj = next (
319- (f for f in files if f ["location" ] == os .path .join (version , file )), None
320- )
321- if not file_obj :
322- abort (404 , file )
323- if "diff" not in file_obj :
344+ if not result ["diff_location" ]:
324345 abort (404 , f"No diff in particular file { file } version" )
325- file_path = file_obj ["diff" ]["location" ]
326- elif diff :
327- abort (400 , f"Changeset must be requested for particular file version" )
346+ file_path = result ["diff_location" ]
328347 else :
329- # get latest version of file
330- file_path = next ((f ["location" ] for f in files if f ["path" ] == file ), None )
331-
332- if not file_path :
333- abort (404 , file )
348+ file_path = result ["location" ]
334349
335350 if version and not diff :
336351 project .storage .restore_versioned_file (file , version )
0 commit comments