Skip to content

Commit

Permalink
Avoid filename conflicts with content and attachments, handle oserror (
Browse files Browse the repository at this point in the history
…#385)

* Handle OSError in download thread gracefully

* Assign a extension based on mimetype to all attachments

* Replace dots in content directories
  • Loading branch information
sanjacob authored Sep 20, 2024
1 parent 6d74c29 commit cfca56a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
18 changes: 15 additions & 3 deletions blackboard_sync/content/attachment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid
import mimetypes

from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
Expand All @@ -15,10 +16,21 @@ class Attachment(BStream):

def __init__(self, attachment: BBAttachment, api_path: BBContentPath,
job: DownloadJob):
self.filename = attachment.fileName
filename = attachment.fileName or str(uuid.uuid1())
name_ext = '.' + filename.split('.')[-1]

# Guess extension based on content
mime = attachment.mimeType or 'text/plain'
real_ext = mimetypes.guess_extension(mime, strict=False)
possible_ext = mimetypes.guess_all_extensions(mime, strict=False)

if name_ext in possible_ext:
self.filename = filename
else:
self.filename = filename + real_ext

self.stream = job.session.download(attachment_id=attachment.id,
**api_path)

def write(self, path: Path, executor: ThreadPoolExecutor) -> None:
filename = self.filename or str(uuid.uuid1())
super().write_base(path / filename, executor, self.stream)
super().write_base(path / self.filename, executor, self.stream)
2 changes: 1 addition & 1 deletion blackboard_sync/content/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, content: BBCourseContent, api_path: BBContentPath,

Handler = Content.get_handler(content.contentHandler)

self.title = content.title_path_safe
self.title = content.title_path_safe.replace('.', '_')

try:
self.handler = Handler(content, api_path, job)
Expand Down
4 changes: 2 additions & 2 deletions blackboard_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ def download(self) -> datetime | None:
except BBUnauthorizedError:
logger.exception("User session expired")
self.log_out()
except RequestException:
logger.exception("Network failure")
except (RequestException, OSError):
logger.exception("Download error")
self._has_error = True

# manually postpone next sync job
Expand Down

0 comments on commit cfca56a

Please sign in to comment.