-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileIO.cpp
131 lines (101 loc) · 2.66 KB
/
FileIO.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
#include "FileIO.hpp"
FileIO::FileIO()
{
}
bool FileIO::write(const QString& source, const QString& data)
{
if (source.isEmpty())
return false;
QFile file(source);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;
QTextStream out(&file);
out << data;
file.close();
return true;
}
bool FileIO::decodeAndWrite(const QString& source, const QString& data)
{
if (source.isEmpty())
return false;
QFile file(source);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;
QByteArray bytes;
bytes.append(data);
file.write(QByteArray::fromBase64(bytes));
file.close();
return true;
}
bool FileIO::setUserAvatar(const QString &user_key, const QString &data)
{
if(!m_tmpAvatarDir.isValid()) {
return false;
}
QImage image;
QByteArray dataBytes, resultBytes;
QBuffer buffer(&resultBytes, this);
QString avatar_path = this->getAvatarPath(user_key);
dataBytes.append(data);
// Resize image
image.loadFromData(QByteArray::fromBase64(dataBytes));
QImage(image.scaled(128, 128, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)).save(&buffer, "JPG");
buffer.open(QIODevice::WriteOnly);
return this->decodeAndWrite(avatar_path, resultBytes.toBase64());
}
bool FileIO::hasAvatar(const QString &user_key)
{
return QFile::exists(this->getAvatarPath(user_key));
}
QString FileIO::getAvatarPath(const QString &user_key)
{
if(!m_tmpAvatarDir.isValid()) {
return 0;
}
if(user_key == 0) {
return this->m_tmpAvatarDir.path();
}
// Note that FileIO::setUserAvatar() method will generate JPG files
return m_tmpAvatarDir.path().append("/").append(user_key).append(".jpg");
}
QUrl FileIO::getAvatarUrl(const QString &user_key)
{
return QUrl::fromLocalFile(this->getAvatarPath(user_key));
}
QString FileIO::getImagePath(const QString &name)
{
if(!m_tmpImageDir.isValid()) {
return 0;
}
return m_tmpImageDir.path().append("/").append(name);
}
QString FileIO::decodeImage(const QString &data)
{
if(data.isEmpty()) {
return "";
}
if(!m_tmpImageDir.isValid()) {
return 0;
}
QString image_name(this->getMd5Hash(data));
QString image_path(this->getImagePath(image_name));
// Check to see if file exist already
if(QFile::exists(image_path)) {
return QUrl::fromLocalFile(image_path).toString();
}
// If file is not exist yet, create it
if(this->decodeAndWrite(image_path, data)) {
return QUrl::fromLocalFile(image_path).toString();
}
return 0;
}
QString FileIO::getNewUserKey()
{
return QUuid::createUuid().toString();
}
QString FileIO::getMd5Hash(const QString &data)
{
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(data.toUtf8());
return hash.result().toHex();
}