/* This file is part of libhttpserver Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include class file_upload_resource : public httpserver::http_resource { public: std::shared_ptr render_GET(const httpserver::http_request&) { std::string get_response = "\n"; get_response += " \n"; get_response += "

File Upload with Cleanup Callback Demo

\n"; get_response += "

Uploaded files will be moved to the permanent directory.

\n"; get_response += "
\n"; get_response += " \n"; get_response += "

\n"; get_response += " \n"; get_response += "
\n"; get_response += " \n"; get_response += "\n"; return std::shared_ptr(new httpserver::string_response(get_response, 200, "text/html")); } std::shared_ptr render_POST(const httpserver::http_request& req) { std::string post_response = "\n"; post_response += "\n"; post_response += "

Upload Complete

\n"; post_response += "

Files have been moved to permanent storage:

\n"; post_response += "
    \n"; for (auto &file_key : req.get_files()) { for (auto &files : file_key.second) { post_response += "
  • " + files.first + " (" + std::to_string(files.second.get_file_size()) + " bytes)
  • \n"; } } post_response += "
\n"; post_response += " Upload more\n"; post_response += "\n"; return std::shared_ptr(new httpserver::string_response(post_response, 201, "text/html")); } }; int main(int argc, char** argv) { if (3 != argc) { std::cout << "Usage: file_upload_with_callback " << std::endl; std::cout << std::endl; std::cout << " temp_dir: directory for temporary upload storage" << std::endl; std::cout << " permanent_dir: directory where files will be moved after upload" << std::endl; return -1; } std::string temp_dir = argv[1]; std::string permanent_dir = argv[2]; std::cout << "Starting file upload server on port 8080..." << std::endl; std::cout << " Temporary directory: " << temp_dir << std::endl; std::cout << " Permanent directory: " << permanent_dir << std::endl; std::cout << std::endl; std::cout << "Open http://localhost:8080 in your browser to upload files." << std::endl; httpserver::webserver ws = httpserver::create_webserver(8080) .file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY) .file_upload_dir(temp_dir) .generate_random_filename_on_upload() .file_cleanup_callback([&permanent_dir](const std::string& key, const std::string& filename, const httpserver::http::file_info& info) { (void)key; // Unused in this example // Move the uploaded file to permanent storage std::string dest = permanent_dir + "/" + filename; int result = std::rename(info.get_file_system_file_name().c_str(), dest.c_str()); if (result == 0) { std::cout << "Moved: " << filename << " -> " << dest << std::endl; return false; // Don't delete - we moved it } else { std::cerr << "Failed to move " << filename << ", will be deleted" << std::endl; return true; // Delete the temp file on failure } }); file_upload_resource fur; ws.register_resource("/", &fur); ws.start(true); return 0; }