22#define ATTACHMENT_H
33#include " Poco/Net/PartSource.h"
44#include " Utils.h"
5- #include < qstring.h >
5+ #include < QString >
66#include < iostream>
7+ #include < fstream>
8+ #include < QVariant>
9+ #include < QSharedPointer>
10+ #include < QDebug>
11+ #include < QByteArray>
12+ #include < QFile>
13+ #include " Exception/MailClientException.h"
14+ #include " Exception/MailGenerationException.h"
15+
16+ using namespace std ;
717
818class Attachment : public Poco ::Net::PartSource
919{
20+ private:
21+ typedef enum {unDownloaded, isDownloading, isDownloaded} downloadState;
22+
1023 Utils::MediaType _attachmentType;
11- size_t _fileSize;
24+ qint64 _fileSize;
25+ qint64 _hasDownloaded;
1226 QString _fileName;
27+ QString _filePath;
28+ downloadState _state;
1329 QString _accessCommand;
30+ fstream _fileStream;
31+
32+ const static qint64 _fileMaxSize = 1024 *1024 ;
33+
34+ int findPostfix (const QString &src) {
35+ int i = src.length () - 1 ;
36+ while (src[i]!=' .' ) i--;
37+ return i;
38+ }
1439
1540public:
41+
42+ std::istream& stream () override {
43+ return _fileStream;
44+ }
45+
1646 Attachment (const std::string& mediaType):
17- _attachmentType (mediaType) {}
47+ _attachmentType (mediaType) {
48+ _state = unDownloaded;
49+ _hasDownloaded = 0 ;
50+ }
1851
1952 Attachment (const std::string& type, const std::string& subType):
20- _attachmentType (type, subType) {}
53+ _attachmentType (type, subType) {
54+ _state = unDownloaded;
55+ _hasDownloaded = 0 ;
56+ }
2157
2258 Attachment (const Utils::MediaType& mediaType):
23- _attachmentType (mediaType) {}
59+ _attachmentType (mediaType) {
60+ _state = unDownloaded;
61+ _hasDownloaded = 0 ;
62+ }
2463
2564 Utils::MediaType getAttachmentType () const {
2665 return _attachmentType;
@@ -38,20 +77,75 @@ class Attachment : public Poco::Net::PartSource
3877 return _fileName;
3978 }
4079
41- void setFileName (QString i) {
80+ void setFileName (const QString& i) {
4281 _fileName = i;
4382 }
4483
84+ QString getFilePath () const {
85+ return _filePath;
86+ }
87+
88+ void setFilePath (const QString& i) {
89+ _filePath = i;
90+ _fileStream.open (_filePath.toStdString (), ios::in|ios::out|ios::binary);
91+ }
92+
4593 QString getAccessCommand () const {
4694 return _accessCommand;
4795 }
4896
49- void setAccessCommand (QString i) {
97+ void setAccessCommand (const QString& i) {
5098 _accessCommand = i;
5199 }
52100
53- bool Download (QByteArray a) {
101+ bool Download (const QByteArray& data) {
102+
103+ QString tmp = _filePath;
104+ int t = findPostfix (tmp);
105+ QString postfix = tmp.mid (t, tmp.length ()-t);
106+ tmp = tmp.mid (0 ,t);
107+ if (_state == unDownloaded) { // make sure the file name is not existed
108+ for (int i = 1 ; QFile::exists (_filePath); i++) {
109+ _filePath = tmp + " (" ;
110+ _filePath += i + ' 0' ;
111+ _filePath += " )" ;
112+ _filePath += postfix;
113+ }
114+ _state = isDownloading;
115+ }
116+
117+ QFile output (_filePath);
118+ output.open (QIODevice::WriteOnly|QIODevice::Append);
119+ qint64 size = data.size ();
120+ output.write (data.data (), size);
121+ output.close ();
122+
123+ _hasDownloaded += size;
124+ if (_hasDownloaded == _fileSize)
125+ _state = isDownloaded;
126+ return true ;
127+ }
128+
129+ bool Upload (QByteArray& data) {
130+ QFile input (_filePath);
131+
132+ try {
133+ if (input.isOpen ())
134+ throw (MailGenerationException (" File is being occupied!" ));
135+ if (!input.open (QIODevice::ReadOnly)) // file not existed
136+ throw (MailGenerationException (" File doesn't exists!" ));
137+ qint64 size = input.size ();
138+ if (size > _fileMaxSize)
139+ throw (MailGenerationException (" File size is beyond limit!" ));
140+ } catch (MailGenerationException& mse) {
141+ qDebug () << mse.what () << endl;
142+ return false ;
143+ }
144+
145+ data = input.readAll ();
54146
147+ input.close ();
148+ return true ;
55149 }
56150
57151};
0 commit comments