81{
83
84 const auto upload_filename = upload_data.upload_path.filename();
85 const auto upload_parent_path = upload_data.upload_path.parent_path();
86 wxString test_msg;
87 if (!
test(test_msg)) {
88 error_fn(std::move(test_msg));
89 return false;
90 }
91
92 bool res = false;
93
94 std::string strDest = upload_parent_path.string();
95 if (strDest.front()!='/')
96 {
97 strDest.insert(0,"/");
98 }
99
101 auto urlSetDir =
make_url(
"upload.cgi",
"UPDIR",strDest);
102 auto urlUpload =
make_url(
"upload.cgi");
103
104 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3% / %4%, filename: %5%")
105 % name
106 % upload_data.source_path
107 % urlPrepare
108 % urlUpload
109 % upload_filename.string();
110
111
112 auto httpPrepare =
Http::get(std::move(urlPrepare));
113 httpPrepare.on_error([&](std::string body, std::string
error,
unsigned status) {
114 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Error preparing upload: %2%, HTTP %3%, body: `%4%`") % name %
error % status % body;
116 res = false;
117 })
118 .on_complete([&, this](std::string body, unsigned) {
119 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got prepare result: %2%") % name % body;
120 res = boost::icontains(body, "SUCCESS");
121 if (! res) {
122 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Request completed but no SUCCESS message was received.") % name;
124 }
125 })
126 .perform_sync();
127
128 if(! res ) {
129 return res;
130 }
131
132
133 auto httpDir =
Http::get(std::move(urlSetDir));
134 httpDir.on_error([&](std::string body, std::string
error,
unsigned status) {
135 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Error setting upload dir: %2%, HTTP %3%, body: `%4%`") % name %
error % status % body;
137 res = false;
138 })
139 .on_complete([&, this](std::string body, unsigned) {
140 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got dir select result: %2%") % name % body;
141 res = boost::icontains(body, "SUCCESS");
142 if (! res) {
143 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Request completed but no SUCCESS message was received.") % name;
145 }
146 })
147 .perform_sync();
148
149 if(! res ) {
150 return res;
151 }
152
154 http.form_add_file("file", upload_data.source_path.string(), upload_filename.string())
155 .on_complete([&](std::string body, unsigned status) {
156 BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: File uploaded: HTTP %2%: %3%") % name % status % body;
157 res = boost::icontains(body, "SUCCESS");
158 if (! res) {
159 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Request completed but no SUCCESS message was received.") % name;
161 }
162 })
163 .on_error([&](std::string body, std::string
error,
unsigned status) {
164 BOOST_LOG_TRIVIAL(
error) << boost::format(
"%1%: Error uploading file: %2%, HTTP %3%, body: `%4%`") % name %
error % status % body;
166 res = false;
167 })
168 .on_progress([&](Http::Progress progress, bool &cancel) {
169 prorgess_fn(std::move(progress), cancel);
170 if (cancel) {
171
172 BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Upload canceled") % name;
173 res = false;
174 }
175 })
176 .perform_sync();
177
178 return res;
179}
std::string timestamp_str() const
Definition FlashAir.cpp:181
bool test(wxString &curl_msg) const override
Definition FlashAir.cpp:36
static Http post(std::string url)
Definition Http.cpp:612