Skip to content

Commit

Permalink
check if obj exists on s3 (#607)
Browse files Browse the repository at this point in the history
* check if obj exists on s3

* Brian's fixes & testing

* unneeded method

---------

Co-authored-by: Wiessfelt, Katie <[email protected]>
  • Loading branch information
katiewiessfelt and Wiessfelt, Katie authored Sep 19, 2024
1 parent aed8e05 commit 0c5c17c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
31 changes: 27 additions & 4 deletions castle/cms/syndication.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from castle.cms.files.aws import uploaded
from castle.cms.theming import contentpanel_xpath
from castle.cms.utils import has_image
from lxml.html import tostring
Expand Down Expand Up @@ -49,17 +50,39 @@ def _init(self):
except TypeError:
pass

@property
def in_aws(self):
return uploaded(self.context)

@property
def has_local_file(self):
return self.file is not None and not self.in_aws

@property
def has_enclosure(self):
return self.in_aws or self.has_local_file

@property
def file_length(self):
if self.has_enclosure:
if self.in_aws:
try:
return self.file.getSize()
except POSKeyError:
return self.context.file.original_size
except Exception: # nosec B110
pass
except SystemError:
if self.has_local_file:
try:
return self.file.getSize()
except Exception: # nosec B110
pass
return 0

@property
def file_type(self):
if self.in_aws:
return self.context.file.original_content_type
elif self.has_local_file:
return self.file.contentType

@property
def has_image(self):
return self.image is not None
Expand Down
10 changes: 10 additions & 0 deletions castle/cms/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ def test_move_file_private(self):
self.assertTrue(hasattr(fileOb.file, 'original_size'))
self.assertTrue(hasattr(fileOb.file, 'original_content_type'))

# after move, it should still have values in original attributes
# which should have the same values as the original attributes
self.assertEqual(fileOb.file.file_length, fileOb.file.original_size)
self.assertEqual(fileOb.file.file_type, fileOb.file.original_content_type)

# after move, there should be annotations on the object
annotations = IAnnotations(fileOb)
url = annotations[aws.STORAGE_KEY].get('url', None)
Expand Down Expand Up @@ -177,6 +182,11 @@ def test_move_file_public(self):
self.assertFalse(hasattr(fileOb.file, 'original_size'))
self.assertFalse(hasattr(fileOb.file, 'original_content_type'))

# after move, it should still have values in original attributes
# which should have the same values as the original attributes
self.assertEqual(fileOb.file.file_length, fileOb.file.original_size)
self.assertEqual(fileOb.file.file_type, fileOb.file.original_content_type)

# before move, there should be no annotations
annotations = IAnnotations(fileOb)
self.assertIsNone(annotations.get(aws.STORAGE_KEY, None))
Expand Down

0 comments on commit 0c5c17c

Please sign in to comment.