-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.cpp
213 lines (182 loc) · 6.48 KB
/
Field.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
208
209
210
211
212
213
// This file is distributed under GPLv3 licence
// Author: Gorelov Grigory ([email protected])
//
// Contacts and other info are on the WEB page: grigory.info/MPFDParser
#include "includes/Field.h"
#include "includes/Parser.h"
MPFD::Field::Field() {
type = 0;
FieldContent = NULL;
FieldContentLength = 0;
}
MPFD::Field::~Field() {
if (FieldContent) {
delete FieldContent;
}
if (type == FileType) {
if (file.is_open()) {
file.close();
remove((TempDir + "/" + TempFile).c_str());
}
}
}
void MPFD::Field::SetType(int type) {
if ((type == TextType) || (type == FileType)) {
this->type = type;
} else {
throw MPFD::Exception("Trying to set type of field, but type is incorrect.");
}
}
int MPFD::Field::GetType() {
if (type > 0) {
return type;
} else {
throw MPFD::Exception("Trying to get type of field, but no type was set.");
}
}
void MPFD::Field::AcceptSomeData(char *data, long length) {
if (type == TextType) {
if (FieldContent == NULL) {
FieldContent = new char[length + 1];
} else {
FieldContent = (char*) realloc(FieldContent, FieldContentLength + length + 1);
}
memcpy(FieldContent + FieldContentLength, data, length);
FieldContentLength += length;
FieldContent[FieldContentLength] = 0;
} else if (type == FileType) {
if (WhereToStoreUploadedFiles == Parser::StoreUploadedFilesInFilesystem) {
if (TempDir.length() > 0) {
if (!file.is_open()) {
int i = 1;
std::ifstream testfile;
std::string tempfile;
do {
if (testfile.is_open()) {
testfile.close();
}
std::stringstream ss;
ss << "MPFD_Temp_" << i;
TempFile = ss.str();
tempfile = TempDir + "/" + TempFile;
testfile.open(tempfile.c_str(), std::ios::in);
i++;
} while (testfile.is_open());
file.open(tempfile.c_str(), std::ios::out | std::ios::binary | std::ios_base::trunc);
}
if (file.is_open()) {
file.write(data, length);
file.flush();
} else {
throw Exception(std::string("Cannot write to file ") + TempDir + "/" + TempFile);
}
} else {
throw MPFD::Exception("Trying to AcceptSomeData for a file but no TempDir is set.");
}
} else { // If files are stored in memory
if (FieldContent == NULL) {
FieldContent = new char[length];
} else {
FieldContent = (char*) realloc(FieldContent, FieldContentLength + length);
}
memcpy(FieldContent + FieldContentLength, data, length);
FieldContentLength += length;
}
} else {
throw MPFD::Exception("Trying to AcceptSomeData but no type was set.");
}
}
void MPFD::Field::SetTempDir(std::string dir) {
TempDir = dir;
}
unsigned long MPFD::Field::GetFileContentSize() {
if (type == 0) {
throw MPFD::Exception("Trying to get file content size, but no type was set.");
} else {
if (type == FileType) {
if (WhereToStoreUploadedFiles == Parser::StoreUploadedFilesInMemory) {
return FieldContentLength;
} else {
throw MPFD::Exception("Trying to get file content size, but uploaded files are stored in filesystem.");
}
} else {
throw MPFD::Exception("Trying to get file content size, but the type is not file.");
}
}
}
char * MPFD::Field::GetFileContent() {
if (type == 0) {
throw MPFD::Exception("Trying to get file content, but no type was set.");
} else {
if (type == FileType) {
if (WhereToStoreUploadedFiles == Parser::StoreUploadedFilesInMemory) {
return FieldContent;
} else {
throw MPFD::Exception("Trying to get file content, but uploaded files are stored in filesystem.");
}
} else {
throw MPFD::Exception("Trying to get file content, but the type is not file.");
}
}
}
std::string MPFD::Field::GetTextTypeContent() {
if (type == 0) {
throw MPFD::Exception("Trying to get text content of the field, but no type was set.");
} else {
if (type != TextType) {
throw MPFD::Exception("Trying to get content of the field, but the type is not text.");
} else {
if (FieldContent == NULL) {
return std::string();
} else {
return std::string(FieldContent);
}
}
}
}
std::string MPFD::Field::GetTempFileName() {
if (type == 0) {
throw MPFD::Exception("Trying to get file temp name, but no type was set.");
} else {
if (type == FileType) {
if (WhereToStoreUploadedFiles == Parser::StoreUploadedFilesInFilesystem) {
return std::string(TempDir + "/" + TempFile);
} else {
throw MPFD::Exception("Trying to get file temp name, but uplaoded files are stored in memory.");
}
} else {
throw MPFD::Exception("Trying to get file temp name, but the type is not file.");
}
}
}
std::string MPFD::Field::GetFileName() {
if (type == 0) {
throw MPFD::Exception("Trying to get file name, but no type was set.");
} else {
if (type == FileType) {
return FileName;
} else {
throw MPFD::Exception("Trying to get file name, but the type is not file.");
}
}
}
void MPFD::Field::SetFileName(std::string name) {
FileName = name;
}
void MPFD::Field::SetUploadedFilesStorage(int where) {
WhereToStoreUploadedFiles = where;
}
void MPFD::Field::SetFileContentType(std::string type) {
FileContentType = type;
}
std::string MPFD::Field::GetFileMimeType() {
if (type == 0) {
throw MPFD::Exception("Trying to get mime type of file, but no type was set.");
} else {
if (type != FileType) {
throw MPFD::Exception("Trying to get mime type of the field, but the type is not File.");
} else {
return std::string(FileContentType);
}
}
}