diff options
Diffstat (limited to 'src/archive_parser.cpp')
-rw-r--r-- | src/archive_parser.cpp | 35 |
1 files changed, 33 insertions, 2 deletions
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 <QTextDocument> #include <QFileInfo> #include <QDateTime> +#include <QDirIterator> 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; + } + } +} |