Handling when Singlefile extension uploads may be not be the usual text/html.
Some checks failed
CodeQL / Analyze (push) Has been cancelled

This commit is contained in:
Roland 2025-08-07 12:41:36 -06:00
parent 76b9ead174
commit d7691fba71
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 27 additions and 6 deletions

View file

@ -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;
}

View file

@ -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')

View file

@ -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)):

View file

@ -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

View file

@ -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=}'

View file

@ -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.