comparison OrthancFramework/Sources/FileStorage/FilesystemStorage.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/FileStorage/FilesystemStorage.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "FilesystemStorage.h"
36
37 // http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system
38 // http://stackoverflow.com/questions/446358/storing-a-large-number-of-images
39
40 #include "../Logging.h"
41 #include "../OrthancException.h"
42 #include "../Toolbox.h"
43 #include "../SystemToolbox.h"
44
45 #include <boost/filesystem/fstream.hpp>
46
47
48 static std::string ToString(const boost::filesystem::path& p)
49 {
50 #if BOOST_HAS_FILESYSTEM_V3 == 1
51 return p.filename().string();
52 #else
53 return p.filename();
54 #endif
55 }
56
57
58 namespace Orthanc
59 {
60 boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const
61 {
62 namespace fs = boost::filesystem;
63
64 if (!Toolbox::IsUuid(uuid))
65 {
66 throw OrthancException(ErrorCode_ParameterOutOfRange);
67 }
68
69 fs::path path = root_;
70
71 path /= std::string(&uuid[0], &uuid[2]);
72 path /= std::string(&uuid[2], &uuid[4]);
73 path /= uuid;
74
75 #if BOOST_HAS_FILESYSTEM_V3 == 1
76 path.make_preferred();
77 #endif
78
79 return path;
80 }
81
82 FilesystemStorage::FilesystemStorage(std::string root)
83 {
84 //root_ = boost::filesystem::absolute(root).string();
85 root_ = root;
86
87 SystemToolbox::MakeDirectory(root);
88 }
89
90
91
92 static const char* GetDescriptionInternal(FileContentType content)
93 {
94 // This function is for logging only (internal use), a more
95 // fully-featured version is available in ServerEnumerations.cpp
96 switch (content)
97 {
98 case FileContentType_Unknown:
99 return "Unknown";
100
101 case FileContentType_Dicom:
102 return "DICOM";
103
104 case FileContentType_DicomAsJson:
105 return "JSON summary of DICOM";
106
107 default:
108 return "User-defined";
109 }
110 }
111
112
113 void FilesystemStorage::Create(const std::string& uuid,
114 const void* content,
115 size_t size,
116 FileContentType type)
117 {
118 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
119 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
120
121 boost::filesystem::path path;
122
123 path = GetPath(uuid);
124
125 if (boost::filesystem::exists(path))
126 {
127 // Extremely unlikely case: This Uuid has already been created
128 // in the past.
129 throw OrthancException(ErrorCode_InternalError);
130 }
131
132 if (boost::filesystem::exists(path.parent_path()))
133 {
134 if (!boost::filesystem::is_directory(path.parent_path()))
135 {
136 throw OrthancException(ErrorCode_DirectoryOverFile);
137 }
138 }
139 else
140 {
141 if (!boost::filesystem::create_directories(path.parent_path()))
142 {
143 throw OrthancException(ErrorCode_FileStorageCannotWrite);
144 }
145 }
146
147 SystemToolbox::WriteFile(content, size, path.string());
148 }
149
150
151 void FilesystemStorage::Read(std::string& content,
152 const std::string& uuid,
153 FileContentType type)
154 {
155 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
156 << "\" content type";
157
158 content.clear();
159 SystemToolbox::ReadFile(content, GetPath(uuid).string());
160 }
161
162
163 uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const
164 {
165 boost::filesystem::path path = GetPath(uuid);
166 return boost::filesystem::file_size(path);
167 }
168
169
170
171 void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const
172 {
173 namespace fs = boost::filesystem;
174
175 result.clear();
176
177 if (fs::exists(root_) && fs::is_directory(root_))
178 {
179 for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current)
180 {
181 if (SystemToolbox::IsRegularFile(current->path().string()))
182 {
183 try
184 {
185 fs::path d = current->path();
186 std::string uuid = ToString(d);
187 if (Toolbox::IsUuid(uuid))
188 {
189 fs::path p0 = d.parent_path().parent_path().parent_path();
190 std::string p1 = ToString(d.parent_path().parent_path());
191 std::string p2 = ToString(d.parent_path());
192 if (p1.length() == 2 &&
193 p2.length() == 2 &&
194 p1 == uuid.substr(0, 2) &&
195 p2 == uuid.substr(2, 2) &&
196 p0 == root_)
197 {
198 result.insert(uuid);
199 }
200 }
201 }
202 catch (fs::filesystem_error&)
203 {
204 }
205 }
206 }
207 }
208 }
209
210
211 void FilesystemStorage::Clear()
212 {
213 namespace fs = boost::filesystem;
214 typedef std::set<std::string> List;
215
216 List result;
217 ListAllFiles(result);
218
219 for (List::const_iterator it = result.begin(); it != result.end(); ++it)
220 {
221 Remove(*it, FileContentType_Unknown /*ignored in this class*/);
222 }
223 }
224
225
226 void FilesystemStorage::Remove(const std::string& uuid,
227 FileContentType type)
228 {
229 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type);
230
231 namespace fs = boost::filesystem;
232
233 fs::path p = GetPath(uuid);
234
235 try
236 {
237 fs::remove(p);
238 }
239 catch (...)
240 {
241 // Ignore the error
242 }
243
244 // Remove the two parent directories, ignoring the error code if
245 // these directories are not empty
246
247 try
248 {
249 #if BOOST_HAS_FILESYSTEM_V3 == 1
250 boost::system::error_code err;
251 fs::remove(p.parent_path(), err);
252 fs::remove(p.parent_path().parent_path(), err);
253 #else
254 fs::remove(p.parent_path());
255 fs::remove(p.parent_path().parent_path());
256 #endif
257 }
258 catch (...)
259 {
260 // Ignore the error
261 }
262 }
263
264
265 uintmax_t FilesystemStorage::GetCapacity() const
266 {
267 return boost::filesystem::space(root_).capacity;
268 }
269
270 uintmax_t FilesystemStorage::GetAvailableSpace() const
271 {
272 return boost::filesystem::space(root_).available;
273 }
274 }