Skip to content

Commit e3beb49

Browse files
authored
fix: S3 storage exists always return false for directories (#77)
These changes use listdir method instead of exists method of stoage because storage.exists expects a file path when storage is set to S3
1 parent de80759 commit e3beb49

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [Bugfix] Make addition of block usage key in scorm path backward compatible. (by @ziafazal)

openedxscorm/scormxblock.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def popup_window(self, request, _suffix):
341341
return Response(body=rendered)
342342

343343
def clean_storage(self):
344-
if self.storage.exists(self.extract_folder_base_path):
344+
if self.path_exists(self.extract_folder_base_path):
345345
logger.info(
346346
'Removing previously unzipped "%s"', self.extract_folder_base_path
347347
)
@@ -400,7 +400,7 @@ def index_page_url(self):
400400
return ""
401401
folder = self.extract_folder_path
402402
if self.storage.exists(
403-
os.path.join(self.extract_folder_base_path, self.index_page_path)
403+
os.path.join(self.extract_folder_base_path, self.clean_path(self.index_page_path))
404404
):
405405
# For backward-compatibility, we must handle the case when the xblock data
406406
# is stored in the base folder.
@@ -417,14 +417,30 @@ def extract_folder_path(self):
417417
"""
418418
return os.path.join(self.extract_folder_base_path, self.package_meta["sha1"])
419419

420+
def clean_path(self, path):
421+
"""
422+
Removes query string from a path
423+
"""
424+
return path.split('?')[0] if path else path
425+
426+
def path_exists(self, path):
427+
"""
428+
Returs True if given path exists in storage otherwise returns False
429+
"""
430+
try:
431+
dirs, files = self.storage.listdir(path)
432+
return True if dirs or files else False
433+
except FileNotFoundError:
434+
return False
435+
420436
@property
421437
def extract_folder_base_path(self):
422438
"""
423439
Path to the folder where packages will be extracted.
424440
Compute hash of the unique block usage_id and use that as our directory name.
425441
"""
426442
# For backwards compatibility, we return the old path if the directory exists
427-
if self.storage.exists(self.extract_old_folder_base_path):
443+
if self.path_exists(self.extract_old_folder_base_path):
428444
return self.extract_old_folder_base_path
429445
sha1 = hashlib.sha1()
430446
sha1.update(str(self.scope_ids.usage_id).encode())

0 commit comments

Comments
 (0)