From d7691fba717c11edff60fe6fdc2c21ad75f4c9e9 Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 7 Aug 2025 12:41:36 -0600 Subject: [PATCH] Handling when Singlefile extension uploads may be not be the usual text/html. --- app/src/Events.js | 4 ++++ modules/archive/__init__.py | 4 ++-- modules/archive/api.py | 10 ++++++++-- modules/archive/models.py | 3 +-- wrolpi/events.py | 4 ++++ wrolpi/files/models.py | 8 ++++++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/src/Events.js b/app/src/Events.js index 29a35412..300db707 100644 --- a/app/src/Events.js +++ b/app/src/Events.js @@ -101,6 +101,10 @@ function handleEvents(events) { () => window.open(url, '_self')); } + if (event === 'upload_archive_failed') { + eventToast('Archive Upload Failed!', message, 'error', 5000); + } + if (subject) { newestEvents[subject] = dt; } diff --git a/modules/archive/__init__.py b/modules/archive/__init__.py index 26ada14b..e7d63fdf 100644 --- a/modules/archive/__init__.py +++ b/modules/archive/__init__.py @@ -112,8 +112,8 @@ def model_archive(file_group: FileGroup, session: Session = None) -> Archive: session.flush([file_group]) file_group_id = file_group.id - # All Archives have an HTML Singlefile. - html_paths = file_group.my_paths('text/html') + # All Archives have an HTML Singlefile. Singlefile may be text HTML, or bytes HTML. + html_paths = file_group.my_html_paths() if not html_paths: logger.error('Query returned a group without an HTML file!') raise InvalidArchive('FileGroup does not contain any html files') diff --git a/modules/archive/api.py b/modules/archive/api.py index 6303fe15..5268338f 100644 --- a/modules/archive/api.py +++ b/modules/archive/api.py @@ -107,9 +107,15 @@ async def singlefile_upload_switch_handler(url=None): q_size = q.qsize() logger.info(f'singlefile_upload_switch_handler queue size: {q_size}') - archive = await lib.singlefile_to_archive(singlefile) - logger.info(f'Created Archive from upload ({q_size}): {archive}') + try: + archive = await lib.singlefile_to_archive(singlefile) + except Exception as e: + logger.error(f'singlefile_upload_switch_handler failed', exc_info=e) + Events.send_upload_archive_failed(f'Failed to convert singlefile to archive: {e}') + raise + name = archive.file_group.title or archive.file_group.url + logger.info(f'Created Archive from upload ({q_size}): {archive}') Events.send_archive_uploaded(f'Created Archive from upload: {name}', url=archive.location) url = archive.file_group.url if url and (download := Download.get_by_url(url)): diff --git a/modules/archive/models.py b/modules/archive/models.py index 24877d1f..22f7f969 100644 --- a/modules/archive/models.py +++ b/modules/archive/models.py @@ -141,8 +141,7 @@ class Archive(Base, ModelHelper): @property def singlefile_file(self) -> Optional[dict]: from modules.archive.lib import is_singlefile_file - files = self.file_group.my_files('text/html') - for file in files: + for file in self.file_group.my_html_files(): if is_singlefile_file(file['path']): return file diff --git a/wrolpi/events.py b/wrolpi/events.py index 17810936..ce604308 100644 --- a/wrolpi/events.py +++ b/wrolpi/events.py @@ -96,6 +96,10 @@ class Events: def send_archive_uploaded(cls, message: str = None, url: str = None): send_event('upload_archive', message, subject='upload', url=url) + @classmethod + def send_upload_archive_failed(cls, message: str = None): + send_event('upload_archive_failed', message, subject='upload') + def log_event(event: str, message: str = None, action: str = None, subject: str = None): log = f'{event=}' diff --git a/wrolpi/files/models.py b/wrolpi/files/models.py index 8fd10f0e..29a60767 100644 --- a/wrolpi/files/models.py +++ b/wrolpi/files/models.py @@ -275,6 +275,14 @@ class FileGroup(ModelHelper, Base): def my_mobi_files(self) -> List[dict]: return self.my_files('application/x-mobipocket-ebook') + def my_html_files(self) -> List[dict]: + html_files = self.my_files('text/html', 'application/octet-stream') + html_files = [i for i in html_files if i['path'].name.endswith('.html')] + return html_files + + def my_html_paths(self) -> List[pathlib.Path]: + return [i['path'] for i in self.my_html_files()] + def delete(self, add_to_skip_list: bool = True): """Delete this FileGroup record, and all of its files.