Skip to content

Commit

Permalink
Add "Content-Length" header for run PGN
Browse files Browse the repository at this point in the history
  • Loading branch information
ppigazzini committed Apr 27, 2024
1 parent 11dd764 commit e01bd92
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion server/fishtest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,14 @@ def download_run_pgns(self):
if not match:
return Response("Invalid filename format", status=400)
run_id = match.group(1)
pgns_reader = self.request.rundb.get_run_pgns(run_id)
pgns_reader, total_size = self.request.rundb.get_run_pgns(run_id)
if pgns_reader is None:
return Response("No data found", status=404)
response = Response(content_type="application/gzip")
response.app_iter = FileIter(pgns_reader)
response.headers["Content-Disposition"] = f'attachment; filename="{pgns_name}"'
response.headers["Content-Encoding"] = "gzip"
response.headers["Content-Length"] = str(total_size)
return response

@view_config(route_name="api_download_nn")
Expand Down
15 changes: 12 additions & 3 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ def new_run(
return self.runs.insert_one(new_run).inserted_id

def upload_pgn(self, run_id, pgn_zip):
self.pgndb.insert_one({"run_id": run_id, "pgn_zip": Binary(pgn_zip)})
self.pgndb.insert_one(
{"run_id": run_id, "pgn_zip": Binary(pgn_zip), "size": len(pgn_zip)}
)
return {}

def get_pgn(self, run_id):
Expand All @@ -271,8 +273,15 @@ def get_run_pgns(self, run_id):
if pgns is not None:
# Create a generator that yields each pgn.gz file
pgn_generator = (pgn["pgn_zip"] for pgn in pgns)
return GeneratorAsFileReader(pgn_generator)
return None
# Compute the total size using MongoDB's aggregation framework
total_size = self.pgndb.aggregate(
[
{"$match": {"run_id": {"$regex": f"^{run_id}"}}},
{"$group": {"_id": None, "totalSize": {"$sum": "$size"}}},
]
).next()["totalSize"]
return GeneratorAsFileReader(pgn_generator), total_size
return None, 0

def write_nn(self, net):
validate(nn_schema, net, "net")
Expand Down

0 comments on commit e01bd92

Please sign in to comment.