-
Notifications
You must be signed in to change notification settings - Fork 191
Add options to store files directly to the file system and to omit duplicating files in memory #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5f22dfd
916610a
d9adfcb
220fdd8
52cb06f
71d1f20
1f37cfa
fac086a
5843f5f
bc5a4fe
8d7ea8e
329c066
07e9f4d
29ba743
2606d61
8ab2972
3880d8d
2e99b1d
fd7bab9
ccc67e8
799c201
f329061
8b5c172
cb802d1
c6151c4
4cb49c8
7f2c353
27fc9a8
1cf683e
88d14aa
b3a39dd
cbca260
f896540
897eab9
7b73c48
4f2cecf
c1878f5
f189014
8e16406
4bcc846
f4fb34f
0e90742
ee91ea9
ac91c63
f13d1fe
91b90e5
4de82c9
acebdfa
6fff5f0
dbba47c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| This file is part of libhttpserver | ||
| Copyright (C) 2011-2019 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 <string.h> | ||
| #include "httpserver/file_info.hpp" | ||
|
|
||
| namespace httpserver { | ||
| namespace http { | ||
|
|
||
| void file_info::set_file_system_file_name(const std::string file_system_file_name) { | ||
| _file_system_file_name = file_system_file_name; | ||
| } | ||
|
|
||
| void file_info::grow_file_size(size_t additional_file_size) { | ||
| _file_size += additional_file_size; | ||
| } | ||
| size_t file_info::get_file_size() const { | ||
| return _file_size; | ||
| } | ||
| const std::string file_info::get_file_system_file_name() const { | ||
| return _file_system_file_name; | ||
| } | ||
|
|
||
|
|
||
| } // namespace http | ||
| } // namespace httpserver |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| This file is part of libhttpserver | ||
| Copyright (C) 2011-2019 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 | ||
| */ | ||
|
|
||
| #if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION) | ||
| #error "Only <httpserver.hpp> or <httpserverpp> can be included directly." | ||
| #endif | ||
|
|
||
| #ifndef SRC_HTTPSERVER_FILE_INFO_HPP_ | ||
| #define SRC_HTTPSERVER_FILE_INFO_HPP_ | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace httpserver { | ||
|
|
||
| namespace http { | ||
|
|
||
| class file_info { | ||
| public: | ||
| void set_file_system_file_name(const std::string file_system_file_name); | ||
| void grow_file_size(size_t additional_file_size); | ||
| size_t get_file_size() const; | ||
| const std::string get_file_system_file_name() const; | ||
|
|
||
| private: | ||
| size_t _file_size; | ||
| std::string _file_system_file_name; | ||
| }; | ||
|
|
||
| } // namespace http | ||
| } // namespace httpserver | ||
| #endif // SRC_HTTPSERVER_FILE_INFO_HPP_ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| #include <vector> | ||
|
|
||
| #include "httpserver/http_utils.hpp" | ||
| #include "httpserver/file_info.hpp" | ||
|
|
||
| struct MHD_Connection; | ||
|
|
||
|
|
@@ -141,13 +142,13 @@ class http_request { | |
| * @param upload_file_name the file name the user uploaded (this is the identifier for the map entry) | ||
| * @result a file info struct file_info_s | ||
|
ctomahogh marked this conversation as resolved.
|
||
| **/ | ||
| const file_info_s& get_or_create_file_info(const std::string &key, const std::string& upload_file_name); | ||
| http::file_info& get_or_create_file_info(const std::string &key, const std::string& upload_file_name); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we make the output const? nit: also move the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I see it, the output cannot be const, as in the using function ( |
||
|
|
||
| /** | ||
| * Method used to get all files passed with the request. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏼 |
||
| * @result result a map<string, file_info_s> > that will be filled with all files | ||
| **/ | ||
| const std::map<std::string, std::map<std::string, file_info_s>> get_files() const { | ||
| const std::map<std::string, std::map<std::string, http::file_info>> get_files() const { | ||
| return files; | ||
| } | ||
|
|
||
|
|
@@ -256,7 +257,7 @@ class http_request { | |
| std::string path; | ||
| std::string method; | ||
| std::map<std::string, std::string, http::arg_comparator> args; | ||
| std::map<std::string, std::map<std::string, file_info_s>> files; | ||
| std::map<std::string, std::map<std::string, http::file_info>> files; | ||
| std::string content = ""; | ||
| size_t content_size_limit = static_cast<size_t>(-1); | ||
| std::string version; | ||
|
|
@@ -318,24 +319,6 @@ class http_request { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method used to set the file system file name into the map by key. | ||
| * @param upload_file_name The file name the user uploaded (identifying the map entry) | ||
| * @param file_system_file_name The path to the file in the file system (the name may not be the original name depending on configuration of the webserver) | ||
| **/ | ||
| void set_file_system_file_name(const std::string& key, const std::string& upload_file_name, const std::string& file_system_file_name) { | ||
| files[key][upload_file_name].file_system_file_name = file_system_file_name; | ||
| } | ||
|
|
||
| /** | ||
| * Method used grow the filesize of an entry in the map by key. | ||
| * @param upload_file_name The file name the user uploaded (identifying the map entry) | ||
| * @param additional_file_size The additional size as a number to be added to the entry. | ||
| **/ | ||
| void grow_file_size(const std::string& key, const std::string& upload_file_name, size_t additional_file_size) { | ||
| files[key][upload_file_name].file_size += additional_file_size; | ||
| } | ||
|
|
||
| /** | ||
| * Method used to set the path requested. | ||
| * @param path The path searched by the request. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -471,17 +471,17 @@ MHD_Result webserver::post_iterator(void *cls, enum MHD_ValueKind kind, | |
|
|
||
| if (filename && mr->ws->file_upload_target != FILE_UPLOAD_MEMORY_ONLY) { | ||
| // either get the existing file info struct or create a new one in the file map | ||
| const file_info_s &file_info = mr->dhr->get_or_create_file_info(key, filename); | ||
| http::file_info &file = mr->dhr->get_or_create_file_info(key, filename); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: move |
||
| // if the file_system_file_name is not filled yet, this is a new entry and the name has to be set | ||
| // (either random or copy of the original filename) | ||
| if (file_info.file_system_file_name.empty()) { | ||
| if (file.get_file_system_file_name().empty()) { | ||
| if (mr->ws->generate_random_filename_on_upload) { | ||
| mr->dhr->set_file_system_file_name(key, filename, http_utils::generate_random_upload_filename(mr->ws->post_upload_dir)); | ||
| file.set_file_system_file_name(http_utils::generate_random_upload_filename(mr->ws->post_upload_dir)); | ||
| } else { | ||
| mr->dhr->set_file_system_file_name(key, filename, mr->ws->post_upload_dir + "/" + std::string(filename)); | ||
| file.set_file_system_file_name(mr->ws->post_upload_dir + "/" + std::string(filename)); | ||
| } | ||
| // to not append to an already existing file, delete an already existing file | ||
| unlink(file_info.file_system_file_name.c_str()); | ||
| unlink(file.get_file_system_file_name().c_str()); | ||
| } | ||
|
|
||
| // if multiple files are uploaded, a different filename indicates the start of a new file, so close the previous one | ||
|
|
@@ -495,15 +495,15 @@ MHD_Result webserver::post_iterator(void *cls, enum MHD_ValueKind kind, | |
| mr->upload_key = key; | ||
| mr->upload_filename = filename; | ||
| mr->upload_ostrm = new std::ofstream(); | ||
| mr->upload_ostrm->open(file_info.file_system_file_name, std::ios::binary | std::ios::app); | ||
| mr->upload_ostrm->open(file.get_file_system_file_name(), std::ios::binary | std::ios::app); | ||
| } | ||
|
|
||
| if (size > 0) { | ||
| mr->upload_ostrm->write(data, size); | ||
| } | ||
|
|
||
| // update the file size in the map | ||
| mr->dhr->grow_file_size(key, filename, size); | ||
| file.grow_file_size(size); | ||
| } | ||
| return MHD_YES; | ||
| } catch(const std::exception& e) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will fix itself when we use the custom exception |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.