-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathactions.h
285 lines (246 loc) · 6.79 KB
/
actions.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#ifndef ACTIONS_H
#define ACTIONS_H
#include <QObject>
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <QRunnable>
#include <QDebug>
#include <QFuture>
#include <QFutureWatcher>
#include <aws/core/utils/ResourceManager.h>
namespace qlibs3 {
using namespace Aws;
using namespace Aws::S3;
/* from Aws::S3::TransferStatus */
enum class TransferStatus {
//this value is only used for directory synchronization
EXACT_OBJECT_ALREADY_EXISTS,
//Operation is still queued and has not begun processing
NOT_STARTED,
//Operation is now running
IN_PROGRESS,
//Operation was canceled. A Canceled operation can still be retried
CANCELED,
//Operation failed, A failed operaton can still be retried.
FAILED,
//Operation was successful
COMPLETED,
//Operation either failed or was canceled and a user deleted the multi-part upload from S3.
ABORTED
};
typedef Aws::S3::Model::Bucket s3bucket;
typedef Aws::S3::Model::Object s3object;
typedef Aws::Client::AWSError<S3Errors> s3error;
typedef Aws::S3::Model::CommonPrefix s3prefix;
QString AwsString2QString(const Aws::String &s);
Aws::String QString2AwsString(const QString &s);
#define BUFFERSIZE (5<<20) //5MB
class CommandAction: public QObject
{
Q_OBJECT
public:
CommandAction(QFuture<void> f, QObject *parent = 0): QObject(parent), future(f)
{
}
~CommandAction()
{
qDebug() << "command action is destoried";
}
explicit CommandAction(QObject *parent = 0): QObject(parent)
{
}
void setFuture(const QFuture<void> f);
void waitForFinished();
bool isFinished();
/*
s3error getError();
void setError(const s3error &);
private:
QMutex errMutex;
s3error err;
*/
/*
signals:
void finished(bool success, s3error err);
*/
protected:
QFuture<void> future;
};
class DeleteObjectAction : public CommandAction
{
Q_OBJECT
public:
explicit DeleteObjectAction(QObject *parent = 0): CommandAction(parent)
{
}
signals:
void DeleteObjectFinished(bool success, s3error error);
};
class ListBucketAction: public CommandAction
{
Q_OBJECT
public:
ListBucketAction(QFuture<void> f, QObject *parent = 0): CommandAction(f, parent)
{
}
explicit ListBucketAction(QObject *parent = 0): CommandAction(parent)
{
}
~ListBucketAction()
{
qDebug() << "listBucketAction is destoried";
}
signals:
void ListBucketInfo(s3bucket bucket);
void ListBucketFinished(bool success, s3error error);
};
class CreateBucketAction: public CommandAction
{
Q_OBJECT
public:
CreateBucketAction(QFuture<void> f, QObject *parent = 0): CommandAction(f, parent)
{
}
explicit CreateBucketAction(QObject *parent = 0): CommandAction(parent)
{
}
~CreateBucketAction()
{
qDebug() << "createBucketAction is destoried";
}
signals:
void CreateBucketFinished(bool success, s3error error);
};
class DeleteBucketAction: public CommandAction
{
Q_OBJECT
public:
DeleteBucketAction(QFuture<void> f, QObject *parent = 0): CommandAction(f, parent)
{
}
explicit DeleteBucketAction(QObject *parent = 0): CommandAction(parent)
{
}
~DeleteBucketAction()
{
qDebug() << "deleteBucketAction is destoried";
}
signals:
void DeleteBucketFinished(bool success, s3error error);
};
class ListObjectAction: public CommandAction
{
Q_OBJECT
public:
ListObjectAction(QFuture<void> f, QObject *parent = 0): CommandAction(f, parent)
{
};
explicit ListObjectAction(QObject *parent = 0): CommandAction(parent)
{
};
~ListObjectAction()
{
qDebug() << "listObjectAction is destoried";
}
signals:
void ListObjectInfo(s3object object, QString bucketName);
void ListPrefixInfo(s3prefix prefix, QString bucketName);
void ListObjectFinished(bool success, s3error error, bool truncated, QString nextMarker);
};
class PutObjectAction : public CommandAction
{
Q_OBJECT
public:
PutObjectAction(QFuture<void> f, QObject *parent = 0) : CommandAction(f, parent) {}
explicit PutObjectAction(QObject *parent = 0) : CommandAction(parent) {}
~PutObjectAction()
{
qDebug() << "PutObjectAction is destroyed";
}
signals:
void PutObjectFinished(bool success, s3error error);
};
class ObjectHandlerInterface: public QObject
{
Q_OBJECT
public:
ObjectHandlerInterface(QObject *parent = 0): QObject(parent)
{
}
virtual int start() = 0;
virtual void stop() = 0;
virtual void waitForFinish() = 0;
virtual ~ObjectHandlerInterface() = default;
signals:
void updateProgress(uint64_t, uint64_t);
void updateStatus(TransferStatus);
void finished(bool, s3error);
};
//for multipart uploads
struct PartState {
Aws::String etag;
int partID;
uint64_t rangeBegin;
uint64_t sizeInBytes;
bool success;
};
using PartStateMap = QMap<int, PartState *>;
class UploadObjectHandler: public ObjectHandlerInterface
{
Q_OBJECT
public:
UploadObjectHandler(QObject *parent, std::shared_ptr<S3Client> client, QString bucketName,
QString keyName, QString readFile, QString contentType);
~UploadObjectHandler()
{
qDebug() << "UploadObjectHandler: " << m_keyName.c_str() << "destoried";
}
int start() Q_DECL_OVERRIDE;
void stop() Q_DECL_OVERRIDE;
void waitForFinish() Q_DECL_OVERRIDE;
private:
bool shouldContinue();
void doUpload();
void doMultipartUpload(const std::shared_ptr<Aws::IOStream> &fileStream);
void doSinglePartUpload(const std::shared_ptr<Aws::IOStream> &fileStream);
std::shared_ptr<S3Client> m_client;
Aws::String m_bucketName;
Aws::String m_keyName;
QString m_readFile;
Aws::String m_contenttype;
std::atomic<long> m_status;
std::atomic<bool> m_cancel;
long long m_totalSize;
long long m_totalTransfered;
QFuture<void> future;
Aws::String m_uploadId;
PartStateMap m_queueMap;
};
class DownloadObjectHandler: public ObjectHandlerInterface
{
Q_OBJECT
public:
DownloadObjectHandler(QObject *parent, std::shared_ptr<S3Client> client, const QString &bucketName,
const QString &keyName, const QString &writeToFile);
int start() Q_DECL_OVERRIDE;
void stop() Q_DECL_OVERRIDE;
void waitForFinish() Q_DECL_OVERRIDE;
~DownloadObjectHandler()
{
qDebug() << "DownloadObjectHandler: " << m_keyName.c_str() << "Destoried";
}
private:
void doDownload();
std::shared_ptr<S3Client> m_client;
Aws::String m_bucketName;
Aws::String m_keyName;
QString m_writeToFile;
std::atomic<long> m_status;
std::atomic<bool> m_cancel;
uint64_t m_totalSize;
uint64_t m_totalTransfered;
QFuture<void> future;
};
} //end qlibs3
#endif // ACTIONS_H