|
@@ -226,15 +226,75 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
|
|
|
answer["command"] = "put";
|
|
|
|
|
|
- // TODO establish real connection and receive file
|
|
|
- answer["accept"] = false;
|
|
|
+ if (this->putFile.is_open() && root["cancel"].asBool() == true) {
|
|
|
+ // cancel upload
|
|
|
+ this->putFile.close();
|
|
|
+ std::remove(this->putFileName.c_str());
|
|
|
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
- // send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ } else if (this->putFile.is_open()) {
|
|
|
+ // upload chunks
|
|
|
+
|
|
|
+ // decode base64 string
|
|
|
+ const std::string data = base64::decode(root["data"].asString());
|
|
|
+
|
|
|
+ this->putFile << data;
|
|
|
+
|
|
|
+ // close file if put sends remaining = 0
|
|
|
+ if (root["remaining"].asInt() == 0) {
|
|
|
+ this->putFile.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // start upload
|
|
|
+ const std::string filename = root["file"].asString();
|
|
|
+ if (filename.find("/") != std::string::npos) {
|
|
|
+ // slashes in file names are illegal
|
|
|
+ answer["accept"] = false;
|
|
|
+
|
|
|
+ const std::string answerString =
|
|
|
+ Json::writeString(stringBuilder, answer);
|
|
|
+ // send answer
|
|
|
+ sock.async_write_some(buffer(answerString, max_length),
|
|
|
+ boost::bind(&con_handler::handle_write,
|
|
|
+ shared_from_this(),
|
|
|
+ placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+ } else {
|
|
|
+ // build file path
|
|
|
+ this->putFileName = this->fileDirectory;
|
|
|
+ this->putFileName.append(filename);
|
|
|
+
|
|
|
+ // open file and test if it already exists
|
|
|
+ this->putFile.open(this->putFileName,
|
|
|
+ std::ios::app | std::ios::binary);
|
|
|
+ if (this->putFile.tellp() != std::ios::beg) {
|
|
|
+ // file already exists
|
|
|
+ this->putFile.close();
|
|
|
+ answer["accept"] = false;
|
|
|
+
|
|
|
+ const std::string answerString =
|
|
|
+ Json::writeString(stringBuilder, answer);
|
|
|
+ // send answer
|
|
|
+ sock.async_write_some(buffer(answerString, max_length),
|
|
|
+ boost::bind(&con_handler::handle_write,
|
|
|
+ shared_from_this(),
|
|
|
+ placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+ } else {
|
|
|
+ // accept file
|
|
|
+ answer["accept"] = true;
|
|
|
+
|
|
|
+ const std::string answerString =
|
|
|
+ Json::writeString(stringBuilder, answer);
|
|
|
+ // send answer
|
|
|
+ sock.async_write_some(buffer(answerString, max_length),
|
|
|
+ boost::bind(&con_handler::handle_write,
|
|
|
+ shared_from_this(),
|
|
|
+ placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
} else if (root["command"].compare("get") == 0) {
|
|
|
// read next data
|
|
|
sock.async_read_some(buffer(data, max_length),
|