|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <cstdio> |
| 22 | +#include <iostream> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +#include <httpserver.hpp> |
| 27 | + |
| 28 | +class file_upload_resource : public httpserver::http_resource { |
| 29 | + public: |
| 30 | + std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) { |
| 31 | + std::string get_response = "<html>\n"; |
| 32 | + get_response += " <body>\n"; |
| 33 | + get_response += " <h1>File Upload with Cleanup Callback Demo</h1>\n"; |
| 34 | + get_response += " <p>Uploaded files will be moved to the permanent directory.</p>\n"; |
| 35 | + get_response += " <form method=\"POST\" enctype=\"multipart/form-data\">\n"; |
| 36 | + get_response += " <input type=\"file\" name=\"file\" multiple>\n"; |
| 37 | + get_response += " <br><br>\n"; |
| 38 | + get_response += " <input type=\"submit\" value=\"Upload\">\n"; |
| 39 | + get_response += " </form>\n"; |
| 40 | + get_response += " </body>\n"; |
| 41 | + get_response += "</html>\n"; |
| 42 | + |
| 43 | + return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(get_response, 200, "text/html")); |
| 44 | + } |
| 45 | + |
| 46 | + std::shared_ptr<httpserver::http_response> render_POST(const httpserver::http_request& req) { |
| 47 | + std::string post_response = "<html>\n"; |
| 48 | + post_response += "<body>\n"; |
| 49 | + post_response += " <h1>Upload Complete</h1>\n"; |
| 50 | + post_response += " <p>Files have been moved to permanent storage:</p>\n"; |
| 51 | + post_response += " <ul>\n"; |
| 52 | + |
| 53 | + for (auto &file_key : req.get_files()) { |
| 54 | + for (auto &files : file_key.second) { |
| 55 | + post_response += " <li>" + files.first + " (" + |
| 56 | + std::to_string(files.second.get_file_size()) + " bytes)</li>\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + post_response += " </ul>\n"; |
| 61 | + post_response += " <a href=\"/\">Upload more</a>\n"; |
| 62 | + post_response += "</body>\n</html>"; |
| 63 | + return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(post_response, 201, "text/html")); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +int main(int argc, char** argv) { |
| 68 | + if (3 != argc) { |
| 69 | + std::cout << "Usage: file_upload_with_callback <temp_dir> <permanent_dir>" << std::endl; |
| 70 | + std::cout << std::endl; |
| 71 | + std::cout << " temp_dir: directory for temporary upload storage" << std::endl; |
| 72 | + std::cout << " permanent_dir: directory where files will be moved after upload" << std::endl; |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + std::string temp_dir = argv[1]; |
| 77 | + std::string permanent_dir = argv[2]; |
| 78 | + |
| 79 | + std::cout << "Starting file upload server on port 8080..." << std::endl; |
| 80 | + std::cout << " Temporary directory: " << temp_dir << std::endl; |
| 81 | + std::cout << " Permanent directory: " << permanent_dir << std::endl; |
| 82 | + std::cout << std::endl; |
| 83 | + std::cout << "Open http://localhost:8080 in your browser to upload files." << std::endl; |
| 84 | + |
| 85 | + httpserver::webserver ws = httpserver::create_webserver(8080) |
| 86 | + .file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY) |
| 87 | + .file_upload_dir(temp_dir) |
| 88 | + .generate_random_filename_on_upload() |
| 89 | + .file_cleanup_callback([&permanent_dir](const std::string& key, |
| 90 | + const std::string& filename, |
| 91 | + const httpserver::http::file_info& info) { |
| 92 | + (void)key; // Unused in this example |
| 93 | + // Move the uploaded file to permanent storage |
| 94 | + std::string dest = permanent_dir + "/" + filename; |
| 95 | + int result = std::rename(info.get_file_system_file_name().c_str(), dest.c_str()); |
| 96 | + |
| 97 | + if (result == 0) { |
| 98 | + std::cout << "Moved: " << filename << " -> " << dest << std::endl; |
| 99 | + return false; // Don't delete - we moved it |
| 100 | + } else { |
| 101 | + std::cerr << "Failed to move " << filename << ", will be deleted" << std::endl; |
| 102 | + return true; // Delete the temp file on failure |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + file_upload_resource fur; |
| 107 | + ws.register_resource("/", &fur); |
| 108 | + ws.start(true); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
0 commit comments