Skip to content

Commit

Permalink
Added attribute size to attachments googleapis#259
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Casanovas committed Jul 1, 2019
1 parent 2a92dda commit f358a81
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions O365/utils/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
self.content = None
self.on_disk = False
self.on_cloud = kwargs.get('on_cloud', False)
self.size = None

if attachment:
if isinstance(attachment, dict):
Expand All @@ -106,6 +107,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
self.attachment_type = 'item' if 'item' in attachment.get(
'@odata.type', '').lower() else 'file'
self.on_disk = False
self.size = attachment.get(self._cc('size'), None)
else:
file_path = attachment.get('path', attachment.get('name'))
if file_path is None:
Expand All @@ -117,6 +119,8 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
self.attachment = Path(file_path) if self.on_disk else None
self.name = (self.attachment.name if self.on_disk
else attachment.get('name'))
self.size = self.attachment.stat().st_size if self.attachment else None

elif isinstance(attachment, str):
self.attachment = Path(attachment)
self.name = self.attachment.name
Expand All @@ -135,11 +139,15 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
self.content = attachment.to_api_data()
self.content['@odata.type'] = attachment.attachment_type

if self.content is None and self.attachment and \
self.attachment.exists():
if self.content is None and self.attachment and self.attachment.exists():
with self.attachment.open('rb') as file:
self.content = base64.b64encode(file.read()).decode('utf-8')
self.on_disk = True
self.size = self.attachment.stat().st_size

def __len__(self):
""" Returns the size of this attachment """
return self.size

def to_api_data(self):
""" Returns a dict to communicate with the server
Expand Down

0 comments on commit f358a81

Please sign in to comment.