Skip to content

Commit

Permalink
Refactored Office 365 Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Oct 11, 2024
1 parent 19c0d8a commit f6c7dde
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 93 deletions.
27 changes: 25 additions & 2 deletions apprise/attachment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,27 @@ def download(self):

def open(self, mode='rb'):
"""
return our file pointer and track it (we'll auto close later
return our file pointer and track it (we'll auto close later)
"""
pointer = open(self.path, mode=mode)
self.__pointers.add(pointer)
return pointer

def chunk(self, size=5242880):
"""
A Generator that yield chunks of a file with the specified size.
By default the chunk size is set to 5MB (5242880 bytes)
"""

with self.open() as file:
while True:
chunk = file.read(size)
if not chunk:
break

yield chunk

def __enter__(self):
"""
support with keyword
Expand Down Expand Up @@ -431,7 +446,15 @@ def __len__(self):
Returns the filesize of the attachment.
"""
return os.path.getsize(self.path) if self.path else 0
if not self:
return 0

try:
return os.path.getsize(self.path) if self.path else 0

except OSError:
# OSError can occur if the file is inaccessible
return 0

def __bool__(self):
"""
Expand Down
Loading

0 comments on commit f6c7dde

Please sign in to comment.