Skip to content

Commit

Permalink
Add support to re-write depot URLs
Browse files Browse the repository at this point in the history
This will enable support to use https://assets.algobowl.org URLs in
algobowl-mines-prod.
  • Loading branch information
jackrosenthal committed Feb 19, 2024
1 parent f596409 commit 8b8dc26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
11 changes: 6 additions & 5 deletions algobowl/controllers/file_redirector.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import datetime
import re
from typing import Union

import tg

import algobowl.model as model


def input_redirector(group_id):
def input_redirector(group_id: str) -> model.Input:
group_id = int(group_id)
group = model.DBSession.query(model.Group).get(group_id)
if not group:
Expand All @@ -24,12 +25,12 @@ def input_redirector(group_id):
if file_is_public or is_group_member or is_admin:
if not group.input:
tg.abort(404, "An input file has not been uploaded yet.")
return group.input.data
return group.input

tg.abort(403, "You do not have access to this file at this time.")


def output_redirector(from_group_id, to_group_id):
def output_redirector(from_group_id: str, to_group_id: str) -> model.Output:
from_group_id = int(from_group_id)
to_group_id = int(to_group_id)
output = (
Expand Down Expand Up @@ -57,7 +58,7 @@ def output_redirector(from_group_id, to_group_id):
or is_admin
or (is_verifier and visible_to_verifier)
):
return output.data
return output

tg.abort(403, "You do not have access to this file at this time.")

Expand All @@ -68,7 +69,7 @@ def output_redirector(from_group_id, to_group_id):
]


def get_file(filename):
def get_file(filename: str) -> Union[model.Input, model.Output]:
for pattern, func in file_pattern_handlers:
m = pattern.fullmatch(filename)
if m:
Expand Down
8 changes: 5 additions & 3 deletions algobowl/controllers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def tos(self):

@expose()
def files(self, filename, redirect=False):
file = file_redirector.get_file(filename)
db_obj = file_redirector.get_file(filename)
if "://" in db_obj.url:
tg.redirect(db_obj.url)
if redirect:
tg.redirect(file.url)
tg.redirect(db_obj.data.url)
tg.response.content_type = "application/octet-stream"
return file.file.read()
return db_obj.data.file.read()

@expose()
def login(self):
Expand Down
13 changes: 11 additions & 2 deletions algobowl/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import enum
from typing import Optional

import sqlalchemy as sa
import tg
Expand Down Expand Up @@ -287,6 +288,14 @@ def __repr__(self):
)


def _get_depot_url(filename: str, file_id: Optional[str]) -> str:
"""Helper to implement .url property of Input/Output objects."""
rewrite_rule = tg.config.get("algobowl.depot_url_rewrite")
if rewrite_rule and file_id:
return rewrite_rule.format(filename=filename, file_id=file_id)
return tg.url(f"/files/{filename}")


class Input(DeclarativeBase):
__tablename__ = "input"
db_icon = "far fa-file"
Expand All @@ -308,7 +317,7 @@ def filename(self):

@property
def url(self):
return tg.url(f"/files/{self.filename}")
return _get_depot_url(self.filename, self.data and self.data.file_id)

def __repr__(self):
return "Input from [{!r}]".format(self.group)
Expand Down Expand Up @@ -350,7 +359,7 @@ def filename(self):

@property
def url(self):
return tg.url(f"/files/{self.filename}")
return _get_depot_url(self.filename, self.data and self.data.file_id)

def __repr__(self):
return "Output from [{!r}] for {!r}".format(self.group, self.input)
Expand Down

0 comments on commit 8b8dc26

Please sign in to comment.