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 
30 #include "Poco/Path.h"
31 #include "Poco/File.h"
32 #include "Poco/URI.h"
33 #include "Poco/DateTime.h"
34 #include "Poco/Random.h"
35 #include "Poco/StreamCopier.h"
36 #include "Poco/Net/HTMLForm.h"
37 #include "Poco/Net/PartHandler.h"
38 #include "Poco/Net/MessageHeader.h"
39 #include "Poco/CountingStream.h"
40 #include <Poco/JSON/JSON.h>
41 #include <Poco/JSON/JSONException.h>
42 #include <Poco/JSON/Array.h>
43 #include <Poco/JSON/Object.h>
44 
45 #include "files/file_properties.h"
46 #include "files/file.h"
47 #include "tools/settings_manager.h"
48 
49 
50 namespace NAF
51 {
52  namespace Files
53  {
54  enum class OperationType;
55  enum class FileType;
56  class FileManager;
57  }
58 }
59 
60 using namespace Poco;
61 using namespace Poco::Net;
62 
63 
64 enum class NAF::Files::OperationType
65 {
66  kDownload
67  ,kUpload
68  ,kDelete
69 };
70 
71 
72 class NAF::Files::FileManager: public Net::PartHandler
73 {
74  public:
75  FileManager();
76  FileManager(OperationType operation_type);
77  ~FileManager();
78 
79  std::map<std::string, Files::FileProperties>& get_supported_files()
80  {
81  auto& var = supported_files_;
82  return var;
83  }
84  OperationType get_operation_type() const{return operation_type_;}
85  std::string get_directory_base() const{return directory_base_;}
86  std::string get_directory_for_uploaded_files() const{return directory_for_uploaded_files_;}
87  std::string get_directory_for_temp_files() const{return directory_for_temp_files_;}
88  JSON::Object::Ptr& get_result()
89  {
90  auto& var = result_;
91  return var;
92  }
93  std::vector<Files::File>& get_files()
94  {
95  auto& var = files_;
96  return var;
97  }
98 
99  void set_operation_type(OperationType operation_type){operation_type_ = operation_type;}
100  void set_directory_base(std::string directory_base) { directory_base_ = directory_base;}
101  void set_directory_for_uploaded_files(std::string directory_for_uploaded_files) { directory_for_uploaded_files_ = directory_for_uploaded_files;}
102  void set_directory_for_temp_files(std::string directory_for_temp_files) { directory_for_temp_files_ = directory_for_temp_files;}
103 
104  void handlePart(const MessageHeader& header, std::istream& stream) override;
105  std::string GenerateName_(std::string name);
106  bool CheckFile_(Files::File& current_file);
107  bool CheckFiles_();
108  bool IsSupported_();
109  bool IsSupported_(Files::File& file);
110  void DownloadFile_(std::ostream& out_response);
111  void UploadFile_();
112  void RemoveFile_();
113  void AddSupportedFile_(std::string extension, Files::FileProperties file_properties);
114  void AddBasicSupportedFiles_();
115  Files::File CreateTempFile_(std::string uri);
116  Files::File CreateTempFileFromAddress_(std::string address);
117  void ProcessContentLength_();
118  bool VerifyMaxFileSize_();
119 
120 
121  protected:
122  void ProcessFiles_(Files::File& file, Files::FileProperties properties);
123  void ProcessFileType_();
124  std::string SplitHeaderValue_(const MessageHeader& header, std::string header_name, std::string parameter);
125  void CheckTargetFilename_(Files::File& file, std::string directory);
126  std::size_t ReplaceText_(std::string& inout, std::string what, std::string with);
127 
128  private:
129  std::map<std::string, Files::FileProperties> supported_files_;
130  OperationType operation_type_;
131  std::string directory_base_;
132  std::string directory_for_uploaded_files_;
133  std::string directory_for_temp_files_;
134  JSON::Object::Ptr result_;
135  std::vector<Files::File> files_;
136 };
137 
138 #endif // NAF_FILES_FILEMANAGER
Definition: file_manager.h:73
Definition: file_properties.h:36
Definition: file.h:38