From de360a6e5c8b9a431ce1caa46a6dc0337e463b08 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Wed, 14 Jun 2023 18:28:11 -0400 Subject: Search for attachment directory root Implement a method that finds the attachment directory root as it is possible that the attachment url referenced in the outbox.json file isn't relative to its location but present instead inside a subdirectory that is next to the outbox.json file. --- src/archive_parser.cpp | 35 +++++++++++++++++++++++++++++++++-- src/archive_parser.h | 5 +++++ 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp index e4e9ed0..8c54a55 100644 --- a/src/archive_parser.cpp +++ b/src/archive_parser.cpp @@ -7,6 +7,7 @@ #include #include #include +#include Archive::Archive(QString outbox_filename, ArchiveType archive_type) : outbox_filename(outbox_filename), archive_type(archive_type) {} @@ -41,6 +42,8 @@ Archive::InitError Archive::init() { outbox_items = new QJsonArray(outbox_json->value("orderedItems").toArray()); // we'll need it during Archive's lifetime } + archive_root_dir = QFileInfo(outbox_filename).absoluteDir(); + return NoError; } @@ -242,8 +245,12 @@ QString Archive::get_html_status_attachments(QJsonValueRef attachments_ref, int if (attachment.contains("url")) { QString url = attachment["url"].toString(); - QFileInfo path(outbox_filename); - url.prepend(path.absolutePath()); + + // Initialize attachment_dir if it hasn't been done + if (attachment_dir_have_to_find) find_attachment_dir(url); + + // Also add a "/", after the attachment dir path, to make sure that the url starts with one as sometimes the ones in the json do not. We could make this prettier (but a bit slower) by only adding it conditionally. This might be worth it as the path can be copied and opened in the browser. + url.prepend(attachment_dir.absolutePath() + "/"); att_html.replace("{{path}}", url); att_html.replace("{{filename}}", QFileInfo(url).fileName()); // FIXME: this is ugly } @@ -344,3 +351,27 @@ QString Archive::get_html_status_text(int status_index) { return text; } + +void Archive::find_attachment_dir(QString example_attachment) { + // Find the root directory name of the attachment + QString root_name = example_attachment.split('/', Qt::SkipEmptyParts)[0]; + + // Iterate over each subdirectory of the archive to find the directory containing the attachments + archive_root_dir.setFilter(QDir::Dirs|QDir::NoDotAndDotDot); + QDirIterator it(archive_root_dir, QDirIterator::Subdirectories); + while (it.hasNext()) { + QString current_dir = it.next(); + + if (current_dir.section('/', -1) == root_name) { + // We have found the directory + // Remove the root_name component (that should be at the end) as it will be added back due to being part of the attachment url + attachment_dir.setPath(current_dir.remove(root_name)); + attachment_dir_have_to_find = false; + + if (not attachment_dir.exists()) + // This shouldn't happen, but log it in case' + qDebug() << "Attachment dir does not exist!" << attachment_dir.canonicalPath(); + return; + } + } +} diff --git a/src/archive_parser.h b/src/archive_parser.h index 6b2baa4..419a939 100644 --- a/src/archive_parser.h +++ b/src/archive_parser.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "types.h" @@ -34,10 +35,14 @@ public: QString get_html_status_text(int status_index); private: QString outbox_filename; + QDir archive_root_dir; ArchiveType archive_type; + QDir attachment_dir; + bool attachment_dir_have_to_find = true; QJsonObject *outbox_json = nullptr; QJsonArray *outbox_items = nullptr; QString get_html_status_attachments(QJsonValueRef attachments_ref, int text_zone_width); + void find_attachment_dir(QString example_attachment); }; -- cgit v1.2.3-54-g00ecf