1 | // SPDX-FileCopyrightText: 2017 Kitsune Ral <kitsune-ral@users.sf.net> |
2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
3 | |
4 | #include "eventcontent.h" |
5 | |
6 | #include "../logging_categories_p.h" |
7 | |
8 | #include "../converters.h" |
9 | |
10 | #include <QtCore/QFileInfo> |
11 | #include <QtCore/QMimeDatabase> |
12 | |
13 | using namespace Quotient::EventContent; |
14 | |
15 | QJsonObject Base::toJson() const |
16 | { |
17 | QJsonObject o; |
18 | fillJson(o); |
19 | return o; |
20 | } |
21 | |
22 | FileInfo::FileInfo(const QFileInfo& fi) |
23 | : source(QUrl::fromLocalFile(localfile: fi.filePath())), |
24 | mimeType(QMimeDatabase().mimeTypeForFile(fileInfo: fi)), |
25 | payloadSize(fi.size()), |
26 | originalName(fi.fileName()) |
27 | { |
28 | Q_ASSERT(fi.isFile()); |
29 | } |
30 | |
31 | FileInfo::FileInfo(FileSourceInfo sourceInfo, qint64 payloadSize, |
32 | const QMimeType& mimeType, QString originalFilename) |
33 | : source(std::move(sourceInfo)) |
34 | , mimeType(mimeType) |
35 | , payloadSize(payloadSize) |
36 | , originalName(std::move(originalFilename)) |
37 | { |
38 | if (!isValid()) |
39 | qCWarning(MESSAGES) |
40 | << "To client developers: using FileInfo(QUrl, qint64, ...) " |
41 | "constructor for non-mxc resources is deprecated since Quotient " |
42 | "0.7; for local resources, use FileInfo(QFileInfo) instead" ; |
43 | } |
44 | |
45 | FileInfo::FileInfo(FileSourceInfo sourceInfo, const QJsonObject& infoJson, |
46 | QString originalFilename) |
47 | : source(std::move(sourceInfo)) |
48 | , originalInfoJson(infoJson) |
49 | , mimeType( |
50 | QMimeDatabase().mimeTypeForName(nameOrAlias: infoJson["mimetype" _ls].toString())) |
51 | , payloadSize(fromJson<qint64>(jv: infoJson["size" _ls])) |
52 | , originalName(std::move(originalFilename)) |
53 | { |
54 | if (!mimeType.isValid()) |
55 | mimeType = QMimeDatabase().mimeTypeForData(data: QByteArray()); |
56 | } |
57 | |
58 | bool FileInfo::isValid() const |
59 | { |
60 | const auto& u = url(); |
61 | return u.scheme() == "mxc"_ls && QString(u.authority() + u.path()).count(c: u'/') == 1; |
62 | } |
63 | |
64 | QUrl FileInfo::url() const |
65 | { |
66 | return getUrlFromSourceInfo(fsi: source); |
67 | } |
68 | |
69 | QJsonObject Quotient::EventContent::toInfoJson(const FileInfo& info) |
70 | { |
71 | QJsonObject infoJson; |
72 | if (info.payloadSize != -1) |
73 | infoJson.insert(QStringLiteral("size" ), value: info.payloadSize); |
74 | if (info.mimeType.isValid()) |
75 | infoJson.insert(QStringLiteral("mimetype" ), value: info.mimeType.name()); |
76 | return infoJson; |
77 | } |
78 | |
79 | ImageInfo::ImageInfo(const QFileInfo& fi, QSize imageSize) |
80 | : FileInfo(fi), imageSize(imageSize) |
81 | {} |
82 | |
83 | ImageInfo::ImageInfo(FileSourceInfo sourceInfo, qint64 fileSize, |
84 | const QMimeType& type, QSize imageSize, |
85 | const QString& originalFilename) |
86 | : FileInfo(std::move(sourceInfo), fileSize, type, originalFilename) |
87 | , imageSize(imageSize) |
88 | {} |
89 | |
90 | ImageInfo::ImageInfo(FileSourceInfo sourceInfo, const QJsonObject& infoJson, |
91 | const QString& originalFilename) |
92 | : FileInfo(std::move(sourceInfo), infoJson, originalFilename) |
93 | , imageSize(infoJson["w" _ls].toInt(), infoJson["h" _ls].toInt()) |
94 | {} |
95 | |
96 | QJsonObject Quotient::EventContent::toInfoJson(const ImageInfo& info) |
97 | { |
98 | auto infoJson = toInfoJson(info: static_cast<const FileInfo&>(info)); |
99 | if (info.imageSize.width() != -1) |
100 | infoJson.insert(QStringLiteral("w" ), value: info.imageSize.width()); |
101 | if (info.imageSize.height() != -1) |
102 | infoJson.insert(QStringLiteral("h" ), value: info.imageSize.height()); |
103 | return infoJson; |
104 | } |
105 | |
106 | Thumbnail::Thumbnail(const QJsonObject& infoJson, |
107 | const Omittable<EncryptedFileMetadata>& efm) |
108 | : ImageInfo(QUrl(infoJson["thumbnail_url" _ls].toString()), |
109 | infoJson["thumbnail_info" _ls].toObject()) |
110 | { |
111 | if (efm) |
112 | source = *efm; |
113 | } |
114 | |
115 | void Thumbnail::dumpTo(QJsonObject& infoJson) const |
116 | { |
117 | if (url().isValid()) |
118 | fillJson(jo&: infoJson, jsonKeys: { "thumbnail_url"_ls , "thumbnail_file"_ls }, fsi: source); |
119 | if (!imageSize.isEmpty()) |
120 | infoJson.insert(QStringLiteral("thumbnail_info" ), |
121 | value: toInfoJson(info: *this)); |
122 | } |
123 | |