File: htmlwriteattachment.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 3,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
  Copyright (C) 2023-2025  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  signalbackup-tools is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "signalbackup.ih"

#include <cerrno>

bool SignalBackup::HTMLwriteAttachment(std::string const &directory, std::string const &threaddir,
                                       long long int rowid, long long int uniqueid, //std::string const &ext,
                                       std::string const &attachment_filename, long long int timestamp,
                                       bool overwrite, bool append) const
{
  auto attachmentfound = d_attachments.find({rowid, uniqueid});
  if (attachmentfound == d_attachments.end()) [[unlikely]]
    return false;

  // directory + threaddir is guaranteed to exist at this point, check/create 'media'
  if (!bepaald::fileOrDirExists(directory + "/" + threaddir + "/media"))
  {
    if (!bepaald::createDir(directory + "/" + threaddir + "/media"))
    {
      Logger::error("Failed to create directory `", directory, "/", threaddir, "/media");
      return false;
    }
  }
  else if (!bepaald::isDir(directory + "/" + threaddir + "/media"))
  {
    Logger::error("Failed to create directory `", directory, "/", threaddir, "/media");
    return false;
  }

  // check actual attachmentfile file
  std::string attachment_filename_full = directory + "/" + threaddir + "/media/" +
    attachment_filename;
  // "/media/Attachment_" + bepaald::toString(rowid) + "_" + bepaald::toString(uniqueid) + "." + ext;

  WIN_CHECK_PATH_LENGTH(attachment_filename_full);

  if (bepaald::fileOrDirExists(attachment_filename_full))
  {
    if (append) // file already exists, but we were asked to just use the existing file, so we're done
      return true;
    if (!overwrite) // file already exists, but we were no asked to overwrite -> error!
    {

      //std::cout << attachment_filename << std::endl;

      Logger::error("Attachment file exists. Not overwriting");
      return false;
    }
  }

  AttachmentFrame *a = attachmentfound->second.get();

  // write actual attachment:
  std::ofstream attachmentstream(WIN_LONGPATH(attachment_filename_full), std::ios_base::binary);
  if (!attachmentstream.is_open())
  {
    Logger::error("Failed to open file for writing: '", attachment_filename_full, "'",
                  " (errno: ", std::strerror(errno), ")"); // note: errno is not required to be set by std
    // temporary !!
    {
      std::error_code error;
      std::filesystem::space_info const si = std::filesystem::space(directory, error);
      if (!error)
      {
        Logger::message("Space available: ", static_cast<std::intmax_t>(si.available),
                        "\nAttachment size: ", a->attachmentSize());
      }
    }
    return false;
  }
  else
  {
    if (!attachmentstream.write(reinterpret_cast<char *>(a->attachmentData()), a->attachmentSize()))
      return false;
    // write was succesfull. drop attachment data
    a->clearData();

    attachmentstream.close(); // need to close, or the auto-close will change files mtime again.

    if (timestamp >= 0)
      if (!setFileTimeStamp(attachment_filename_full, timestamp)) [[unlikely]]
        Logger::warning("Failed to set timestamp for attachment '", attachment_filename_full, "'");
  }
  return true;
}