Skip to content

Commit d737d2b

Browse files
authored
Merge pull request #127 from MerginMaps/fix_project_download
Fix project download
2 parents dd0b652 + 5424e97 commit d737d2b

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

server/mergin/sync/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Project(db.Model):
3636
)
3737
updated = db.Column(db.DateTime, onupdate=datetime.utcnow)
3838
# metadata for project files (see also FileInfoSchema)
39-
files = db.Column(JSONB, default=[])
39+
files = db.deferred(db.Column(JSONB, default=[]))
4040
tags = db.Column(ARRAY(String), server_default="{}")
4141
disk_usage = db.Column(BIGINT, nullable=False, default=0)
4242
latest_version = db.Column(db.String, index=True)

server/mergin/sync/public_api_controller.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)