3
3
#include " random"
4
4
#include " string"
5
5
#include < algorithm>
6
+ #include < drogon/HttpClient.h>
6
7
#include < drogon/HttpResponse.h>
8
+ #include < fstream>
7
9
#include < iostream>
8
10
#include < ostream>
9
11
#include < regex>
12
+ #include < vector>
10
13
// Include platform-specific headers
11
14
#ifdef _WIN32
12
15
#include < winsock2.h>
@@ -32,6 +35,90 @@ inline std::string extractBase64(const std::string &input) {
32
35
return " " ;
33
36
}
34
37
38
+ // Helper function to encode data to Base64
39
+ inline std::string base64Encode (const std::vector<unsigned char > &data) {
40
+ static const char encodingTable[] =
41
+ " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
42
+ std::string encodedData;
43
+ int i = 0 ;
44
+ int j = 0 ;
45
+ unsigned char array3[3 ];
46
+ unsigned char array4[4 ];
47
+
48
+ for (unsigned char c : data) {
49
+ array3[i++] = c;
50
+ if (i == 3 ) {
51
+ array4[0 ] = (array3[0 ] & 0xfc ) >> 2 ;
52
+ array4[1 ] = ((array3[0 ] & 0x03 ) << 4 ) + ((array3[1 ] & 0xf0 ) >> 4 );
53
+ array4[2 ] = ((array3[1 ] & 0x0f ) << 2 ) + ((array3[2 ] & 0xc0 ) >> 6 );
54
+ array4[3 ] = array3[2 ] & 0x3f ;
55
+
56
+ for (i = 0 ; i < 4 ; i++)
57
+ encodedData += encodingTable[array4[i]];
58
+ i = 0 ;
59
+ }
60
+ }
61
+
62
+ if (i) {
63
+ for (j = i; j < 3 ; j++)
64
+ array3[j] = ' \0 ' ;
65
+
66
+ array4[0 ] = (array3[0 ] & 0xfc ) >> 2 ;
67
+ array4[1 ] = ((array3[0 ] & 0x03 ) << 4 ) + ((array3[1 ] & 0xf0 ) >> 4 );
68
+ array4[2 ] = ((array3[1 ] & 0x0f ) << 2 ) + ((array3[2 ] & 0xc0 ) >> 6 );
69
+
70
+ for (j = 0 ; j < i + 1 ; j++)
71
+ encodedData += encodingTable[array4[j]];
72
+
73
+ while (i++ < 3 )
74
+ encodedData += ' =' ;
75
+ }
76
+
77
+ return encodedData;
78
+ }
79
+
80
+ // Function to load an image and convert it to Base64
81
+ inline std::string imageToBase64 (const std::string &imagePath) {
82
+ std::ifstream imageFile (imagePath, std::ios::binary);
83
+ if (!imageFile.is_open ()) {
84
+ throw std::runtime_error (" Could not open the image file." );
85
+ }
86
+
87
+ std::vector<unsigned char > buffer (std::istreambuf_iterator<char >(imageFile),
88
+ {});
89
+ return base64Encode (buffer);
90
+ }
91
+
92
+ // Helper function to generate a unique filename
93
+ inline std::string generateUniqueFilename (const std::string &prefix,
94
+ const std::string &extension) {
95
+ // Get current time as a timestamp
96
+ auto now = std::chrono::system_clock::now ();
97
+ auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
98
+ auto epoch = now_ms.time_since_epoch ();
99
+ auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
100
+
101
+ // Generate a random number
102
+ std::random_device rd;
103
+ std::mt19937 gen (rd ());
104
+ std::uniform_int_distribution<> dis (1000 , 9999 );
105
+
106
+ std::stringstream ss;
107
+ ss << prefix << value.count () << " _" << dis (gen) << extension;
108
+ return ss.str ();
109
+ }
110
+
111
+ inline void
112
+ processLocalImage (const std::string &localPath,
113
+ std::function<void (const std::string &)> callback) {
114
+ try {
115
+ std::string base64Image = imageToBase64 (localPath);
116
+ callback (base64Image); // Invoke the callback with the Base64 string
117
+ } catch (const std::exception &e) {
118
+ std::cerr << " Error during processing: " << e.what () << std::endl;
119
+ }
120
+ }
121
+
35
122
inline std::vector<std::string> listFilesInDir (const std::string &path) {
36
123
std::vector<std::string> files;
37
124
0 commit comments