summary refs log tree commit diff
path: root/src/notifications/Manager.cpp
blob: 6fa06cb23725c4fd170a11a25c217210a42ddd55 (plain) (blame)
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
#include "notifications/Manager.h"

#include "Cache.h"
#include "EventAccessors.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "Utils.h"

#include <QFile>
#include <QImage>
#include <QStandardPaths>

#include <mtxclient/crypto/client.hpp>

QString
NotificationsManager::cacheImage(const mtx::events::collections::TimelineEvents &event)
{
        const auto url      = mtx::accessors::url(event);
        auto encryptionInfo = mtx::accessors::file(event);

        auto filename = QString::fromStdString(mtx::accessors::body(event));
        QString path{QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" +
                     filename};

        http::client()->download(
          url,
          [path, url, encryptionInfo](const std::string &data,
                                      const std::string &,
                                      const std::string &,
                                      mtx::http::RequestErr err) {
                  if (err) {
                          nhlog::net()->warn("failed to retrieve image {}: {} {}",
                                             url,
                                             err->matrix_error.error,
                                             static_cast<int>(err->status_code));
                          return;
                  }

                  try {
                          auto temp = data;
                          if (encryptionInfo)
                                  temp = mtx::crypto::to_string(
                                    mtx::crypto::decrypt_file(temp, encryptionInfo.value()));

                          QFile file{path};

                          if (!file.open(QIODevice::WriteOnly))
                                  return;

                          // delete any existing file content
                          file.resize(0);
                          file.write(QByteArray(temp.data(), (int)temp.size()));

                          // resize the image (really inefficient, I know, but I can't find any
                          // better way right off
                          QImage img{path};

                          // delete existing contents
                          file.resize(0);

                          // make sure to save as PNG (because Plasma doesn't do JPEG in
                          // notifications)
                          //                          if (!file.fileName().endsWith(".png"))
                          //                                  file.rename(file.fileName() + ".png");

                          img.scaled(200, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation)
                            .save(&file);
                          file.close();

                          return;
                  } catch (const std::exception &e) {
                          nhlog::ui()->warn("Error while caching file to: {}", e.what());
                  }
          });

        return path.toHtmlEscaped();
}