message: allow extracting message parts to file

And add unit-test.

Fixes #2467
This commit is contained in:
Dirk-Jan C. Binnema
2023-04-05 00:05:10 +03:00
parent cd23e6015d
commit 026a19bcfa
5 changed files with 41 additions and 20 deletions

View File

@ -125,8 +125,6 @@ MimeObject::headers() const noexcept
return hdrs;
}
Result<size_t>
MimeObject::write_to_stream(const MimeFormatOptions& f_opts,
MimeStream& stream) const
@ -139,6 +137,22 @@ MimeObject::write_to_stream(const MimeFormatOptions& f_opts,
return Ok(static_cast<size_t>(written));
}
Result<size_t>
MimeObject::to_file(const std::string& path, bool overwrite) const noexcept
{
GError *err{};
auto strm{g_mime_stream_fs_open(path.c_str(),
O_WRONLY | O_CREAT | O_TRUNC |(overwrite ? 0 : O_EXCL),
S_IRUSR|S_IWUSR,
&err)};
if (!strm)
return Err(Error::Code::File, &err, "failed to open '%s'", path.c_str());
MimeStream stream{MimeStream::make_from_stream(strm)};
return write_to_stream({}, stream);
}
Option<std::string>
MimeObject::to_string_opt() const noexcept
{
@ -525,11 +539,8 @@ MimePart::to_string() const noexcept
buffer.resize(buflen);
return buffer;
}
Result<size_t>
MimePart::to_file(const std::string& path, bool overwrite) const noexcept
{
@ -558,9 +569,6 @@ MimePart::to_file(const std::string& path, bool overwrite) const noexcept
return Ok(static_cast<size_t>(written));
}
void
MimeMultipart::for_each(const ForEachFunc& func) const noexcept
{