-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudsync.cpp
174 lines (139 loc) · 5.38 KB
/
cloudsync.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
#include "cloudsync.h"
#include <QDir>
#include <QDebug>
#include "qts3.h"
int cloudSyncFilesWork(cloudSyncData_t* data, QStringList &files)
{
int failed = 0;
QtS3 s3(data->S3Access,data->S3Secret);
QStringList headers;
headers << "Content-Type: image/jpeg";
for (int i = 0; i < files.size(); i++) {
qDebug() << "Processing file:" << files.at(i);
QFile srcFile(data->filesDirName + "/" + files.at(i));
if (srcFile.open(QIODevice::ReadOnly)) {
QByteArray fileData = srcFile.readAll();
srcFile.close();
QtS3Reply<void> putReply = s3.put(data->S3Bucket.toLocal8Bit() ,files.at(i) ,fileData ,headers);
if (putReply.isSuccess()) {
qDebug() << "Put successful deleting local copy";
srcFile.remove();
} else {
qDebug() << "Put failed keep for next time through:" << putReply.anyErrorString();
failed++;
}
} else {
qDebug() << "Error opening file" << srcFile.fileName();
failed++;
}
}
return failed;
}
int cloudSyncEmailsWork(cloudSyncData_t* data, QStringList &emails)
{
int failed = 0;
for (int i = 0; i < emails.size(); i++) {
qDebug() << "Processing email:" << emails.at(i);
QFile file(data->emailDirName + "/" + emails.at(i));
if (file.open(QIODevice::ReadOnly)) {
// First we need to create an SmtpClient object
// We will use the Gmail's smtp server (smtp.gmail.com, port 465, ssl)
// qDebug() << "emailServer" << data->emailServer;
// qDebug() << "emailPort" << data->emailPort;
// qDebug() << "emailUser" << data->emailUser;
// qDebug() << "emailFrom" << data->emailFrom;
// qDebug() << "emailDomain" << data->emailDomain;
// qDebug() << "emailPassword" << data->emailPassword;
// qDebug() << "emailTransport" << data->emailTransport;
SmtpClient smtp(data->emailServer, data->emailPort, SmtpClient::TcpConnection);
if (data->emailTransport == QString("SSL")) {
smtp.setConnectionType(SmtpClient::SslConnection);
} else if (data->emailTransport == QString("TLS")) {
smtp.setConnectionType(SmtpClient::TlsConnection);
}
// We need to set the username (your email address) and password
// for smtp authentification.
smtp.setUser(data->emailUser);
smtp.setPassword(data->emailPassword);
smtp.setName(data->emailDomain);
smtp.setAuthMethod(SmtpClient::AuthLogin);
// Now we create a MimeMessage object. This is the email.
MimeMessage message;
EmailAddress sender(data->emailUser, data->emailFrom);
message.setSender(&sender);
QString rcptTo;
rcptTo = file.readLine();
rcptTo.chop(1);
EmailAddress to(rcptTo, rcptTo);
message.addRecipient(&to);
EmailAddress bcc(data->emailUser, data->emailUser);
message.addBcc(&bcc);
message.setSubject(data->emailSubject);
// Now add some text to the email.
// First we create a MimeText object.
QString msgText;
msgText += data->emailPreamble;
while (!file.atEnd()) {
QString destFileName = file.readLine();
msgText += data->S3URL + data->S3Bucket + "/" + destFileName;
}
msgText += data->emailPostamble;
qDebug() << "Message: " << msgText;
MimeText text(msgText);
// Now add it to the mail
message.addPart(&text);
// Now we can send the mail
if (!smtp.connectToHost()) {
qDebug() << "Failed to connect to host!" << endl;
failed ++;
goto abort;
}
if (!smtp.login()) {
qDebug() << "Failed to login!" << endl;
failed ++;
goto abort;
}
if (!smtp.sendMail(message)) {
qDebug() << "Failed to send mail!" << endl;
failed ++;
goto abort;
}
smtp.quit();
qDebug() << "Successfully staged E-Mail";
file.close();
file.remove();
} else {
qDebug() << "Error opening file" << file.fileName();
failed ++;
}
}
abort:
return failed;
}
void cloudSyncWork (cloudSyncData_t* data)
{
//qDebug() << "cloudSyncWork";
QDir filesDir(data->filesDirName);
QDir emailDir(data->emailDirName);
// First process any pending images to send to the cloud
QStringList files = filesDir.entryList(QDir::Files);
bool allSunk = true;
if (cloudSyncFilesWork(data, files)) allSunk = false;
// If all the files are uploaded send any pending E-Mails
if (allSunk && !data->emailServer.isEmpty() && !data->emailDomain.isEmpty() && !data->emailUser.isEmpty() && !data->emailFrom.isEmpty() && !data->emailPassword.isEmpty()) {
files = emailDir.entryList(QDir::Files);
cloudSyncEmailsWork(data, files);
}
data->working = false;
}
cloudSyncThread::cloudSyncThread() :
QThread()
{
}
void cloudSyncThread::run()
{
while(1) {
cloudSyncWork(&data);
sleep(30);
}
}