Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/WebDavStorage.cpp @ 5765:247fc5368693 find-refactoring
un-sharing DatabaseConstraint and ISqlLookupFormatter with orthanc-databases
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Sep 2024 16:14:22 +0200 |
parents | f7adfb22e20e |
children |
rev | line source |
---|---|
4230 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
4230 | 8 * |
9 * This program is free software: you can redistribute it and/or | |
10 * modify it under the terms of the GNU Lesser General Public License | |
11 * as published by the Free Software Foundation, either version 3 of | |
12 * the License, or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Lesser General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Lesser General Public | |
20 * License along with this program. If not, see | |
21 * <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 #include "../PrecompiledHeaders.h" | |
26 #include "WebDavStorage.h" | |
27 | |
4304 | 28 #include "../Logging.h" |
4230 | 29 #include "../OrthancException.h" |
30 #include "../SystemToolbox.h" | |
31 #include "../TemporaryFile.h" | |
32 #include "../Toolbox.h" | |
33 | |
34 namespace Orthanc | |
35 { | |
36 class WebDavStorage::StorageFile : public boost::noncopyable | |
37 { | |
38 private: | |
39 std::unique_ptr<TemporaryFile> file_; | |
40 std::string content_; | |
41 MimeType mime_; | |
42 boost::posix_time::ptime time_; | |
43 | |
44 void Touch() | |
45 { | |
46 time_ = boost::posix_time::second_clock::universal_time(); | |
47 } | |
48 | |
49 public: | |
50 StorageFile() : | |
51 mime_(MimeType_Binary) | |
52 { | |
53 Touch(); | |
54 } | |
55 | |
56 void SetContent(const std::string& content, | |
57 MimeType mime, | |
58 bool isMemory) | |
59 { | |
60 if (isMemory) | |
61 { | |
62 content_ = content; | |
63 file_.reset(); | |
64 } | |
65 else | |
66 { | |
67 content_.clear(); | |
68 file_.reset(new TemporaryFile); | |
69 file_->Write(content); | |
70 } | |
71 | |
72 mime_ = mime; | |
73 Touch(); | |
74 } | |
75 | |
76 MimeType GetMimeType() const | |
77 { | |
78 return mime_; | |
79 } | |
80 | |
81 void GetContent(std::string& target) const | |
82 { | |
83 if (file_.get() == NULL) | |
84 { | |
85 target = content_; | |
86 } | |
87 else | |
88 { | |
89 file_->Read(target); | |
90 } | |
91 } | |
92 | |
93 const boost::posix_time::ptime& GetTime() const | |
94 { | |
95 return time_; | |
96 } | |
97 | |
98 uint64_t GetContentLength() const | |
99 { | |
100 if (file_.get() == NULL) | |
101 { | |
102 return content_.size(); | |
103 } | |
104 else | |
105 { | |
106 return file_->GetFileSize(); | |
107 } | |
108 } | |
109 }; | |
110 | |
111 | |
112 class WebDavStorage::StorageFolder : public boost::noncopyable | |
113 { | |
114 private: | |
115 typedef std::map<std::string, StorageFile*> Files; | |
116 typedef std::map<std::string, StorageFolder*> Subfolders; | |
117 | |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
118 Files files_; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
119 Subfolders subfolders_; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
120 boost::posix_time::ptime time_; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
121 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
122 void Touch() |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
123 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
124 time_ = boost::posix_time::second_clock::universal_time(); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
125 } |
4230 | 126 |
127 void CheckName(const std::string& name) | |
128 { | |
129 if (name.empty() || | |
130 name.find('/') != std::string::npos || | |
131 name.find('\\') != std::string::npos || | |
132 name.find('\0') != std::string::npos) | |
133 { | |
134 throw OrthancException(ErrorCode_ParameterOutOfRange, | |
135 "Bad resource name for WebDAV: " + name); | |
136 } | |
137 } | |
138 | |
139 bool IsExisting(const std::string& name) const | |
140 { | |
141 return (files_.find(name) != files_.end() || | |
142 subfolders_.find(name) != subfolders_.end()); | |
143 } | |
144 | |
145 public: | |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
146 StorageFolder() |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
147 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
148 Touch(); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
149 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
150 |
4230 | 151 ~StorageFolder() |
152 { | |
153 for (Files::iterator it = files_.begin(); it != files_.end(); ++it) | |
154 { | |
155 assert(it->second != NULL); | |
156 delete it->second; | |
157 } | |
158 | |
159 for (Subfolders::iterator it = subfolders_.begin(); it != subfolders_.end(); ++it) | |
160 { | |
161 assert(it->second != NULL); | |
162 delete it->second; | |
163 } | |
164 } | |
165 | |
4239
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
166 size_t GetSize() const |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
167 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
168 return files_.size() + subfolders_.size(); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
169 } |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
170 |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
171 const boost::posix_time::ptime& GetModificationTime() const |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
172 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
173 return time_; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
174 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
175 |
4230 | 176 const StorageFile* LookupFile(const std::string& name) const |
177 { | |
178 Files::const_iterator found = files_.find(name); | |
179 if (found == files_.end()) | |
180 { | |
181 return NULL; | |
182 } | |
183 else | |
184 { | |
185 assert(found->second != NULL); | |
186 return found->second; | |
187 } | |
188 } | |
189 | |
190 bool CreateSubfolder(const std::string& name) | |
191 { | |
192 CheckName(name); | |
193 | |
194 if (IsExisting(name)) | |
195 { | |
196 LOG(ERROR) << "WebDAV folder already existing: " << name; | |
197 return false; | |
198 } | |
199 else | |
200 { | |
201 subfolders_[name] = new StorageFolder; | |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
202 Touch(); |
4230 | 203 return true; |
204 } | |
205 } | |
206 | |
207 bool StoreFile(const std::string& name, | |
208 const std::string& content, | |
209 MimeType mime, | |
210 bool isMemory) | |
211 { | |
212 CheckName(name); | |
213 | |
214 if (subfolders_.find(name) != subfolders_.end()) | |
215 { | |
216 LOG(ERROR) << "WebDAV folder already existing: " << name; | |
217 return false; | |
218 } | |
219 | |
220 Files::iterator found = files_.find(name); | |
221 if (found == files_.end()) | |
222 { | |
223 std::unique_ptr<StorageFile> f(new StorageFile); | |
224 f->SetContent(content, mime, isMemory); | |
225 files_[name] = f.release(); | |
226 } | |
227 else | |
228 { | |
229 assert(found->second != NULL); | |
230 found->second->SetContent(content, mime, isMemory); | |
231 } | |
232 | |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
233 Touch(); |
4230 | 234 return true; |
235 } | |
236 | |
237 StorageFolder* LookupFolder(const std::vector<std::string>& path) | |
238 { | |
239 if (path.empty()) | |
240 { | |
241 return this; | |
242 } | |
243 else | |
244 { | |
245 Subfolders::const_iterator found = subfolders_.find(path[0]); | |
246 if (found == subfolders_.end()) | |
247 { | |
248 return NULL; | |
249 } | |
250 else | |
251 { | |
252 assert(found->second != NULL); | |
253 | |
254 std::vector<std::string> p(path.begin() + 1, path.end()); | |
255 return found->second->LookupFolder(p); | |
256 } | |
257 } | |
258 } | |
259 | |
260 void ListCollection(Collection& collection) const | |
261 { | |
262 for (Files::const_iterator it = files_.begin(); it != files_.end(); ++it) | |
263 { | |
264 assert(it->second != NULL); | |
265 | |
266 std::unique_ptr<File> f(new File(it->first)); | |
267 f->SetContentLength(it->second->GetContentLength()); | |
268 f->SetCreationTime(it->second->GetTime()); | |
269 collection.AddResource(f.release()); | |
270 } | |
271 | |
272 for (Subfolders::const_iterator it = subfolders_.begin(); it != subfolders_.end(); ++it) | |
273 { | |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
274 std::unique_ptr<Folder> f(new Folder(it->first)); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
275 f->SetModificationTime(it->second->GetModificationTime()); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
276 collection.AddResource(f.release()); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
277 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
278 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
279 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
280 bool DeleteItem(const std::vector<std::string>& path) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
281 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
282 if (path.size() == 0) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
283 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
284 throw OrthancException(ErrorCode_InternalError); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
285 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
286 else if (path.size() == 1) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
287 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
288 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
289 Files::iterator f = files_.find(path[0]); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
290 if (f != files_.end()) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
291 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
292 assert(f->second != NULL); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
293 delete f->second; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
294 files_.erase(f); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
295 Touch(); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
296 return true; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
297 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
298 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
299 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
300 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
301 Subfolders::iterator f = subfolders_.find(path[0]); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
302 if (f != subfolders_.end()) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
303 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
304 assert(f->second != NULL); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
305 delete f->second; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
306 subfolders_.erase(f); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
307 Touch(); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
308 return true; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
309 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
310 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
311 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
312 return false; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
313 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
314 else |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
315 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
316 Subfolders::iterator f = subfolders_.find(path[0]); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
317 if (f != subfolders_.end()) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
318 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
319 assert(f->second != NULL); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
320 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
321 std::vector<std::string> p(path.begin() + 1, path.end()); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
322 if (f->second->DeleteItem(p)) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
323 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
324 Touch(); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
325 return true; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
326 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
327 else |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
328 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
329 return false; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
330 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
331 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
332 else |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
333 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
334 return false; |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
335 } |
4230 | 336 } |
337 } | |
4239
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
338 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
339 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
340 void RemoveEmptyFolders() |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
341 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
342 std::list<std::string> emptyFolders; |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
343 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
344 for (Subfolders::const_iterator it = subfolders_.begin(); it != subfolders_.end(); ++it) |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
345 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
346 assert(it->second != NULL); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
347 it->second->RemoveEmptyFolders(); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
348 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
349 if (it->second->GetSize() == 0) |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
350 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
351 assert(it->second != NULL); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
352 delete it->second; |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
353 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
354 emptyFolders.push_back(it->first); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
355 } |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
356 } |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
357 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
358 for (std::list<std::string>::const_iterator it = emptyFolders.begin(); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
359 it != emptyFolders.end(); ++it) |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
360 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
361 assert(subfolders_.find(*it) != subfolders_.end()); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
362 subfolders_.erase(*it); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
363 } |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
364 } |
4230 | 365 }; |
366 | |
367 | |
368 WebDavStorage::StorageFolder* WebDavStorage::LookupParentFolder(const std::vector<std::string>& path) | |
369 { | |
370 if (path.empty()) | |
371 { | |
372 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
373 } | |
374 | |
375 std::vector<std::string> p(path.begin(), path.end() - 1); | |
376 return root_->LookupFolder(p); | |
377 } | |
378 | |
379 | |
380 WebDavStorage::WebDavStorage(bool isMemory) : | |
381 root_(new StorageFolder), | |
382 isMemory_(isMemory) | |
383 { | |
384 } | |
385 | |
386 | |
387 bool WebDavStorage::IsExistingFolder(const std::vector<std::string>& path) | |
388 { | |
389 boost::recursive_mutex::scoped_lock lock(mutex_); | |
390 | |
391 return (root_->LookupFolder(path) != NULL); | |
392 } | |
393 | |
394 | |
395 bool WebDavStorage::ListCollection(Collection& collection, | |
396 const std::vector<std::string>& path) | |
397 { | |
398 boost::recursive_mutex::scoped_lock lock(mutex_); | |
399 | |
400 const StorageFolder* folder = root_->LookupFolder(path); | |
401 if (folder == NULL) | |
402 { | |
403 return false; | |
404 } | |
405 else | |
406 { | |
407 folder->ListCollection(collection); | |
408 return true; | |
409 } | |
410 } | |
411 | |
412 | |
413 bool WebDavStorage::GetFileContent(MimeType& mime, | |
414 std::string& content, | |
415 boost::posix_time::ptime& modificationTime, | |
416 const std::vector<std::string>& path) | |
417 { | |
418 boost::recursive_mutex::scoped_lock lock(mutex_); | |
419 | |
420 const StorageFolder* folder = LookupParentFolder(path); | |
421 if (folder == NULL) | |
422 { | |
423 return false; | |
424 } | |
425 else | |
426 { | |
427 const StorageFile* file = folder->LookupFile(path.back()); | |
428 if (file == NULL) | |
429 { | |
430 return false; | |
431 } | |
432 else | |
433 { | |
434 mime = file->GetMimeType(); | |
435 file->GetContent(content); | |
436 modificationTime = file->GetTime(); | |
437 return true; | |
438 } | |
439 } | |
440 } | |
441 | |
442 | |
443 bool WebDavStorage::StoreFile(const std::string& content, | |
444 const std::vector<std::string>& path) | |
445 { | |
446 boost::recursive_mutex::scoped_lock lock(mutex_); | |
447 | |
448 StorageFolder* folder = LookupParentFolder(path); | |
449 if (folder == NULL) | |
450 { | |
451 LOG(WARNING) << "Inexisting folder in WebDAV: " << Toolbox::FlattenUri(path); | |
452 return false; | |
453 } | |
454 else | |
455 { | |
456 LOG(INFO) << "Storing " << content.size() | |
457 << " bytes in WebDAV bucket: " << Toolbox::FlattenUri(path);; | |
458 | |
459 MimeType mime = SystemToolbox::AutodetectMimeType(path.back()); | |
460 return folder->StoreFile(path.back(), content, mime, isMemory_); | |
461 } | |
462 } | |
463 | |
464 | |
465 bool WebDavStorage::CreateFolder(const std::vector<std::string>& path) | |
466 { | |
467 boost::recursive_mutex::scoped_lock lock(mutex_); | |
468 | |
469 StorageFolder* folder = LookupParentFolder(path); | |
470 if (folder == NULL) | |
471 { | |
472 LOG(WARNING) << "Inexisting folder in WebDAV: " << Toolbox::FlattenUri(path); | |
473 return false; | |
474 } | |
475 else | |
476 { | |
477 LOG(INFO) << "Creating folder in WebDAV bucket: " << Toolbox::FlattenUri(path); | |
478 return folder->CreateSubfolder(path.back()); | |
479 } | |
480 } | |
4232
688435755466
added DELETE in WebDAV, first working virtual filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4230
diff
changeset
|
481 |
688435755466
added DELETE in WebDAV, first working virtual filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4230
diff
changeset
|
482 |
688435755466
added DELETE in WebDAV, first working virtual filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4230
diff
changeset
|
483 bool WebDavStorage::DeleteItem(const std::vector<std::string>& path) |
688435755466
added DELETE in WebDAV, first working virtual filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4230
diff
changeset
|
484 { |
4233
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
485 if (path.empty()) |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
486 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
487 return false; // Cannot delete the root |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
488 } |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
489 else |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
490 { |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
491 boost::recursive_mutex::scoped_lock lock(mutex_); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
492 |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
493 LOG(INFO) << "Deleting from WebDAV bucket: " << Toolbox::FlattenUri(path); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
494 return root_->DeleteItem(path); |
ca2a55a62c81
implementation of DELETE in class WebDavStorage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4232
diff
changeset
|
495 } |
4232
688435755466
added DELETE in WebDAV, first working virtual filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4230
diff
changeset
|
496 } |
4239
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
497 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
498 |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
499 void WebDavStorage::RemoveEmptyFolders() |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
500 { |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
501 boost::recursive_mutex::scoped_lock lock(mutex_); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
502 root_->RemoveEmptyFolders(); |
c8754c4c1862
upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4233
diff
changeset
|
503 } |
4230 | 504 } |