-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMulPaField.h
91 lines (72 loc) · 2.06 KB
/
MulPaField.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
#pragma once
#include <map>
#include <string>
#include <vector>
#include <fstream>
#include <functional>
#include <FlowUtils/FlowParser.h>
class MulPaField : public std::map<std::string, std::string> {
public:
~MulPaField() {
if (_fileIsOpen) {
_file->close();
delete _file;
}
}
std::string Header(const std::string &key) {
auto itr = this->find(key);
if (itr == this->end())
return "";
return itr->second;
}
void addHeader(const std::string &key, const std::string &value) {
this->operator[](key) = value;
}
std::string Name() {
if (_name.empty()) {
_name = FlowParser::between(Header("Content-Disposition"), "name=\"", "\"");
}
return _name;
}
std::string FileName() {
if (_filename.empty()) {
_filename = FlowParser::between(Header("Content-Disposition"), "filename=\"", "\"");
}
return _filename;
}
std::ofstream *File() {
return _file;
}
std::ofstream *OpenIn(const std::string &basePath, const std::function<std::string(MulPaField*)> &function) {
_fileIsOpen = true;
if (function != nullptr) {
FullPath = function(this);
} else {
if (basePath.empty() || basePath.at(basePath.size() - 1) == '/' ||
basePath.at(basePath.size() - 1) == '\\') {
FullPath = basePath + FileName();
} else {
FullPath = basePath + '/' + FileName();
}
}
_file = new std::ofstream(FullPath, std::ofstream::out | std::ofstream::binary);
return _file;
}
bool FileIsOpen() {
return _fileIsOpen;
}
void CloseFile() {
if (_fileIsOpen) {
_fileIsOpen = false;
_file->close();
delete _file;
}
}
std::vector<unsigned char> data;
std::string FullPath;
private:
std::ofstream *_file;
bool _fileIsOpen = false;
std::string _name;
std::string _filename;
};