-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentHubBridge.cpp
More file actions
72 lines (59 loc) · 1.92 KB
/
Copy pathContentHubBridge.cpp
File metadata and controls
72 lines (59 loc) · 1.92 KB
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
#include "ContentHubBridge.h"
#include <QCryptographicHash>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QRegExp>
#include <QStandardPaths>
ContentHubBridge::ContentHubBridge(QObject *parent)
: QObject(parent)
{
}
QString ContentHubBridge::readTextFile(const QUrl &url) const
{
if (!url.isLocalFile()) {
return QString();
}
QFile file(url.toLocalFile());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return QString();
}
return QString::fromUtf8(file.readAll());
}
QUrl ContentHubBridge::writeSharedTextFile(const QString &title, const QString &content) const
{
if (content.isEmpty()) {
return QUrl();
}
const QString basePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (basePath.isEmpty()) {
return QUrl();
}
QDir dir(basePath);
if (!dir.mkpath(QStringLiteral("ContentHubOutgoing"))) {
return QUrl();
}
if (!dir.cd(QStringLiteral("ContentHubOutgoing"))) {
return QUrl();
}
QString safeTitle = title.trimmed();
if (safeTitle.isEmpty()) {
safeTitle = QStringLiteral("shared-note");
}
safeTitle.replace(QRegExp(QStringLiteral("[^A-Za-z0-9._-]+")), QStringLiteral("-"));
safeTitle = safeTitle.left(48).trimmed();
if (safeTitle.isEmpty()) {
safeTitle = QStringLiteral("shared-note");
}
const QByteArray digest = QCryptographicHash::hash(
(content + QString::number(QDateTime::currentMSecsSinceEpoch())).toUtf8(),
QCryptographicHash::Sha1).toHex().left(10);
const QString filePath = dir.filePath(QStringLiteral("%1-%2.txt").arg(safeTitle, QString::fromLatin1(digest)));
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
return QUrl();
}
file.write(content.toUtf8());
file.close();
return QUrl::fromLocalFile(filePath);
}