@@ -91,6 +91,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
91
91
self .content = None
92
92
self .on_disk = False
93
93
self .on_cloud = kwargs .get ('on_cloud' , False )
94
+ self .size = None
94
95
95
96
if attachment :
96
97
if isinstance (attachment , dict ):
@@ -106,6 +107,7 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
106
107
self .attachment_type = 'item' if 'item' in attachment .get (
107
108
'@odata.type' , '' ).lower () else 'file'
108
109
self .on_disk = False
110
+ self .size = attachment .get (self ._cc ('size' ), None )
109
111
else :
110
112
file_path = attachment .get ('path' , attachment .get ('name' ))
111
113
if file_path is None :
@@ -117,6 +119,8 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
117
119
self .attachment = Path (file_path ) if self .on_disk else None
118
120
self .name = (self .attachment .name if self .on_disk
119
121
else attachment .get ('name' ))
122
+ self .size = self .attachment .stat ().st_size if self .attachment else None
123
+
120
124
elif isinstance (attachment , str ):
121
125
self .attachment = Path (attachment )
122
126
self .name = self .attachment .name
@@ -135,11 +139,15 @@ def __init__(self, attachment=None, *, parent=None, **kwargs):
135
139
self .content = attachment .to_api_data ()
136
140
self .content ['@odata.type' ] = attachment .attachment_type
137
141
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 ():
140
143
with self .attachment .open ('rb' ) as file :
141
144
self .content = base64 .b64encode (file .read ()).decode ('utf-8' )
142
145
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
143
151
144
152
def to_api_data (self ):
145
153
""" Returns a dict to communicate with the server
0 commit comments