Mercurial > hg > orthanc-object-storage
annotate Common/StoragePlugin.cpp @ 56:b922ae86bbe1
full static linking against AWS SDK
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Jun 2021 15:00:36 +0200 |
parents | 4c71c5e2edce |
children | ba1be668e475 |
rev | line source |
---|---|
1 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
37
f55b2afdf53d
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
35
diff
changeset
|
3 * Copyright (C) 2020-2021 Osimis S.A., Belgium |
1 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
19 |
1 | 20 #if GOOGLE_STORAGE_PLUGIN==1 |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
21 # include "../Google/GoogleStoragePlugin.h" |
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
22 # define StoragePluginFactory GoogleStoragePluginFactory |
1 | 23 #elif AZURE_STORAGE_PLUGIN==1 |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
24 # include "../Azure/AzureBlobStoragePlugin.h" |
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
25 # define StoragePluginFactory AzureBlobStoragePluginFactory |
1 | 26 #elif AWS_STORAGE_PLUGIN==1 |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
27 # include "../Aws/AwsS3StoragePlugin.h" |
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
28 # define StoragePluginFactory AwsS3StoragePluginFactory |
1 | 29 #else |
56
b922ae86bbe1
full static linking against AWS SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
43
diff
changeset
|
30 # pragma message(error "define a plugin") |
1 | 31 #endif |
32 | |
33 #include <string.h> | |
34 #include <stdio.h> | |
35 #include <string> | |
36 | |
37 #include <iostream> | |
15 | 38 #include <boost/filesystem.hpp> |
39 #include <boost/filesystem/fstream.hpp> | |
40 | |
1 | 41 #include "../Common/EncryptionHelpers.h" |
42 #include "../Common/EncryptionConfigurator.h" | |
35
8a7a5defd5d0
upgrade orthanc framework to 1.8.2 in AWS S3 plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
33
diff
changeset
|
43 |
8a7a5defd5d0
upgrade orthanc framework to 1.8.2 in AWS S3 plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
33
diff
changeset
|
44 #include <Logging.h> |
15 | 45 #include <SystemToolbox.h> |
1 | 46 |
47 static std::unique_ptr<IStoragePlugin> plugin; | |
48 | |
49 static std::unique_ptr<EncryptionHelpers> crypto; | |
50 static bool cryptoEnabled = false; | |
15 | 51 static std::string fileSystemRootPath; |
52 static bool migrationFromFileSystemEnabled = false; | |
20 | 53 static std::string objectsRootPath; |
1 | 54 |
55 | |
56 static OrthancPluginErrorCode StorageCreate(const char* uuid, | |
57 const void* content, | |
58 int64_t size, | |
59 OrthancPluginContentType type) | |
60 { | |
61 try | |
62 { | |
63 std::unique_ptr<IStoragePlugin::IWriter> writer(plugin->GetWriterForObject(uuid, type, cryptoEnabled)); | |
64 | |
65 if (cryptoEnabled) | |
66 { | |
67 std::string encryptedFile; | |
68 | |
69 try | |
70 { | |
71 crypto->Encrypt(encryptedFile, (const char*)content, size); | |
72 } | |
73 catch (EncryptionException& ex) | |
74 { | |
75 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while encrypting object " + std::string(uuid) + ": " + ex.what()); | |
76 return OrthancPluginErrorCode_StorageAreaPlugin; | |
77 } | |
78 | |
79 writer->Write(encryptedFile.data(), encryptedFile.size()); | |
80 } | |
81 else | |
82 { | |
83 writer->Write(reinterpret_cast<const char*>(content), size); | |
84 } | |
85 } | |
86 catch (StoragePluginException& ex) | |
87 { | |
88 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while creating object " + std::string(uuid) + ": " + ex.what()); | |
89 return OrthancPluginErrorCode_StorageAreaPlugin; | |
90 } | |
91 | |
92 return OrthancPluginErrorCode_Success; | |
93 } | |
94 | |
95 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
96 static OrthancPluginErrorCode StorageReadRange(OrthancPluginMemoryBuffer64* target, // Memory buffer where to store the content of the range. The memory buffer is allocated and freed by Orthanc. The length of the range of interest corresponds to the size of this buffer. |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
97 const char* uuid, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
98 OrthancPluginContentType type, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
99 uint64_t rangeStart) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
100 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
101 assert(!cryptoEnabled); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
102 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
103 try |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
104 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
105 std::unique_ptr<IStoragePlugin::IReader> reader(plugin->GetReaderForObject(uuid, type, cryptoEnabled)); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
106 reader->ReadRange(reinterpret_cast<char*>(target->data), target->size, rangeStart); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
107 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
108 catch (StoragePluginException& ex) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
109 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
110 if (migrationFromFileSystemEnabled) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
111 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
112 try |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
113 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
114 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ": " + ex.what() + ", will now try to read it from legacy orthanc storage"); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
115 std::string path = BaseStoragePlugin::GetOrthancFileSystemPath(uuid, fileSystemRootPath); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
116 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
117 std::string stringBuffer; |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
118 Orthanc::SystemToolbox::ReadFileRange(stringBuffer, path, rangeStart, rangeStart + target->size, true); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
119 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
120 memcpy(target->data, stringBuffer.data(), stringBuffer.size()); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
121 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
122 return OrthancPluginErrorCode_Success; |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
123 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
124 catch(Orthanc::OrthancException& e) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
125 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
126 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ": " + std::string(e.What())); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
127 return OrthancPluginErrorCode_StorageAreaPlugin; |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
128 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
129 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
130 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
131 return OrthancPluginErrorCode_Success; |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
132 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
133 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
134 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
135 static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64* target, // Memory buffer where to store the content of the file. It must be allocated by the plugin using OrthancPluginCreateMemoryBuffer64(). The core of Orthanc will free it. |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
136 const char* uuid, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
137 OrthancPluginContentType type) |
1 | 138 { |
139 try | |
140 { | |
141 std::unique_ptr<IStoragePlugin::IReader> reader(plugin->GetReaderForObject(uuid, type, cryptoEnabled)); | |
142 | |
143 size_t fileSize = reader->GetSize(); | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
144 size_t size; |
1 | 145 |
146 if (cryptoEnabled) | |
147 { | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
148 size = fileSize - crypto->OVERHEAD_SIZE; |
1 | 149 } |
150 else | |
151 { | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
152 size = fileSize; |
1 | 153 } |
154 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
155 if (size <= 0) |
1 | 156 { |
157 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ", size of file is too small: " + boost::lexical_cast<std::string>(fileSize) + " bytes"); | |
158 return OrthancPluginErrorCode_StorageAreaPlugin; | |
159 } | |
160 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
161 if (OrthancPluginCreateMemoryBuffer64(OrthancPlugins::GetGlobalContext(), target, size) != OrthancPluginErrorCode_Success) |
1 | 162 { |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
163 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ", cannot allocate memory of size " + boost::lexical_cast<std::string>(size) + " bytes"); |
1 | 164 return OrthancPluginErrorCode_StorageAreaPlugin; |
165 } | |
166 | |
167 if (cryptoEnabled) | |
168 { | |
169 std::vector<char> encrypted(fileSize); | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
170 reader->ReadWhole(encrypted.data(), fileSize); |
1 | 171 |
172 try | |
173 { | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
174 crypto->Decrypt(reinterpret_cast<char*>(target->data), encrypted.data(), fileSize); |
1 | 175 } |
176 catch (EncryptionException& ex) | |
177 { | |
178 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while decrypting object " + std::string(uuid) + ": " + ex.what()); | |
179 return OrthancPluginErrorCode_StorageAreaPlugin; | |
180 } | |
181 } | |
182 else | |
183 { | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
184 reader->ReadWhole(reinterpret_cast<char*>(target->data), fileSize); |
1 | 185 } |
186 } | |
187 catch (StoragePluginException& ex) | |
188 { | |
15 | 189 if (migrationFromFileSystemEnabled) |
190 { | |
191 try | |
192 { | |
193 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ": " + ex.what() + ", will now try to read it from legacy orthanc storage"); | |
194 std::string path = BaseStoragePlugin::GetOrthancFileSystemPath(uuid, fileSystemRootPath); | |
195 | |
196 std::string stringBuffer; | |
197 Orthanc::SystemToolbox::ReadFile(stringBuffer, path); | |
198 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
199 if (OrthancPluginCreateMemoryBuffer64(OrthancPlugins::GetGlobalContext(), target, stringBuffer.size()) != OrthancPluginErrorCode_Success) |
15 | 200 { |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
201 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ", cannot allocate memory of size " + boost::lexical_cast<std::string>(stringBuffer.size()) + " bytes"); |
15 | 202 return OrthancPluginErrorCode_StorageAreaPlugin; |
203 } | |
204 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
205 memcpy(target->data, stringBuffer.data(), stringBuffer.size()); |
15 | 206 |
207 return OrthancPluginErrorCode_Success; | |
208 } | |
209 catch(Orthanc::OrthancException& e) | |
210 { | |
211 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ": " + std::string(e.What())); | |
212 return OrthancPluginErrorCode_StorageAreaPlugin; | |
213 } | |
214 } | |
215 else | |
216 { | |
217 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ": " + ex.what()); | |
218 return OrthancPluginErrorCode_StorageAreaPlugin; | |
219 } | |
1 | 220 } |
221 | |
222 return OrthancPluginErrorCode_Success; | |
223 } | |
224 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
225 static OrthancPluginErrorCode StorageReadWholeLegacy(void** content, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
226 int64_t* size, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
227 const char* uuid, |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
228 OrthancPluginContentType type) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
229 { |
43
4c71c5e2edce
fix memory handling in legacy read
Alain Mazy <am@osimis.io>
parents:
39
diff
changeset
|
230 OrthancPluginMemoryBuffer64 buffer; |
4c71c5e2edce
fix memory handling in legacy read
Alain Mazy <am@osimis.io>
parents:
39
diff
changeset
|
231 OrthancPluginErrorCode result = StorageReadWhole(&buffer, uuid, type); // will allocate OrthancPluginMemoryBuffer64 |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
232 |
43
4c71c5e2edce
fix memory handling in legacy read
Alain Mazy <am@osimis.io>
parents:
39
diff
changeset
|
233 if (result == OrthancPluginErrorCode_Success) |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
234 { |
43
4c71c5e2edce
fix memory handling in legacy read
Alain Mazy <am@osimis.io>
parents:
39
diff
changeset
|
235 *size = buffer.size; |
4c71c5e2edce
fix memory handling in legacy read
Alain Mazy <am@osimis.io>
parents:
39
diff
changeset
|
236 *content = buffer.data; // orthanc will free the buffer (we don't have to delete it ourselves) |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
237 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
238 |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
239 return result; |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
240 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
241 |
1 | 242 |
243 static OrthancPluginErrorCode StorageRemove(const char* uuid, | |
244 OrthancPluginContentType type) | |
245 { | |
246 try | |
247 { | |
248 plugin->DeleteObject(uuid, type, cryptoEnabled); | |
249 } | |
250 catch (StoragePluginException& ex) | |
251 { | |
15 | 252 if (migrationFromFileSystemEnabled) |
253 { | |
254 try | |
255 { | |
256 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while deleting object " + std::string(uuid) + ": " + ex.what() + ", will now try to delete it from legacy orthanc storage"); | |
257 namespace fs = boost::filesystem; | |
258 | |
259 fs::path path = BaseStoragePlugin::GetOrthancFileSystemPath(uuid, fileSystemRootPath); | |
260 | |
261 try | |
262 { | |
263 fs::remove(path); | |
264 } | |
265 catch (...) | |
266 { | |
267 // Ignore the error | |
268 } | |
269 | |
270 // Remove the two parent directories, ignoring the error code if | |
271 // these directories are not empty | |
272 | |
273 try | |
274 { | |
275 boost::system::error_code err; | |
276 fs::remove(path.parent_path(), err); | |
277 fs::remove(path.parent_path().parent_path(), err); | |
278 } | |
279 catch (...) | |
280 { | |
281 // Ignore the error | |
282 } | |
283 | |
284 return OrthancPluginErrorCode_Success; | |
285 } | |
286 catch(Orthanc::OrthancException& e) | |
287 { | |
288 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while deleting object " + std::string(uuid) + ": " + std::string(e.What())); | |
289 return OrthancPluginErrorCode_StorageAreaPlugin; | |
290 } | |
291 } | |
292 else | |
293 { | |
294 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while deleting object " + std::string(uuid) + ": " + ex.what()); | |
295 return OrthancPluginErrorCode_StorageAreaPlugin; | |
296 } | |
1 | 297 } |
298 | |
299 return OrthancPluginErrorCode_Success; | |
300 } | |
301 | |
302 | |
303 extern "C" | |
304 { | |
305 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
306 { | |
307 OrthancPlugins::SetGlobalContext(context); | |
308 | |
15 | 309 Orthanc::Logging::InitializePluginContext(context); |
310 | |
1 | 311 OrthancPlugins::OrthancConfiguration orthancConfig; |
312 | |
313 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + " plugin is initializing"); | |
314 | |
315 /* Check the version of the Orthanc core */ | |
316 if (OrthancPluginCheckVersion(context) == 0) | |
317 { | |
318 char info[1024]; | |
319 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | |
320 context->orthancVersion, | |
321 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
322 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
323 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
324 OrthancPlugins::LogError(info); | |
325 return -1; | |
326 } | |
327 | |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
328 try |
8 | 329 { |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
330 plugin.reset(StoragePluginFactory::CreateStoragePlugin(orthancConfig)); |
1 | 331 |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
332 if (plugin.get() == nullptr) |
15 | 333 { |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
334 return -1; |
15 | 335 } |
1 | 336 |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
337 const char* pluginSectionName = plugin->GetConfigurationSectionName(); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
338 static const char* const ENCRYPTION_SECTION = "StorageEncryption"; |
20 | 339 |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
340 if (orthancConfig.IsSection(pluginSectionName)) |
15 | 341 { |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
342 OrthancPlugins::OrthancConfiguration pluginSection; |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
343 orthancConfig.GetSection(pluginSection, pluginSectionName); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
344 |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
345 migrationFromFileSystemEnabled = pluginSection.GetBooleanValue("MigrationFromFileSystemEnabled", false); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
346 |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
347 if (migrationFromFileSystemEnabled) |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
348 { |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
349 fileSystemRootPath = orthancConfig.GetStringValue("StorageDirectory", "OrthancStorageNotDefined"); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
350 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": migration from file system enabled, source: " + fileSystemRootPath); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
351 } |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
352 |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
353 objectsRootPath = pluginSection.GetStringValue("RootPath", std::string()); |
33 | 354 |
355 if (objectsRootPath.size() >= 1 && objectsRootPath[0] == '/') | |
356 { | |
357 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": The RootPath shall not start with a '/': " + objectsRootPath); | |
358 return -1; | |
359 } | |
15 | 360 |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
361 plugin->SetRootPath(objectsRootPath); |
15 | 362 |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
363 if (pluginSection.IsSection(ENCRYPTION_SECTION)) |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
364 { |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
365 OrthancPlugins::OrthancConfiguration cryptoSection; |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
366 pluginSection.GetSection(cryptoSection, ENCRYPTION_SECTION); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
367 |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
368 crypto.reset(EncryptionConfigurator::CreateEncryptionHelpers(cryptoSection)); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
369 cryptoEnabled = crypto.get() != nullptr; |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
370 } |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
371 |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
372 if (cryptoEnabled) |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
373 { |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
374 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is enabled"); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
375 } |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
376 else |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
377 { |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
378 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is disabled"); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
379 } |
15 | 380 } |
381 | |
39
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
382 if (cryptoEnabled) |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
383 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
384 // with encrypted file, we do not want to support ReadRange. Therefore, we register the old interface |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
385 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageReadWholeLegacy, StorageRemove); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
386 } |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
387 else |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
388 { |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
389 OrthancPluginRegisterStorageArea2(context, StorageCreate, StorageReadWhole, StorageReadRange, StorageRemove); |
50d0be413c42
implemented ReadRange (only in Azure plugin right now)
Alain Mazy <am@osimis.io>
parents:
37
diff
changeset
|
390 } |
1 | 391 } |
27
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
392 catch (Orthanc::OrthancException& e) |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
393 { |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
394 LOG(ERROR) << "Exception while creating the object storage plugin: " << e.What(); |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
395 return -1; |
e1f52b851827
Added "VirtualAddressing" configuration option in the AWS S3 plugin (for compatibility with minio)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
396 } |
1 | 397 |
398 return 0; | |
399 } | |
400 | |
401 | |
402 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
403 { | |
404 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + " plugin is finalizing"); | |
405 } | |
406 | |
407 | |
408 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
409 { | |
410 return StoragePluginFactory::GetStoragePluginName(); | |
411 } | |
412 | |
413 | |
414 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
415 { | |
416 return PLUGIN_VERSION; | |
417 } | |
418 } | |
419 |