Skip to content

Commit 2ea74b4

Browse files
authored
Update gcsdbutils.py handling of filenames
1 parent 1ac2ab9 commit 2ea74b4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/webapp/gcsdbutils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,22 @@
3737

3838
# From a fully qualified file nam (i.e. everything sub-bucket name level), get the job id.
3939
def get_job_id(filename: str) -> int:
40-
tmp = ""
40+
tmp = get_filename_without_approve_dir(filename)
41+
return int(tmp.split("/")[0])
42+
43+
44+
# Remove the approved or unapproved prefix as that isn't a property of the filename itself
45+
# and may change if the file gets approved or unapproved.
46+
def get_filename_without_approve_dir(filename: str) -> int:
4147
if filename.startswith("approved/"):
42-
tmp = filename.removeprefix("approved/")
48+
return filename.removeprefix("approved/")
4349
elif filename.startswith("unapproved/"):
44-
tmp = filename.removeprefix("unapproved/")
50+
return filename.removeprefix("unapproved/")
4551
else:
46-
raise ValueError("Unexpected filename structure.")
47-
return int(tmp.split("/")[0])
52+
return filename
4853

4954

55+
# This should of course be called before any prefix stripping.
5056
def is_file_approved(filename: str) -> bool:
5157
if filename.startswith("approved/"):
5258
return True
@@ -77,6 +83,8 @@ def update_db_from_bucket(inst_id: str, session, storage_control):
7783
if not f.endswith(".png") and not f.endswith(".csv"):
7884
continue
7985
file_approved = is_file_approved(f)
86+
# We strip the approved/unapproved prefix since the file can move between the two and should still be considered one file.
87+
f = get_filename_without_approve_dir(f)
8088
# Check if that file already exists in the table, otherwise add it.
8189
query_result = session.execute(
8290
select(FileTable).where(

0 commit comments

Comments
 (0)