message: augment cache-path() so it supports subparts

Take an optional index parameter for a *subpart*.

This is for the case where we save attachments from a message (in particular,
when forwarding). We can't save them in the same directory for the (rare) case
when there are multiple attachments with the same name. And we don't want to
uniquify the name, since that shows up in e.g. the forwarded file name.

This can be solved by saving each in their own indexed subdir.
This commit is contained in:
Dirk-Jan C. Binnema 2022-05-15 11:28:48 +03:00
parent aea2f58c77
commit 5d9bb72c0b
2 changed files with 21 additions and 7 deletions

View File

@ -36,6 +36,7 @@
#include <cstdlib> #include <cstdlib>
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h>
#include <gmime/gmime.h> #include <gmime/gmime.h>
#include "gmime/gmime-message.h" #include "gmime/gmime-message.h"
@ -231,6 +232,7 @@ Message::load_mime_message(bool reload) const
return false; return false;
} else { } else {
priv_->mime_msg = std::move(mime_msg.value()); priv_->mime_msg = std::move(mime_msg.value());
fill_document(*priv_);
return true; return true;
} }
} }
@ -784,20 +786,29 @@ Message::parts() const
} }
Result<std::string> Result<std::string>
Message::cache_path() const Message::cache_path(Option<size_t> index) const
{ {
/* create tmpdir for this message, if needed */ /* create tmpdir for this message, if needed */
if (priv_->cache_path.empty()) { if (priv_->cache_path.empty()) {
GError *err{}; GError *err{};
auto tpath{to_string_opt_gchar( auto tpath{to_string_opt_gchar(g_dir_make_tmp("mu-cache-XXXXXX", &err))};
g_dir_make_tmp("mu-message-part-XXXXXX", &err))};
if (!tpath) if (!tpath)
return Err(Error::Code::File, &err, return Err(Error::Code::File, &err, "failed to create temp dir");
"failed to create temp dir");
priv_->cache_path = std::move(tpath.value()); priv_->cache_path = std::move(tpath.value());
} }
return Ok(std::string{priv_->cache_path}); if (index) {
GError *err{};
auto tpath = format("%s/%zu", priv_->cache_path.c_str(), *index);
if (g_mkdir(tpath.c_str(), 0700) != 0)
return Err(Error::Code::File, &err,
"failed to create cache dir '%s'; err=%d",
tpath.c_str(), errno);
return Ok(std::move(tpath));
} else
return Ok(std::string{priv_->cache_path});
} }

View File

@ -399,9 +399,12 @@ public:
* Get the path to a cche directory for this message, which * Get the path to a cche directory for this message, which
* is useful for temporarily saving attachments * is useful for temporarily saving attachments
* *
* @param index optionally, create <cache-path>/<index> instead;
* this is useful for having part-specific subdirectories.
*
* @return path to a (created) cache directory, or an error. * @return path to a (created) cache directory, or an error.
*/ */
Result<std::string> cache_path() const; Result<std::string> cache_path(Option<size_t> index={}) const;
/** /**