Skip to content

Commit

Permalink
fix: S3 storage exists always return false for directories (#77)
Browse files Browse the repository at this point in the history
These changes use listdir method instead of exists method of stoage because  storage.exists expects a file path when storage is set to S3
  • Loading branch information
ziafazal authored Jun 21, 2024
1 parent de80759 commit e3beb49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Bugfix] Make addition of block usage key in scorm path backward compatible. (by @ziafazal)
22 changes: 19 additions & 3 deletions openedxscorm/scormxblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def popup_window(self, request, _suffix):
return Response(body=rendered)

def clean_storage(self):
if self.storage.exists(self.extract_folder_base_path):
if self.path_exists(self.extract_folder_base_path):
logger.info(
'Removing previously unzipped "%s"', self.extract_folder_base_path
)
Expand Down Expand Up @@ -400,7 +400,7 @@ def index_page_url(self):
return ""
folder = self.extract_folder_path
if self.storage.exists(
os.path.join(self.extract_folder_base_path, self.index_page_path)
os.path.join(self.extract_folder_base_path, self.clean_path(self.index_page_path))
):
# For backward-compatibility, we must handle the case when the xblock data
# is stored in the base folder.
Expand All @@ -417,14 +417,30 @@ def extract_folder_path(self):
"""
return os.path.join(self.extract_folder_base_path, self.package_meta["sha1"])

def clean_path(self, path):
"""
Removes query string from a path
"""
return path.split('?')[0] if path else path

def path_exists(self, path):
"""
Returs True if given path exists in storage otherwise returns False
"""
try:
dirs, files = self.storage.listdir(path)
return True if dirs or files else False
except FileNotFoundError:
return False

@property
def extract_folder_base_path(self):
"""
Path to the folder where packages will be extracted.
Compute hash of the unique block usage_id and use that as our directory name.
"""
# For backwards compatibility, we return the old path if the directory exists
if self.storage.exists(self.extract_old_folder_base_path):
if self.path_exists(self.extract_old_folder_base_path):
return self.extract_old_folder_base_path
sha1 = hashlib.sha1()
sha1.update(str(self.scope_ids.usage_id).encode())
Expand Down

0 comments on commit e3beb49

Please sign in to comment.