This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.cpp
207 lines (206 loc) · 6.71 KB
/
backend.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "backend.h"
#include "daemon.h"
ZBackend::ZBackend()
: mediaSourceId(0)
{
initPath();
initMedia();
netManager = new QNetworkAccessManager;
}
ZBackend::~ZBackend()
{
// qDebug() << "call ZBackend::~ZBackend()";
saveMainFile(Daemon::instance()->exportNotes());
saveMediaFile();
}
void ZBackend::initPath()
{ // now just implemented mainfile
qDebug() << "call ZBackend::initPath";
bool chk = true;
for (auto i : { MainFile, MediaSourceFile, ConfigFile }) {
QDir dir(storagePath(i));
if (!dir.exists()) {
qDebug() << "Path" << i << "does not exists, init now";
dir.mkpath(storagePath(i));
chk = false;
}
}
if (chk)
return;
QFile oldVersion("stickyNotes.json");
if (oldVersion.exists()) {
qDebug() << "oldVersion detected at" << QDir::currentPath() + '/' + oldVersion.fileName() << ", transform now";
oldVersion.rename(storageFileFullName());
}
}
void ZBackend::initMedia()
{
qDebug() << "call ZBackend::initMedia()";
qDebug() << readWholeFile(storageFileFullName(MediaFile));
QFile list(storageFileFullName(MediaFile));
if (!list.open(QFile::ReadOnly | QFile::Text))
return;
// qDebug() << list.readLine().simplified().toInt();
mediaSourceId = list.readLine().simplified().toInt();
while (!list.atEnd()) {
auto line = list.readLine().simplified().split(' ');
media[QByteArray::fromBase64(line[0])] = QString::fromLocal8Bit(line[1]);
}
qDebug() << media.size() << "media records read";
// qDebug() << calcFileMd5("/home/zyc/Pictures/Wallpapers/灵笼/8ae42dc968d807969365c4f0607275afa4439057.png").toBase64();
list.close();
}
void ZBackend::saveMediaFile()
{
QFile output(storageFileFullName(MediaFile));
output.open(QFile::WriteOnly | QFile::Text);
QTextStream outputStream(&output);
outputStream << mediaSourceId << endl;
for (auto i = media.begin(); i != media.end(); i++) {
outputStream << QString::fromLocal8Bit(i.key().toBase64()) << ' ' << i.value() << endl;
}
output.close();
}
void ZBackend::saveMainFile(const QList<ZNote>& src)
{ //下一步实现双文件储存,防数据遗失
qDebug() << "saving mainfile...";
QFile outputTemp(storagePath() + '~' + storageFileName());
// QFile output(storageFileFullName());
outputTemp.open(QFile::WriteOnly | QFile::Text);
QTextStream outputStream(&outputTemp);
outputStream << exportAsJson(src).toJson(QJsonDocument::Compact);
outputTemp.close();
QFile::remove(storageFileFullName());
QFile::copy(storagePath() + '~' + storageFileName(), storageFileFullName());
}
inline int ZBackend::generateFileID()
{
return ++mediaSourceId;
}
QString ZBackend::insertImage(const QUrl& fileUrl)
{
if (fileUrl.isLocalFile()) {
auto hash = calcFileMd5(fileUrl.path());
if (!media.contains(hash)) {
qDebug() << "file" << fileUrl << "not found in hashTable, now insert";
QString filename;
forever
{
filename = QString::number(generateFileID());
if (QFile::copy(fileUrl.path(), storagePath(MediaSourceFile) + filename))
break;
}
qDebug() << "copy" << fileUrl.path() << "to" << storagePath(MediaSourceFile) + filename;
media[hash] = filename;
}
return hash.toBase64();
} else {
}
return "";
}
QString ZBackend::insertImage(const QByteArray& data)
{
auto hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
if (!media.contains(hash)) {
auto filename = QString::number(generateFileID());
QFile file(storagePath(MediaSourceFile) + filename);
file.open(QFile::WriteOnly);
file.write(data);
file.close();
media[hash] = filename;
}
return hash.toBase64();
}
QByteArray ZBackend::queryImage(const QString& hash)
{
if (media.contains(QByteArray::fromBase64(hash.toLocal8Bit()))) {
auto filename = media[QByteArray::fromBase64(hash.toLocal8Bit())];
QFile file(storagePath(MediaSourceFile) + filename);
file.open(QFile::ReadOnly);
return file.readAll();
} else {
return QByteArray();
}
}
QList<ZNote> ZBackend::getSavedDataList() const
{
qDebug() << "load saved data from" << storageFileFullName();
auto objList = QVariant(QJsonDocument::fromJson(readWholeFile(storageFileFullName()).toUtf8()).array().toVariantList()).toList();
QList<ZNote> rt;
for (auto i : objList) {
auto obj = i.toJsonObject();
rt.push_back(ZNote(obj));
}
return rt;
}
inline QString ZBackend::storageFileFullName(int type)
{
return storagePath(type) + storageFileName(type);
}
inline QString ZBackend::storageFileName(int type)
{
switch (type) {
case MainFile:
return "stickyNotes.json";
case BackupFile:
return "~stickyNotes.json";
case ConfigFile:
return "stickyNotes.conf";
case MediaFile:
return "stickyNotes.map";
default:
return "";
}
}
inline QString ZBackend::storagePath(int type)
{
switch (type) {
case BackupFile:
case MainFile:
case MediaFile:
return QString("%1/").arg(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
case MediaSourceFile:
return QString("%1/resources/").arg(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
case ConfigFile:
return QString("%1/%2/%3/")
.arg(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation), qApp->organizationName(), qApp->applicationName());
default:
return "";
}
}
inline QString ZBackend::readWholeFile(const QString& filename)
{
QFile input(filename);
input.open(QFile::ReadOnly | QFile::Text);
if (!input.isOpen())
return QString();
QTextStream inputStream(&input);
QString rt = inputStream.readAll();
// qDebug()<<"import file content:\n"<<rt;
input.close();
return rt;
}
QJsonDocument ZBackend::exportAsJson(const QList<ZNote>& src)
{
QJsonArray array;
for (auto i : src)
array.append(QJsonValue(i.jsonObject()));
return QJsonDocument(array);
}
inline QByteArray ZBackend::calcFileMd5(const QString& filename)
{
QFile file(filename);
file.open(QIODevice::ReadOnly);
return QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5);
}
QByteArray ZBackend::download(const QString& url)
{
auto loop = new QEventLoop;
QNetworkReply* reply = netManager->get(QNetworkRequest(QUrl(url)));
// qDebug() << reply->url();
QObject::connect(reply, &QNetworkReply::finished, loop, &QEventLoop::quit);
loop->exec();
reply->deleteLater();
auto rt = reply->readAll();
return rt;
}