forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIncoming.cpp
More file actions
108 lines (82 loc) · 2.48 KB
/
FileIncoming.cpp
File metadata and controls
108 lines (82 loc) · 2.48 KB
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
#include "FileIncoming.h"
#include "../utils/Utils.h"
#include <fstream>
namespace Transfer
{
FileIncoming::FileIncoming(std::string &&fileTmpName, std::string &&fileName, std::string &&fileType, const size_t fileSize) noexcept
: file_tmp_name(std::move(fileTmpName) ), file_name(std::move(fileName) ),
file_type(std::move(fileType) ), file_size(fileSize)
{
}
FileIncoming::FileIncoming(const FileIncoming &obj)
: file_tmp_name(obj.file_tmp_name), file_name(obj.file_name),
file_type(obj.file_type), file_size(obj.file_size)
{
}
FileIncoming::FileIncoming(FileIncoming &&obj) noexcept
: file_tmp_name(std::move(obj.file_tmp_name) ), file_name(std::move(obj.file_name) ),
file_type(std::move(obj.file_type) ), file_size(obj.file_size)
{
obj.file_size = 0;
}
const std::string &FileIncoming::getTmpName() const noexcept
{
return this->file_tmp_name;
}
const std::string &FileIncoming::getName() const noexcept
{
return this->file_name;
}
const std::string &FileIncoming::getType() const noexcept
{
return this->file_type;
}
size_t FileIncoming::getSize() const noexcept
{
return this->file_size;
}
bool FileIncoming::isExists() const noexcept
{
std::ifstream file(this->file_tmp_name, std::ifstream::binary);
const bool is_exists = file.good();
file.close();
return is_exists;
}
};
namespace Utils
{
void packFilesIncoming(std::vector<char> &buf, const std::unordered_multimap<std::string, Transfer::FileIncoming> &map)
{
packNumber(buf, map.size() );
for (auto it = map.cbegin(); map.cend() != it; ++it)
{
packString(buf, it->first);
const Transfer::FileIncoming &file = it->second;
packString(buf, file.getTmpName() );
packString(buf, file.getName() );
packString(buf, file.getType() );
packNumber(buf, file.getSize() );
}
}
const uint8_t *unpackFilesIncoming(std::unordered_multimap<std::string, Transfer::FileIncoming> &map, const uint8_t *src)
{
size_t count;
src = unpackNumber(&count, src);
for (size_t i = 0; i < count; ++i)
{
std::string key;
src = unpackString(key, src);
std::string file_tmp_name;
src = unpackString(file_tmp_name, src);
std::string file_name;
src = unpackString(file_name, src);
std::string file_type;
src = unpackString(file_type, src);
size_t file_size;
src = unpackNumber(&file_size, src);
map.emplace(std::move(key), Transfer::FileIncoming(std::move(file_tmp_name), std::move(file_name), std::move(file_type), file_size) );
}
return src;
}
};