file_manager.h
1 /*
2 * Nebula Atom
3 
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef NAF_FILES_FILEMANAGER
19 #define NAF_FILES_FILEMANAGER
20 
21 
22 #include <string>
23 #include <string_view>
24 #include <map>
25 #include <vector>
26 #include <fstream>
27 #include <memory>
28 #include <iostream>
29 #include <exception>
30 #include <stdexcept>
31 
32 #include "Poco/Path.h"
33 #include "Poco/File.h"
34 #include "Poco/URI.h"
35 #include "Poco/DateTime.h"
36 #include "Poco/Random.h"
37 #include "Poco/StreamCopier.h"
38 #include "Poco/Net/HTMLForm.h"
39 #include "Poco/Net/PartHandler.h"
40 #include "Poco/Net/MessageHeader.h"
41 #include "Poco/CountingStream.h"
42 #include <Poco/JSON/JSON.h>
43 #include <Poco/JSON/JSONException.h>
44 #include <Poco/JSON/Array.h>
45 #include <Poco/JSON/Object.h>
46 
47 #include "files/file_properties.h"
48 #include "files/file.h"
49 #include "tools/settings_manager.h"
50 #include "tools/output_logger.h"
51 
52 
53 namespace NAF
54 {
55  namespace Files
56  {
57  enum class OperationType;
58  enum class FileType;
59  class FileManager;
60  }
61 }
62 
63 using namespace Poco;
64 using namespace Poco::Net;
65 
66 
67 enum class NAF::Files::OperationType
68 {
69  kDownload
70  ,kUpload
71  ,kDelete
72 };
73 
74 
75 class NAF::Files::FileManager: public Net::PartHandler
76 {
77  public:
78  using Ptr = std::shared_ptr<FileManager>;
79 
80  FileManager();
81  FileManager(FileManager& file_manager);
82  FileManager(OperationType operation_type);
83  ~FileManager();
84 
85  std::map<std::string, Files::FileProperties>& get_supported_files()
86  {
87  auto& var = supported_files_;
88  return var;
89  }
90  OperationType get_operation_type() const{return operation_type_;}
91  std::string get_directory_base() const{return directory_base_;}
92  std::string get_directory_for_temp_files() const{return directory_for_temp_files_;}
93  JSON::Object::Ptr& get_result()
94  {
95  auto& var = result_;
96  return var;
97  }
98  std::vector<Files::File>& get_files()
99  {
100  auto& var = files_;
101  return var;
102  }
103 
104  void set_operation_type(OperationType operation_type){operation_type_ = operation_type;}
105  void set_directory_base(std::string directory_base) { directory_base_ = directory_base;}
106  void set_directory_for_temp_files(std::string directory_for_temp_files) { directory_for_temp_files_ = directory_for_temp_files;}
107 
108  void handlePart(const MessageHeader& header, std::istream& stream) override;
109  std::string GenerateName_(std::string name);
110  bool CheckFile_(Files::File& current_file);
111  bool CheckFiles_();
112  bool IsSupported_();
113  bool IsSupported_(Files::File& file);
114  void DownloadFile_(std::ostream& out_response);
115  void UploadFile_();
116  void RemoveFile_();
117  void AddSupportedFile_(std::string extension, Files::FileProperties file_properties);
118  void AddBasicSupportedFiles_();
119  Files::File CreateTempFile_(std::string uri);
120  Files::File CreateTempFileFromAddress_(std::string address);
121  void ProcessContentLength_();
122  bool VerifyMaxFileSize_();
123  bool ChangePathAndFilename_(Files::File& file, std::string directory, bool change_filename = true);
124 
125  protected:
126  void ProcessFiles_(Files::File& file, Files::FileProperties properties);
127  void ProcessFileType_();
128  std::string SplitHeaderValue_(const MessageHeader& header, std::string header_name, std::string parameter);
129  std::size_t ReplaceText_(std::string& inout, std::string what, std::string with);
130 
131  private:
132  std::map<std::string, Files::FileProperties> supported_files_;
133  OperationType operation_type_;
134  std::string directory_base_;
135  std::string directory_for_temp_files_;
136  JSON::Object::Ptr result_;
137  std::vector<Files::File> files_;
138 };
139 
140 #endif // NAF_FILES_FILEMANAGER
Definition: file_manager.h:76
Definition: file_properties.h:36
Definition: file.h:38