8000 Added attribute size to attachments #259 · urshala/google-cloud-python@f358a81 · GitHub
[go: up one dir, main page]

Skip to content

Commit f358a81

Browse files
author
Alejandro Casanovas
committed
Added attribute size to attachments googleapis#259
1 parent 2a92dda commit f358a81

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

O365/utils/attachment.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
9191
self.content = None
9292
self.on_disk = False
9393
self.on_cloud = kwargs.get('on_cloud', False)
94+
self.size = None
9495

9596
if attachment:
9697
if isinstance(attachment, dict):
@@ -106,6 +107,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
106107
self.attachment_type = 'item' if 'item' in attachment.get(
107108
'@odata.type', '').lower() else 'file'
108109
self.on_disk = False
110+
self.size = attachment.get(self._cc('size'), None)
109111
else:
110112
file_path = attachment.get('path', attachment.get('name'))
111113
if file_path is None:
@@ -117,6 +119,8 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
117119
self.attachment = Path(file_path) if self.on_disk else None
118120
self.name = (self.attachment.name if self.on_disk
119121
else attachment.get('name'))
122+
self.size = self.attachment.stat().st_size if self.attachment else None
123+
120124
elif isinstance(attachment, str):
121125
self.attachment = Path(attachment)
122126
self.name = self.attachment.name
@@ -135,11 +139,15 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
135139
self.content = attachment.to_api_data()
136140
self.content['@odata.type'] = attachment.attachment_type
137141

138-
if self.content is None and self.attachment and \
139-
self.attachment.exists():
142+
if self.content is None and self.attachment and self.attachment.exists():
140143
with self.attachment.open('rb') as file:
141144
self.content = base64.b64encode(file.read()).decode('utf-8')
142145
self.on_disk = True
146+
self.size = self.attachment.stat().st_size
147+
148+
def __len__(self):
149+
""" Returns the size of this attachment """
150+
return self.size
143151

144152
def to_api_data(self):
145153
""" Returns a dict to communicate with the server

0 commit comments

Comments
 (0)
0