Mercurial > hg > orthanc
annotate OrthancServer/Sources/OrthancWebDav.cpp @ 4585:f0bdd99f3d81 db-changes
created a ITransactionContextFactory around ServerIndex::Listener
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Mar 2021 16:40:38 +0100 |
parents | efd90f778cd2 |
children | 94147ce2f097 |
rev | line source |
---|---|
4240 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4386
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
4240 | 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 "OrthancWebDav.h" | |
35 | |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
36 #include "../../OrthancFramework/Sources/Compression/ZipReader.h" |
4240 | 37 #include "../../OrthancFramework/Sources/DicomFormat/DicomArray.h" |
38 #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" | |
39 #include "../../OrthancFramework/Sources/HttpServer/WebDavStorage.h" | |
4304 | 40 #include "../../OrthancFramework/Sources/Logging.h" |
4240 | 41 #include "Search/DatabaseLookup.h" |
42 #include "ServerContext.h" | |
43 | |
44 #include <boost/regex.hpp> | |
45 #include <boost/algorithm/string/predicate.hpp> | |
46 | |
47 | |
48 static const char* const BY_PATIENTS = "by-patients"; | |
49 static const char* const BY_STUDIES = "by-studies"; | |
4241 | 50 static const char* const BY_DATES = "by-dates"; |
4240 | 51 static const char* const BY_UIDS = "by-uids"; |
52 static const char* const UPLOADS = "uploads"; | |
53 static const char* const MAIN_DICOM_TAGS = "MainDicomTags"; | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
54 static const char* const STUDY_INFO = "study.json"; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
55 static const char* const SERIES_INFO = "series.json"; |
4240 | 56 |
57 | |
58 namespace Orthanc | |
59 { | |
60 static boost::posix_time::ptime GetNow() | |
61 { | |
62 return boost::posix_time::second_clock::universal_time(); | |
63 } | |
64 | |
65 | |
66 static void LookupTime(boost::posix_time::ptime& target, | |
67 ServerContext& context, | |
68 const std::string& publicId, | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
69 ResourceType level, |
4240 | 70 MetadataType metadata) |
71 { | |
72 std::string value; | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
73 if (context.GetIndex().LookupMetadata(value, publicId, level, metadata)) |
4240 | 74 { |
75 try | |
76 { | |
77 target = boost::posix_time::from_iso_string(value); | |
78 return; | |
79 } | |
80 catch (std::exception& e) | |
81 { | |
82 } | |
83 } | |
84 | |
85 target = GetNow(); | |
86 } | |
87 | |
88 | |
89 class OrthancWebDav::DicomIdentifiersVisitor : public ServerContext::ILookupVisitor | |
90 { | |
91 private: | |
92 ServerContext& context_; | |
93 bool isComplete_; | |
94 Collection& target_; | |
95 ResourceType level_; | |
96 | |
97 public: | |
98 DicomIdentifiersVisitor(ServerContext& context, | |
99 Collection& target, | |
100 ResourceType level) : | |
101 context_(context), | |
102 isComplete_(false), | |
103 target_(target), | |
104 level_(level) | |
105 { | |
106 } | |
107 | |
108 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE | |
109 { | |
110 return false; // (*) | |
111 } | |
112 | |
113 virtual void MarkAsComplete() ORTHANC_OVERRIDE | |
114 { | |
115 isComplete_ = true; // TODO | |
116 } | |
117 | |
118 virtual void Visit(const std::string& publicId, | |
119 const std::string& instanceId /* unused */, | |
120 const DicomMap& mainDicomTags, | |
121 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE | |
122 { | |
123 DicomTag tag(0, 0); | |
124 MetadataType timeMetadata; | |
125 | |
126 switch (level_) | |
127 { | |
128 case ResourceType_Study: | |
129 tag = DICOM_TAG_STUDY_INSTANCE_UID; | |
130 timeMetadata = MetadataType_LastUpdate; | |
131 break; | |
132 | |
133 case ResourceType_Series: | |
134 tag = DICOM_TAG_SERIES_INSTANCE_UID; | |
135 timeMetadata = MetadataType_LastUpdate; | |
136 break; | |
137 | |
138 case ResourceType_Instance: | |
139 tag = DICOM_TAG_SOP_INSTANCE_UID; | |
140 timeMetadata = MetadataType_Instance_ReceptionDate; | |
141 break; | |
142 | |
143 default: | |
144 throw OrthancException(ErrorCode_InternalError); | |
145 } | |
146 | |
147 std::string s; | |
148 if (mainDicomTags.LookupStringValue(s, tag, false) && | |
149 !s.empty()) | |
150 { | |
151 std::unique_ptr<Resource> resource; | |
152 | |
153 if (level_ == ResourceType_Instance) | |
154 { | |
155 FileInfo info; | |
156 if (context_.GetIndex().LookupAttachment(info, publicId, FileContentType_Dicom)) | |
157 { | |
158 std::unique_ptr<File> f(new File(s + ".dcm")); | |
159 f->SetMimeType(MimeType_Dicom); | |
160 f->SetContentLength(info.GetUncompressedSize()); | |
161 resource.reset(f.release()); | |
162 } | |
163 } | |
164 else | |
165 { | |
166 resource.reset(new Folder(s)); | |
167 } | |
168 | |
169 if (resource.get() != NULL) | |
170 { | |
171 boost::posix_time::ptime t; | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
172 LookupTime(t, context_, publicId, level_, timeMetadata); |
4240 | 173 resource->SetCreationTime(t); |
174 target_.AddResource(resource.release()); | |
175 } | |
176 } | |
177 } | |
178 }; | |
179 | |
180 | |
181 class OrthancWebDav::DicomFileVisitor : public ServerContext::ILookupVisitor | |
182 { | |
183 private: | |
184 ServerContext& context_; | |
185 bool success_; | |
186 std::string& target_; | |
187 boost::posix_time::ptime& time_; | |
188 | |
189 public: | |
190 DicomFileVisitor(ServerContext& context, | |
191 std::string& target, | |
192 boost::posix_time::ptime& time) : | |
193 context_(context), | |
194 success_(false), | |
195 target_(target), | |
196 time_(time) | |
197 { | |
198 } | |
199 | |
200 bool IsSuccess() const | |
201 { | |
202 return success_; | |
203 } | |
204 | |
205 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE | |
206 { | |
207 return false; // (*) | |
208 } | |
209 | |
210 virtual void MarkAsComplete() ORTHANC_OVERRIDE | |
211 { | |
212 } | |
213 | |
214 virtual void Visit(const std::string& publicId, | |
215 const std::string& instanceId /* unused */, | |
216 const DicomMap& mainDicomTags, | |
217 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE | |
218 { | |
219 if (success_) | |
220 { | |
221 success_ = false; // Two matches => Error | |
222 } | |
223 else | |
224 { | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
225 LookupTime(time_, context_, publicId, ResourceType_Instance, MetadataType_Instance_ReceptionDate); |
4240 | 226 context_.ReadDicom(target_, publicId); |
227 success_ = true; | |
228 } | |
229 } | |
230 }; | |
231 | |
232 | |
233 class OrthancWebDav::OrthancJsonVisitor : public ServerContext::ILookupVisitor | |
234 { | |
235 private: | |
236 ServerContext& context_; | |
237 bool success_; | |
238 std::string& target_; | |
239 ResourceType level_; | |
240 | |
241 public: | |
242 OrthancJsonVisitor(ServerContext& context, | |
243 std::string& target, | |
244 ResourceType level) : | |
245 context_(context), | |
246 success_(false), | |
247 target_(target), | |
248 level_(level) | |
249 { | |
250 } | |
251 | |
252 bool IsSuccess() const | |
253 { | |
254 return success_; | |
255 } | |
256 | |
257 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE | |
258 { | |
259 return false; // (*) | |
260 } | |
261 | |
262 virtual void MarkAsComplete() ORTHANC_OVERRIDE | |
263 { | |
264 } | |
265 | |
266 virtual void Visit(const std::string& publicId, | |
267 const std::string& instanceId /* unused */, | |
268 const DicomMap& mainDicomTags, | |
269 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE | |
270 { | |
4554 | 271 Json::Value resource; |
272 if (context_.GetIndex().ExpandResource(resource, publicId, level_)) | |
4240 | 273 { |
274 if (success_) | |
275 { | |
276 success_ = false; // Two matches => Error | |
277 } | |
278 else | |
279 { | |
4554 | 280 target_ = resource.toStyledString(); |
4240 | 281 |
282 // Replace UNIX newlines with DOS newlines | |
283 boost::replace_all(target_, "\n", "\r\n"); | |
284 | |
285 success_ = true; | |
286 } | |
287 } | |
288 } | |
289 }; | |
290 | |
291 | |
292 class OrthancWebDav::ResourcesIndex : public boost::noncopyable | |
293 { | |
294 public: | |
295 typedef std::map<std::string, std::string> Map; | |
296 | |
297 private: | |
298 ServerContext& context_; | |
299 ResourceType level_; | |
300 std::string template_; | |
301 Map pathToResource_; | |
302 Map resourceToPath_; | |
303 | |
304 void CheckInvariants() | |
305 { | |
306 #ifndef NDEBUG | |
307 assert(pathToResource_.size() == resourceToPath_.size()); | |
308 | |
309 for (Map::const_iterator it = pathToResource_.begin(); it != pathToResource_.end(); ++it) | |
310 { | |
311 assert(resourceToPath_[it->second] == it->first); | |
312 } | |
313 | |
314 for (Map::const_iterator it = resourceToPath_.begin(); it != resourceToPath_.end(); ++it) | |
315 { | |
316 assert(pathToResource_[it->second] == it->first); | |
317 } | |
318 #endif | |
319 } | |
320 | |
321 void AddTags(DicomMap& target, | |
322 const std::string& resourceId, | |
323 ResourceType tagsFromLevel) | |
324 { | |
325 DicomMap tags; | |
326 if (context_.GetIndex().GetMainDicomTags(tags, resourceId, level_, tagsFromLevel)) | |
327 { | |
328 target.Merge(tags); | |
329 } | |
330 } | |
331 | |
332 void Register(const std::string& resourceId) | |
333 { | |
334 // Don't register twice the same resource | |
335 if (resourceToPath_.find(resourceId) == resourceToPath_.end()) | |
336 { | |
337 std::string name = template_; | |
338 | |
339 DicomMap tags; | |
340 | |
341 AddTags(tags, resourceId, level_); | |
342 | |
343 if (level_ == ResourceType_Study) | |
344 { | |
345 AddTags(tags, resourceId, ResourceType_Patient); | |
346 } | |
347 | |
348 DicomArray arr(tags); | |
349 for (size_t i = 0; i < arr.GetSize(); i++) | |
350 { | |
351 const DicomElement& element = arr.GetElement(i); | |
352 if (!element.GetValue().IsNull() && | |
353 !element.GetValue().IsBinary()) | |
354 { | |
355 const std::string tag = FromDcmtkBridge::GetTagName(element.GetTag(), ""); | |
356 boost::replace_all(name, "{{" + tag + "}}", element.GetValue().GetContent()); | |
357 } | |
358 } | |
359 | |
360 // Blank the tags that were not matched | |
361 static const boost::regex REGEX_BLANK_TAGS("{{.*?}}"); // non-greedy match | |
362 name = boost::regex_replace(name, REGEX_BLANK_TAGS, ""); | |
363 | |
364 // UTF-8 characters cannot be used on Windows XP | |
365 name = Toolbox::ConvertToAscii(name); | |
366 boost::replace_all(name, "/", ""); | |
367 boost::replace_all(name, "\\", ""); | |
368 | |
369 // Trim sequences of spaces as one single space | |
370 static const boost::regex REGEX_TRIM_SPACES("{{.*?}}"); | |
371 name = boost::regex_replace(name, REGEX_TRIM_SPACES, " "); | |
372 name = Toolbox::StripSpaces(name); | |
373 | |
374 size_t count = 0; | |
375 for (;;) | |
376 { | |
377 std::string path = name; | |
378 if (count > 0) | |
379 { | |
380 path += " (" + boost::lexical_cast<std::string>(count) + ")"; | |
381 } | |
382 | |
383 if (pathToResource_.find(path) == pathToResource_.end()) | |
384 { | |
385 pathToResource_[path] = resourceId; | |
386 resourceToPath_[resourceId] = path; | |
387 return; | |
388 } | |
389 | |
390 count++; | |
391 } | |
392 | |
393 throw OrthancException(ErrorCode_InternalError); | |
394 } | |
395 } | |
396 | |
397 public: | |
398 ResourcesIndex(ServerContext& context, | |
399 ResourceType level, | |
400 const std::string& templateString) : | |
401 context_(context), | |
402 level_(level), | |
403 template_(templateString) | |
404 { | |
405 } | |
406 | |
407 ResourceType GetLevel() const | |
408 { | |
409 return level_; | |
410 } | |
411 | |
412 void Refresh(std::set<std::string>& removedPaths /* out */, | |
413 const std::set<std::string>& resources) | |
414 { | |
415 CheckInvariants(); | |
416 | |
417 // Detect the resources that have been removed since last refresh | |
418 removedPaths.clear(); | |
419 std::set<std::string> removedResources; | |
420 | |
421 for (Map::iterator it = resourceToPath_.begin(); it != resourceToPath_.end(); ++it) | |
422 { | |
423 if (resources.find(it->first) == resources.end()) | |
424 { | |
425 const std::string& path = it->second; | |
426 | |
427 assert(pathToResource_.find(path) != pathToResource_.end()); | |
428 pathToResource_.erase(path); | |
429 removedPaths.insert(path); | |
430 | |
431 removedResources.insert(it->first); // Delay the removal to avoid disturbing the iterator | |
432 } | |
433 } | |
434 | |
435 // Remove the missing resources | |
436 for (std::set<std::string>::const_iterator it = removedResources.begin(); it != removedResources.end(); ++it) | |
437 { | |
438 assert(resourceToPath_.find(*it) != resourceToPath_.end()); | |
439 resourceToPath_.erase(*it); | |
440 } | |
441 | |
442 CheckInvariants(); | |
443 | |
444 for (std::set<std::string>::const_iterator it = resources.begin(); it != resources.end(); ++it) | |
445 { | |
446 Register(*it); | |
447 } | |
448 | |
449 CheckInvariants(); | |
450 } | |
451 | |
452 const Map& GetPathToResource() const | |
453 { | |
454 return pathToResource_; | |
455 } | |
456 }; | |
457 | |
458 | |
459 class OrthancWebDav::InstancesOfSeries : public INode | |
460 { | |
461 private: | |
462 ServerContext& context_; | |
463 std::string parentSeries_; | |
464 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
465 static bool LookupInstanceId(std::string& instanceId, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
466 const UriComponents& path) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
467 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
468 if (path.size() == 1 && |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
469 boost::ends_with(path[0], ".dcm")) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
470 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
471 instanceId = path[0].substr(0, path[0].size() - 4); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
472 return true; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
473 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
474 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
475 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
476 return false; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
477 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
478 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
479 |
4240 | 480 public: |
481 InstancesOfSeries(ServerContext& context, | |
482 const std::string& parentSeries) : | |
483 context_(context), | |
484 parentSeries_(parentSeries) | |
485 { | |
486 } | |
487 | |
488 virtual bool ListCollection(IWebDavBucket::Collection& target, | |
489 const UriComponents& path) ORTHANC_OVERRIDE | |
490 { | |
491 if (path.empty()) | |
492 { | |
493 std::list<std::string> resources; | |
494 try | |
495 { | |
496 context_.GetIndex().GetChildren(resources, parentSeries_); | |
497 } | |
498 catch (OrthancException&) | |
499 { | |
500 // Unknown (or deleted) parent series | |
501 return false; | |
502 } | |
503 | |
504 for (std::list<std::string>::const_iterator | |
505 it = resources.begin(); it != resources.end(); ++it) | |
506 { | |
507 boost::posix_time::ptime time; | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
508 LookupTime(time, context_, *it, ResourceType_Instance, MetadataType_Instance_ReceptionDate); |
4240 | 509 |
510 FileInfo info; | |
511 if (context_.GetIndex().LookupAttachment(info, *it, FileContentType_Dicom)) | |
512 { | |
513 std::unique_ptr<File> resource(new File(*it + ".dcm")); | |
514 resource->SetMimeType(MimeType_Dicom); | |
515 resource->SetContentLength(info.GetUncompressedSize()); | |
516 resource->SetCreationTime(time); | |
517 target.AddResource(resource.release()); | |
518 } | |
519 } | |
520 | |
521 return true; | |
522 } | |
523 else | |
524 { | |
525 return false; | |
526 } | |
527 } | |
528 | |
529 virtual bool GetFileContent(MimeType& mime, | |
530 std::string& content, | |
531 boost::posix_time::ptime& time, | |
532 const UriComponents& path) ORTHANC_OVERRIDE | |
533 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
534 std::string instanceId; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
535 if (LookupInstanceId(instanceId, path)) |
4240 | 536 { |
537 try | |
538 { | |
539 mime = MimeType_Dicom; | |
540 context_.ReadDicom(content, instanceId); | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
541 LookupTime(time, context_, instanceId, ResourceType_Instance, MetadataType_Instance_ReceptionDate); |
4240 | 542 return true; |
543 } | |
544 catch (OrthancException&) | |
545 { | |
546 // File was removed | |
547 return false; | |
548 } | |
549 } | |
550 else | |
551 { | |
552 return false; | |
553 } | |
554 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
555 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
556 virtual bool DeleteItem(const UriComponents& path) ORTHANC_OVERRIDE |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
557 { |
4252 | 558 if (path.empty()) |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
559 { |
4252 | 560 // Delete all |
561 std::list<std::string> resources; | |
562 try | |
563 { | |
564 context_.GetIndex().GetChildren(resources, parentSeries_); | |
565 } | |
566 catch (OrthancException&) | |
567 { | |
568 // Unknown (or deleted) parent series | |
569 return true; | |
570 } | |
571 | |
572 for (std::list<std::string>::const_iterator it = resources.begin(); | |
573 it != resources.end(); ++it) | |
574 { | |
575 Json::Value info; | |
576 context_.DeleteResource(info, *it, ResourceType_Instance); | |
577 } | |
578 | |
579 return true; | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
580 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
581 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
582 { |
4252 | 583 std::string instanceId; |
584 if (LookupInstanceId(instanceId, path)) | |
585 { | |
586 Json::Value info; | |
587 return context_.DeleteResource(info, instanceId, ResourceType_Instance); | |
588 } | |
589 else | |
590 { | |
591 return false; | |
592 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
593 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
594 } |
4240 | 595 }; |
596 | |
597 | |
598 | |
599 /** | |
600 * The "InternalNode" class corresponds to a non-leaf node in the | |
601 * WebDAV tree, that only contains subfolders (no file). | |
602 * | |
603 * TODO: Implement a LRU index to dynamically remove the oldest | |
604 * children on high RAM usage. | |
605 **/ | |
606 class OrthancWebDav::InternalNode : public INode | |
607 { | |
608 private: | |
609 typedef std::map<std::string, INode*> Children; | |
610 | |
611 Children children_; | |
612 | |
613 INode* GetChild(const std::string& path) // Don't delete the result pointer! | |
614 { | |
615 Children::const_iterator child = children_.find(path); | |
616 if (child == children_.end()) | |
617 { | |
4253 | 618 INode* node = CreateSubfolder(path); |
4240 | 619 |
4253 | 620 if (node == NULL) |
4240 | 621 { |
622 return NULL; | |
623 } | |
624 else | |
625 { | |
4253 | 626 children_[path] = node; |
627 return node; | |
4240 | 628 } |
629 } | |
630 else | |
631 { | |
632 assert(child->second != NULL); | |
633 return child->second; | |
634 } | |
635 } | |
636 | |
637 protected: | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
638 void InvalidateSubfolder(const std::string& path) |
4240 | 639 { |
640 Children::iterator child = children_.find(path); | |
641 if (child != children_.end()) | |
642 { | |
643 assert(child->second != NULL); | |
644 delete child->second; | |
645 children_.erase(child); | |
646 } | |
647 } | |
648 | |
649 virtual void Refresh() = 0; | |
650 | |
651 virtual bool ListSubfolders(IWebDavBucket::Collection& target) = 0; | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
652 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
653 virtual INode* CreateSubfolder(const std::string& path) = 0; |
4240 | 654 |
655 public: | |
656 virtual ~InternalNode() | |
657 { | |
658 for (Children::iterator it = children_.begin(); it != children_.end(); ++it) | |
659 { | |
660 assert(it->second != NULL); | |
661 delete it->second; | |
662 } | |
663 } | |
664 | |
665 virtual bool ListCollection(IWebDavBucket::Collection& target, | |
666 const UriComponents& path) | |
667 ORTHANC_OVERRIDE ORTHANC_FINAL | |
668 { | |
669 Refresh(); | |
670 | |
671 if (path.empty()) | |
672 { | |
673 return ListSubfolders(target); | |
674 } | |
675 else | |
676 { | |
677 // Recursivity | |
678 INode* child = GetChild(path[0]); | |
679 if (child == NULL) | |
680 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
681 // Must be "true" to allow DELETE on folders that are |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
682 // automatically removed through recursive deletion |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
683 return true; |
4240 | 684 } |
685 else | |
686 { | |
687 UriComponents subpath(path.begin() + 1, path.end()); | |
688 return child->ListCollection(target, subpath); | |
689 } | |
690 } | |
691 } | |
692 | |
693 virtual bool GetFileContent(MimeType& mime, | |
694 std::string& content, | |
695 boost::posix_time::ptime& time, | |
696 const UriComponents& path) | |
697 ORTHANC_OVERRIDE ORTHANC_FINAL | |
698 { | |
699 if (path.empty()) | |
700 { | |
701 return false; // An internal node doesn't correspond to a file | |
702 } | |
703 else | |
704 { | |
705 // Recursivity | |
706 Refresh(); | |
707 | |
708 INode* child = GetChild(path[0]); | |
709 if (child == NULL) | |
710 { | |
711 return false; | |
712 } | |
713 else | |
714 { | |
715 UriComponents subpath(path.begin() + 1, path.end()); | |
716 return child->GetFileContent(mime, content, time, subpath); | |
717 } | |
718 } | |
719 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
720 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
721 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
722 virtual bool DeleteItem(const UriComponents& path) ORTHANC_OVERRIDE ORTHANC_FINAL |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
723 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
724 Refresh(); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
725 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
726 if (path.empty()) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
727 { |
4252 | 728 IWebDavBucket::Collection collection; |
729 if (ListSubfolders(collection)) | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
730 { |
4252 | 731 std::set<std::string> content; |
732 collection.ListDisplayNames(content); | |
733 | |
734 for (std::set<std::string>::const_iterator | |
735 it = content.begin(); it != content.end(); ++it) | |
736 { | |
737 INode* node = GetChild(*it); | |
738 if (node) | |
739 { | |
740 node->DeleteItem(path); | |
741 } | |
742 } | |
743 | |
744 return true; | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
745 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
746 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
747 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
748 return false; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
749 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
750 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
751 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
752 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
753 INode* child = GetChild(path[0]); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
754 if (child == NULL) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
755 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
756 return true; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
757 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
758 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
759 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
760 // Recursivity |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
761 UriComponents subpath(path.begin() + 1, path.end()); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
762 return child->DeleteItem(subpath); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
763 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
764 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
765 } |
4240 | 766 }; |
767 | |
768 | |
769 class OrthancWebDav::ListOfResources : public InternalNode | |
770 { | |
771 private: | |
772 ServerContext& context_; | |
773 const Templates& templates_; | |
774 std::unique_ptr<ResourcesIndex> index_; | |
775 MetadataType timeMetadata_; | |
776 | |
777 protected: | |
778 virtual void Refresh() ORTHANC_OVERRIDE ORTHANC_FINAL | |
779 { | |
780 std::list<std::string> resources; | |
781 GetCurrentResources(resources); | |
782 | |
783 std::set<std::string> removedPaths; | |
784 index_->Refresh(removedPaths, std::set<std::string>(resources.begin(), resources.end())); | |
785 | |
786 // Remove the children whose associated resource doesn't exist anymore | |
787 for (std::set<std::string>::const_iterator | |
788 it = removedPaths.begin(); it != removedPaths.end(); ++it) | |
789 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
790 InvalidateSubfolder(*it); |
4240 | 791 } |
792 } | |
793 | |
794 virtual bool ListSubfolders(IWebDavBucket::Collection& target) ORTHANC_OVERRIDE ORTHANC_FINAL | |
795 { | |
796 if (index_->GetLevel() == ResourceType_Instance) | |
797 { | |
798 // Not a collection, no subfolders | |
799 return false; | |
800 } | |
801 else | |
802 { | |
803 const ResourcesIndex::Map& paths = index_->GetPathToResource(); | |
804 | |
805 for (ResourcesIndex::Map::const_iterator it = paths.begin(); it != paths.end(); ++it) | |
806 { | |
807 boost::posix_time::ptime time; | |
4460
6831de40acd9
New metadata automatically computed at the series level: "RemoteAET"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
808 LookupTime(time, context_, it->second, index_->GetLevel(), timeMetadata_); |
4240 | 809 |
810 std::unique_ptr<IWebDavBucket::Resource> resource(new IWebDavBucket::Folder(it->first)); | |
811 resource->SetCreationTime(time); | |
812 target.AddResource(resource.release()); | |
813 } | |
814 | |
815 return true; | |
816 } | |
817 } | |
818 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
819 virtual INode* CreateSubfolder(const std::string& path) ORTHANC_OVERRIDE ORTHANC_FINAL |
4240 | 820 { |
821 ResourcesIndex::Map::const_iterator resource = index_->GetPathToResource().find(path); | |
822 if (resource == index_->GetPathToResource().end()) | |
823 { | |
824 return NULL; | |
825 } | |
826 else | |
827 { | |
828 return CreateResourceNode(resource->second); | |
829 } | |
830 } | |
831 | |
832 ServerContext& GetContext() const | |
833 { | |
834 return context_; | |
835 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
836 |
4240 | 837 virtual void GetCurrentResources(std::list<std::string>& resources) = 0; |
838 | |
839 virtual INode* CreateResourceNode(const std::string& resource) = 0; | |
840 | |
841 public: | |
842 ListOfResources(ServerContext& context, | |
843 ResourceType level, | |
844 const Templates& templates) : | |
845 context_(context), | |
846 templates_(templates) | |
847 { | |
848 Templates::const_iterator t = templates.find(level); | |
849 if (t == templates.end()) | |
850 { | |
851 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
852 } | |
853 | |
854 index_.reset(new ResourcesIndex(context, level, t->second)); | |
855 | |
856 if (level == ResourceType_Instance) | |
857 { | |
858 timeMetadata_ = MetadataType_Instance_ReceptionDate; | |
859 } | |
860 else | |
861 { | |
862 timeMetadata_ = MetadataType_LastUpdate; | |
863 } | |
864 } | |
865 | |
866 ResourceType GetLevel() const | |
867 { | |
868 return index_->GetLevel(); | |
869 } | |
870 | |
871 const Templates& GetTemplates() const | |
872 { | |
873 return templates_; | |
874 } | |
875 }; | |
876 | |
877 | |
878 | |
879 class OrthancWebDav::SingleDicomResource : public ListOfResources | |
880 { | |
881 private: | |
882 std::string parentId_; | |
883 | |
884 protected: | |
885 virtual void GetCurrentResources(std::list<std::string>& resources) ORTHANC_OVERRIDE | |
886 { | |
887 try | |
888 { | |
889 GetContext().GetIndex().GetChildren(resources, parentId_); | |
890 } | |
891 catch (OrthancException&) | |
892 { | |
893 // Unknown parent resource | |
894 resources.clear(); | |
895 } | |
896 } | |
897 | |
898 virtual INode* CreateResourceNode(const std::string& resource) ORTHANC_OVERRIDE | |
899 { | |
900 if (GetLevel() == ResourceType_Instance) | |
901 { | |
902 return NULL; | |
903 } | |
904 else if (GetLevel() == ResourceType_Series) | |
905 { | |
906 return new InstancesOfSeries(GetContext(), resource); | |
907 } | |
908 else | |
909 { | |
910 ResourceType l = GetChildResourceType(GetLevel()); | |
911 return new SingleDicomResource(GetContext(), l, resource, GetTemplates()); | |
912 } | |
913 } | |
914 | |
915 public: | |
916 SingleDicomResource(ServerContext& context, | |
917 ResourceType level, | |
918 const std::string& parentId, | |
919 const Templates& templates) : | |
920 ListOfResources(context, level, templates), | |
921 parentId_(parentId) | |
922 { | |
923 } | |
924 }; | |
925 | |
926 | |
927 class OrthancWebDav::RootNode : public ListOfResources | |
928 { | |
929 protected: | |
930 virtual void GetCurrentResources(std::list<std::string>& resources) ORTHANC_OVERRIDE | |
931 { | |
932 GetContext().GetIndex().GetAllUuids(resources, GetLevel()); | |
933 } | |
934 | |
935 virtual INode* CreateResourceNode(const std::string& resource) ORTHANC_OVERRIDE | |
936 { | |
937 if (GetLevel() == ResourceType_Series) | |
938 { | |
939 return new InstancesOfSeries(GetContext(), resource); | |
940 } | |
941 else | |
942 { | |
943 ResourceType l = GetChildResourceType(GetLevel()); | |
944 return new SingleDicomResource(GetContext(), l, resource, GetTemplates()); | |
945 } | |
946 } | |
947 | |
948 public: | |
949 RootNode(ServerContext& context, | |
950 ResourceType level, | |
951 const Templates& templates) : | |
952 ListOfResources(context, level, templates) | |
953 { | |
954 } | |
955 }; | |
956 | |
957 | |
958 class OrthancWebDav::ListOfStudiesByDate : public ListOfResources | |
959 { | |
960 private: | |
961 std::string year_; | |
962 std::string month_; | |
963 | |
964 class Visitor : public ServerContext::ILookupVisitor | |
965 { | |
966 private: | |
967 std::list<std::string>& resources_; | |
968 | |
969 public: | |
4253 | 970 explicit Visitor(std::list<std::string>& resources) : |
4240 | 971 resources_(resources) |
972 { | |
973 } | |
974 | |
975 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE | |
976 { | |
977 return false; // (*) | |
978 } | |
979 | |
980 virtual void MarkAsComplete() ORTHANC_OVERRIDE | |
981 { | |
982 } | |
983 | |
984 virtual void Visit(const std::string& publicId, | |
985 const std::string& instanceId /* unused */, | |
986 const DicomMap& mainDicomTags, | |
987 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE | |
988 { | |
989 resources_.push_back(publicId); | |
990 } | |
991 }; | |
992 | |
993 protected: | |
994 virtual void GetCurrentResources(std::list<std::string>& resources) ORTHANC_OVERRIDE | |
995 { | |
996 DatabaseLookup query; | |
997 query.AddRestConstraint(DICOM_TAG_STUDY_DATE, year_ + month_ + "01-" + year_ + month_ + "31", | |
998 true /* case sensitive */, true /* mandatory tag */); | |
999 | |
1000 Visitor visitor(resources); | |
1001 GetContext().Apply(visitor, query, ResourceType_Study, 0 /* since */, 0 /* no limit */); | |
1002 } | |
1003 | |
1004 virtual INode* CreateResourceNode(const std::string& resource) ORTHANC_OVERRIDE | |
1005 { | |
1006 return new SingleDicomResource(GetContext(), ResourceType_Series, resource, GetTemplates()); | |
1007 } | |
1008 | |
1009 public: | |
1010 ListOfStudiesByDate(ServerContext& context, | |
1011 const std::string& year, | |
1012 const std::string& month, | |
1013 const Templates& templates) : | |
1014 ListOfResources(context, ResourceType_Study, templates), | |
1015 year_(year), | |
1016 month_(month) | |
1017 { | |
1018 if (year.size() != 4 || | |
1019 month.size() != 2) | |
1020 { | |
1021 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
1022 } | |
1023 } | |
1024 }; | |
1025 | |
1026 | |
1027 class OrthancWebDav::ListOfStudiesByMonth : public InternalNode | |
1028 { | |
1029 private: | |
1030 ServerContext& context_; | |
1031 std::string year_; | |
1032 const Templates& templates_; | |
1033 | |
1034 class Visitor : public ServerContext::ILookupVisitor | |
1035 { | |
1036 private: | |
1037 std::set<std::string> months_; | |
1038 | |
1039 public: | |
1040 const std::set<std::string>& GetMonths() const | |
1041 { | |
1042 return months_; | |
1043 } | |
1044 | |
1045 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE | |
1046 { | |
1047 return false; // (*) | |
1048 } | |
1049 | |
1050 virtual void MarkAsComplete() ORTHANC_OVERRIDE | |
1051 { | |
1052 } | |
1053 | |
1054 virtual void Visit(const std::string& publicId, | |
1055 const std::string& instanceId /* unused */, | |
1056 const DicomMap& mainDicomTags, | |
1057 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE | |
1058 { | |
1059 std::string s; | |
1060 if (mainDicomTags.LookupStringValue(s, DICOM_TAG_STUDY_DATE, false) && | |
1061 s.size() == 8) | |
1062 { | |
1063 months_.insert(s.substr(4, 2)); // Get the month from "YYYYMMDD" | |
1064 } | |
1065 } | |
1066 }; | |
1067 | |
1068 protected: | |
1069 virtual void Refresh() ORTHANC_OVERRIDE | |
1070 { | |
1071 } | |
1072 | |
1073 virtual bool ListSubfolders(IWebDavBucket::Collection& target) ORTHANC_OVERRIDE | |
1074 { | |
1075 DatabaseLookup query; | |
1076 query.AddRestConstraint(DICOM_TAG_STUDY_DATE, year_ + "0101-" + year_ + "1231", | |
1077 true /* case sensitive */, true /* mandatory tag */); | |
1078 | |
1079 Visitor visitor; | |
1080 context_.Apply(visitor, query, ResourceType_Study, 0 /* since */, 0 /* no limit */); | |
1081 | |
1082 for (std::set<std::string>::const_iterator it = visitor.GetMonths().begin(); | |
1083 it != visitor.GetMonths().end(); ++it) | |
1084 { | |
1085 target.AddResource(new IWebDavBucket::Folder(year_ + "-" + *it)); | |
1086 } | |
1087 | |
1088 return true; | |
1089 } | |
1090 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1091 virtual INode* CreateSubfolder(const std::string& path) ORTHANC_OVERRIDE |
4240 | 1092 { |
1093 if (path.size() != 7) // Format: "YYYY-MM" | |
1094 { | |
1095 throw OrthancException(ErrorCode_InternalError); | |
1096 } | |
1097 else | |
1098 { | |
1099 const std::string year = path.substr(0, 4); | |
1100 const std::string month = path.substr(5, 2); | |
1101 return new ListOfStudiesByDate(context_, year, month, templates_); | |
1102 } | |
1103 } | |
1104 | |
1105 public: | |
1106 ListOfStudiesByMonth(ServerContext& context, | |
1107 const std::string& year, | |
1108 const Templates& templates) : | |
1109 context_(context), | |
1110 year_(year), | |
1111 templates_(templates) | |
1112 { | |
1113 if (year_.size() != 4) | |
1114 { | |
1115 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
1116 } | |
1117 } | |
1118 }; | |
1119 | |
1120 | |
1121 class OrthancWebDav::ListOfStudiesByYear : public InternalNode | |
1122 { | |
1123 private: | |
1124 ServerContext& context_; | |
1125 const Templates& templates_; | |
1126 | |
1127 protected: | |
1128 virtual void Refresh() ORTHANC_OVERRIDE | |
1129 { | |
1130 } | |
1131 | |
1132 virtual bool ListSubfolders(IWebDavBucket::Collection& target) ORTHANC_OVERRIDE | |
1133 { | |
1134 std::list<std::string> resources; | |
1135 context_.GetIndex().GetAllUuids(resources, ResourceType_Study); | |
1136 | |
1137 std::set<std::string> years; | |
1138 | |
1139 for (std::list<std::string>::const_iterator it = resources.begin(); it != resources.end(); ++it) | |
1140 { | |
1141 DicomMap tags; | |
1142 std::string studyDate; | |
1143 if (context_.GetIndex().GetMainDicomTags(tags, *it, ResourceType_Study, ResourceType_Study) && | |
1144 tags.LookupStringValue(studyDate, DICOM_TAG_STUDY_DATE, false) && | |
1145 studyDate.size() == 8) | |
1146 { | |
1147 years.insert(studyDate.substr(0, 4)); // Get the year from "YYYYMMDD" | |
1148 } | |
1149 } | |
1150 | |
1151 for (std::set<std::string>::const_iterator it = years.begin(); it != years.end(); ++it) | |
1152 { | |
1153 target.AddResource(new IWebDavBucket::Folder(*it)); | |
1154 } | |
1155 | |
1156 return true; | |
1157 } | |
1158 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1159 virtual INode* CreateSubfolder(const std::string& path) ORTHANC_OVERRIDE |
4240 | 1160 { |
1161 return new ListOfStudiesByMonth(context_, path, templates_); | |
1162 } | |
1163 | |
1164 public: | |
1165 ListOfStudiesByYear(ServerContext& context, | |
1166 const Templates& templates) : | |
1167 context_(context), | |
1168 templates_(templates) | |
1169 { | |
1170 } | |
1171 }; | |
1172 | |
1173 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1174 class OrthancWebDav::DicomDeleteVisitor : public ServerContext::ILookupVisitor |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1175 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1176 private: |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1177 ServerContext& context_; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1178 ResourceType level_; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1179 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1180 public: |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1181 DicomDeleteVisitor(ServerContext& context, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1182 ResourceType level) : |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1183 context_(context), |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1184 level_(level) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1185 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1186 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1187 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1188 virtual bool IsDicomAsJsonNeeded() const ORTHANC_OVERRIDE |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1189 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1190 return false; // (*) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1191 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1192 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1193 virtual void MarkAsComplete() ORTHANC_OVERRIDE |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1194 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1195 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1196 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1197 virtual void Visit(const std::string& publicId, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1198 const std::string& instanceId /* unused */, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1199 const DicomMap& mainDicomTags /* unused */, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1200 const Json::Value* dicomAsJson /* unused (*) */) ORTHANC_OVERRIDE |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1201 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1202 Json::Value info; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1203 context_.DeleteResource(info, publicId, level_); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1204 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1205 }; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1206 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1207 |
4240 | 1208 void OrthancWebDav::AddVirtualFile(Collection& collection, |
1209 const UriComponents& path, | |
1210 const std::string& filename) | |
1211 { | |
1212 MimeType mime; | |
1213 std::string content; | |
4246 | 1214 boost::posix_time::ptime modification; // Unused, let the date be set to "GetNow()" |
4240 | 1215 |
1216 UriComponents p = path; | |
1217 p.push_back(filename); | |
1218 | |
1219 if (GetFileContent(mime, content, modification, p)) | |
1220 { | |
1221 std::unique_ptr<File> f(new File(filename)); | |
1222 f->SetMimeType(mime); | |
1223 f->SetContentLength(content.size()); | |
1224 collection.AddResource(f.release()); | |
1225 } | |
1226 } | |
1227 | |
1228 | |
1229 void OrthancWebDav::UploadWorker(OrthancWebDav* that) | |
1230 { | |
1231 assert(that != NULL); | |
1232 | |
1233 boost::posix_time::ptime lastModification = GetNow(); | |
1234 | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1235 while (that->uploadRunning_) |
4240 | 1236 { |
1237 std::unique_ptr<IDynamicObject> obj(that->uploadQueue_.Dequeue(100)); | |
1238 if (obj.get() != NULL) | |
1239 { | |
1240 that->Upload(reinterpret_cast<const SingleValueObject<std::string>&>(*obj).GetValue()); | |
1241 lastModification = GetNow(); | |
1242 } | |
4245
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1243 else if (GetNow() - lastModification > boost::posix_time::seconds(30)) |
4240 | 1244 { |
4245
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1245 /** |
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1246 * After every 30 seconds of inactivity, remove the empty |
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1247 * folders. This delay is needed to avoid removing |
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1248 * just-created folders before the remote WebDAV has time to |
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1249 * write files into it. |
c70df925151e
RequestOrigin_WebDav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4244
diff
changeset
|
1250 **/ |
4268
0ae2ca210077
new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4264
diff
changeset
|
1251 LOG(TRACE) << "Cleaning up the empty WebDAV upload folders"; |
4240 | 1252 that->uploads_.RemoveEmptyFolders(); |
1253 lastModification = GetNow(); | |
1254 } | |
1255 } | |
1256 } | |
1257 | |
1258 | |
1259 void OrthancWebDav::Upload(const std::string& path) | |
1260 { | |
1261 UriComponents uri; | |
1262 Toolbox::SplitUriComponents(uri, path); | |
1263 | |
1264 LOG(INFO) << "Upload from WebDAV: " << path; | |
1265 | |
1266 MimeType mime; | |
1267 std::string content; | |
1268 boost::posix_time::ptime time; | |
1269 if (uploads_.GetFileContent(mime, content, time, uri)) | |
1270 { | |
4244
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1271 bool success = false; |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1272 |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1273 if (ZipReader::IsZipMemoryBuffer(content)) |
4240 | 1274 { |
4374
79ef2b6d8e76
there will be a 1.8.2 release before 1.9.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4358
diff
changeset
|
1275 // New in Orthanc 1.8.2 |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1276 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromMemory(content)); |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1277 |
4386 | 1278 std::string filename, uncompressedFile; |
1279 while (reader->ReadNextFile(filename, uncompressedFile)) | |
4244
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1280 { |
4386 | 1281 if (!uncompressedFile.empty()) |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1282 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1283 LOG(INFO) << "Uploading DICOM file extracted from a ZIP archive in WebDAV: " << filename; |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1284 |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1285 std::unique_ptr<DicomInstanceToStore> instance(DicomInstanceToStore::CreateFromBuffer(uncompressedFile)); |
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1286 instance->SetOrigin(DicomInstanceOrigin::FromWebDav()); |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1287 |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1288 std::string publicId; |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1289 |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1290 try |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1291 { |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1292 context_.Store(publicId, *instance, StoreInstanceMode_Default); |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1293 } |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1294 catch (OrthancException& e) |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1295 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1296 if (e.GetErrorCode() == ErrorCode_BadFileFormat) |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1297 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1298 LOG(ERROR) << "Cannot import non-DICOM file from ZIP archive: " << filename; |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1299 } |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1300 } |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1301 } |
4244
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1302 } |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1303 |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1304 success = true; |
4240 | 1305 } |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1306 else |
4240 | 1307 { |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1308 std::unique_ptr<DicomInstanceToStore> instance(DicomInstanceToStore::CreateFromBuffer(content)); |
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1309 instance->SetOrigin(DicomInstanceOrigin::FromWebDav()); |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1310 |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1311 try |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1312 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1313 std::string publicId; |
4508
8f9090b137f1
Optimization in C-STORE SCP by avoiding an unnecessary DICOM parsing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4460
diff
changeset
|
1314 StoreStatus status = context_.Store(publicId, *instance, StoreInstanceMode_Default); |
4358
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1315 if (status == StoreStatus_Success || |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1316 status == StoreStatus_AlreadyStored) |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1317 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1318 LOG(INFO) << "Successfully imported DICOM instance from WebDAV: " |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1319 << path << " (Orthanc ID: " << publicId << ")"; |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1320 success = true; |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1321 } |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1322 } |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1323 catch (OrthancException& e) |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1324 { |
d6929f052ec4
ZIP archives containing DICOM files can be uploaded using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
1325 } |
4244
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1326 } |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1327 |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1328 uploads_.DeleteItem(uri); |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1329 |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1330 if (!success) |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1331 { |
416c35da7d25
robustness against non-dicom files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4243
diff
changeset
|
1332 LOG(WARNING) << "Cannot import DICOM instance from WebWAV (maybe not a DICOM file): " << path; |
4240 | 1333 } |
1334 } | |
1335 } | |
1336 | |
1337 | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1338 OrthancWebDav::INode& OrthancWebDav::GetRootNode(const std::string& rootPath) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1339 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1340 if (rootPath == BY_PATIENTS) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1341 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1342 return *patients_; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1343 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1344 else if (rootPath == BY_STUDIES) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1345 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1346 return *studies_; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1347 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1348 else if (rootPath == BY_DATES) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1349 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1350 return *dates_; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1351 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1352 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1353 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1354 throw OrthancException(ErrorCode_InternalError); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1355 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1356 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1357 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1358 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1359 OrthancWebDav::OrthancWebDav(ServerContext& context, |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1360 bool allowDicomDelete, |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1361 bool allowUpload) : |
4240 | 1362 context_(context), |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1363 allowDicomDelete_(allowDicomDelete), |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1364 allowUpload_(allowUpload), |
4240 | 1365 uploads_(false /* store uploads as temporary files */), |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1366 uploadRunning_(false) |
4240 | 1367 { |
1368 patientsTemplates_[ResourceType_Patient] = "{{PatientID}} - {{PatientName}}"; | |
1369 patientsTemplates_[ResourceType_Study] = "{{StudyDate}} - {{StudyDescription}}"; | |
1370 patientsTemplates_[ResourceType_Series] = "{{Modality}} - {{SeriesDescription}}"; | |
1371 | |
1372 studiesTemplates_[ResourceType_Study] = "{{PatientID}} - {{PatientName}} - {{StudyDescription}}"; | |
1373 studiesTemplates_[ResourceType_Series] = patientsTemplates_[ResourceType_Series]; | |
1374 | |
1375 patients_.reset(new RootNode(context, ResourceType_Patient, patientsTemplates_)); | |
1376 studies_.reset(new RootNode(context, ResourceType_Study, studiesTemplates_)); | |
1377 dates_.reset(new ListOfStudiesByYear(context, studiesTemplates_)); | |
1378 } | |
1379 | |
1380 | |
1381 bool OrthancWebDav::IsExistingFolder(const UriComponents& path) | |
1382 { | |
1383 if (path.empty()) | |
1384 { | |
1385 return true; | |
1386 } | |
1387 else if (path[0] == BY_UIDS) | |
1388 { | |
1389 return (path.size() <= 3 && | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1390 (path.size() != 3 || path[2] != STUDY_INFO)); |
4240 | 1391 } |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1392 else if (path[0] == BY_PATIENTS || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1393 path[0] == BY_STUDIES || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1394 path[0] == BY_DATES) |
4240 | 1395 { |
4252 | 1396 IWebDavBucket::Collection collection; |
1397 return GetRootNode(path[0]).ListCollection(collection, UriComponents(path.begin() + 1, path.end())); | |
4240 | 1398 } |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1399 else if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1400 path[0] == UPLOADS) |
4240 | 1401 { |
1402 return uploads_.IsExistingFolder(UriComponents(path.begin() + 1, path.end())); | |
1403 } | |
1404 else | |
1405 { | |
1406 return false; | |
1407 } | |
1408 } | |
1409 | |
1410 | |
1411 bool OrthancWebDav::ListCollection(Collection& collection, | |
1412 const UriComponents& path) | |
1413 { | |
1414 if (path.empty()) | |
1415 { | |
4241 | 1416 collection.AddResource(new Folder(BY_DATES)); |
4240 | 1417 collection.AddResource(new Folder(BY_PATIENTS)); |
1418 collection.AddResource(new Folder(BY_STUDIES)); | |
1419 collection.AddResource(new Folder(BY_UIDS)); | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1420 |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1421 if (allowUpload_) |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1422 { |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1423 collection.AddResource(new Folder(UPLOADS)); |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1424 } |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1425 |
4240 | 1426 return true; |
1427 } | |
1428 else if (path[0] == BY_UIDS) | |
1429 { | |
1430 DatabaseLookup query; | |
1431 ResourceType level; | |
1432 size_t limit = 0; // By default, no limits | |
1433 | |
1434 if (path.size() == 1) | |
1435 { | |
1436 level = ResourceType_Study; | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1437 limit = 0; // TODO - Should we limit here? |
4240 | 1438 } |
1439 else if (path.size() == 2) | |
1440 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1441 AddVirtualFile(collection, path, STUDY_INFO); |
4240 | 1442 |
1443 level = ResourceType_Series; | |
1444 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], | |
1445 true /* case sensitive */, true /* mandatory tag */); | |
1446 } | |
1447 else if (path.size() == 3) | |
1448 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1449 AddVirtualFile(collection, path, SERIES_INFO); |
4240 | 1450 |
1451 level = ResourceType_Instance; | |
1452 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], | |
1453 true /* case sensitive */, true /* mandatory tag */); | |
1454 query.AddRestConstraint(DICOM_TAG_SERIES_INSTANCE_UID, path[2], | |
1455 true /* case sensitive */, true /* mandatory tag */); | |
1456 } | |
1457 else | |
1458 { | |
1459 return false; | |
1460 } | |
1461 | |
1462 DicomIdentifiersVisitor visitor(context_, collection, level); | |
1463 context_.Apply(visitor, query, level, 0 /* since */, limit); | |
1464 | |
1465 return true; | |
1466 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1467 else if (path[0] == BY_PATIENTS || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1468 path[0] == BY_STUDIES || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1469 path[0] == BY_DATES) |
4240 | 1470 { |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1471 return GetRootNode(path[0]).ListCollection(collection, UriComponents(path.begin() + 1, path.end())); |
4240 | 1472 } |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1473 else if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1474 path[0] == UPLOADS) |
4240 | 1475 { |
1476 return uploads_.ListCollection(collection, UriComponents(path.begin() + 1, path.end())); | |
1477 } | |
1478 else | |
1479 { | |
1480 return false; | |
1481 } | |
1482 } | |
1483 | |
1484 | |
1485 bool OrthancWebDav::GetFileContent(MimeType& mime, | |
1486 std::string& content, | |
1487 boost::posix_time::ptime& modificationTime, | |
1488 const UriComponents& path) | |
1489 { | |
1490 if (path.empty()) | |
1491 { | |
1492 return false; | |
1493 } | |
1494 else if (path[0] == BY_UIDS) | |
1495 { | |
1496 if (path.size() == 3 && | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1497 path[2] == STUDY_INFO) |
4240 | 1498 { |
1499 DatabaseLookup query; | |
1500 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], | |
1501 true /* case sensitive */, true /* mandatory tag */); | |
1502 | |
1503 OrthancJsonVisitor visitor(context_, content, ResourceType_Study); | |
1504 context_.Apply(visitor, query, ResourceType_Study, 0 /* since */, 0 /* no limit */); | |
1505 | |
1506 mime = MimeType_Json; | |
1507 return visitor.IsSuccess(); | |
1508 } | |
1509 else if (path.size() == 4 && | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1510 path[3] == SERIES_INFO) |
4240 | 1511 { |
1512 DatabaseLookup query; | |
1513 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], | |
1514 true /* case sensitive */, true /* mandatory tag */); | |
1515 query.AddRestConstraint(DICOM_TAG_SERIES_INSTANCE_UID, path[2], | |
1516 true /* case sensitive */, true /* mandatory tag */); | |
1517 | |
1518 OrthancJsonVisitor visitor(context_, content, ResourceType_Series); | |
1519 context_.Apply(visitor, query, ResourceType_Series, 0 /* since */, 0 /* no limit */); | |
1520 | |
1521 mime = MimeType_Json; | |
1522 return visitor.IsSuccess(); | |
1523 } | |
1524 else if (path.size() == 4 && | |
1525 boost::ends_with(path[3], ".dcm")) | |
1526 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1527 const std::string sopInstanceUid = path[3].substr(0, path[3].size() - 4); |
4240 | 1528 |
1529 DatabaseLookup query; | |
1530 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], | |
1531 true /* case sensitive */, true /* mandatory tag */); | |
1532 query.AddRestConstraint(DICOM_TAG_SERIES_INSTANCE_UID, path[2], | |
1533 true /* case sensitive */, true /* mandatory tag */); | |
1534 query.AddRestConstraint(DICOM_TAG_SOP_INSTANCE_UID, sopInstanceUid, | |
1535 true /* case sensitive */, true /* mandatory tag */); | |
1536 | |
1537 DicomFileVisitor visitor(context_, content, modificationTime); | |
1538 context_.Apply(visitor, query, ResourceType_Instance, 0 /* since */, 0 /* no limit */); | |
1539 | |
1540 mime = MimeType_Dicom; | |
1541 return visitor.IsSuccess(); | |
1542 } | |
1543 else | |
1544 { | |
1545 return false; | |
1546 } | |
1547 } | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1548 else if (path[0] == BY_PATIENTS || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1549 path[0] == BY_STUDIES || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1550 path[0] == BY_DATES) |
4240 | 1551 { |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1552 return GetRootNode(path[0]).GetFileContent(mime, content, modificationTime, UriComponents(path.begin() + 1, path.end())); |
4241 | 1553 } |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1554 else if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1555 path[0] == UPLOADS) |
4240 | 1556 { |
1557 return uploads_.GetFileContent(mime, content, modificationTime, UriComponents(path.begin() + 1, path.end())); | |
1558 } | |
1559 else | |
1560 { | |
1561 return false; | |
1562 } | |
1563 } | |
1564 | |
1565 | |
1566 bool OrthancWebDav::StoreFile(const std::string& content, | |
1567 const UriComponents& path) | |
1568 { | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1569 if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1570 path.size() >= 1 && |
4240 | 1571 path[0] == UPLOADS) |
1572 { | |
1573 UriComponents subpath(UriComponents(path.begin() + 1, path.end())); | |
1574 | |
1575 if (uploads_.StoreFile(content, subpath)) | |
1576 { | |
1577 if (!content.empty()) | |
1578 { | |
1579 uploadQueue_.Enqueue(new SingleValueObject<std::string>(Toolbox::FlattenUri(subpath))); | |
1580 } | |
1581 return true; | |
1582 } | |
1583 else | |
1584 { | |
1585 return false; | |
1586 } | |
1587 } | |
1588 else | |
1589 { | |
1590 return false; | |
1591 } | |
1592 } | |
1593 | |
1594 | |
1595 bool OrthancWebDav::CreateFolder(const UriComponents& path) | |
1596 { | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1597 if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1598 path.size() >= 1 && |
4240 | 1599 path[0] == UPLOADS) |
1600 { | |
1601 return uploads_.CreateFolder(UriComponents(path.begin() + 1, path.end())); | |
1602 } | |
1603 else | |
1604 { | |
1605 return false; | |
1606 } | |
1607 } | |
1608 | |
1609 | |
1610 bool OrthancWebDav::DeleteItem(const std::vector<std::string>& path) | |
1611 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1612 if (path.empty()) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1613 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1614 return false; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1615 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1616 else if (path[0] == BY_UIDS && |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1617 path.size() >= 2 && |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1618 path.size() <= 4) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1619 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1620 if (allowDicomDelete_) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1621 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1622 ResourceType level; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1623 DatabaseLookup query; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1624 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1625 query.AddRestConstraint(DICOM_TAG_STUDY_INSTANCE_UID, path[1], |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1626 true /* case sensitive */, true /* mandatory tag */); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1627 level = ResourceType_Study; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1628 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1629 if (path.size() >= 3) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1630 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1631 if (path[2] == STUDY_INFO) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1632 { |
4252 | 1633 return true; // Allow deletion of virtual files (to avoid blocking recursive DELETE) |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1634 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1635 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1636 query.AddRestConstraint(DICOM_TAG_SERIES_INSTANCE_UID, path[2], |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1637 true /* case sensitive */, true /* mandatory tag */); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1638 level = ResourceType_Series; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1639 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1640 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1641 if (path.size() == 4) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1642 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1643 if (path[3] == SERIES_INFO) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1644 { |
4252 | 1645 return true; // Allow deletion of virtual files (to avoid blocking recursive DELETE) |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1646 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1647 else if (boost::ends_with(path[3], ".dcm")) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1648 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1649 const std::string sopInstanceUid = path[3].substr(0, path[3].size() - 4); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1650 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1651 query.AddRestConstraint(DICOM_TAG_SOP_INSTANCE_UID, sopInstanceUid, |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1652 true /* case sensitive */, true /* mandatory tag */); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1653 level = ResourceType_Instance; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1654 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1655 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1656 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1657 return false; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1658 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1659 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1660 |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1661 DicomDeleteVisitor visitor(context_, level); |
4252 | 1662 context_.Apply(visitor, query, level, 0 /* since */, 0 /* no limit */); |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1663 return true; |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1664 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1665 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1666 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1667 return false; // read-only |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1668 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1669 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1670 else if (path[0] == BY_PATIENTS || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1671 path[0] == BY_STUDIES || |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1672 path[0] == BY_DATES) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1673 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1674 if (allowDicomDelete_) |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1675 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1676 return GetRootNode(path[0]).DeleteItem(UriComponents(path.begin() + 1, path.end())); |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1677 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1678 else |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1679 { |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1680 return false; // read-only |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1681 } |
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1682 } |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1683 else if (allowUpload_ && |
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1684 path[0] == UPLOADS) |
4240 | 1685 { |
1686 return uploads_.DeleteItem(UriComponents(path.begin() + 1, path.end())); | |
1687 } | |
1688 else | |
1689 { | |
4242
5cfa6ba75dfc
deleting resources from Orthanc WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4241
diff
changeset
|
1690 return false; |
4240 | 1691 } |
1692 } | |
1693 | |
1694 | |
1695 void OrthancWebDav::Start() | |
1696 { | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1697 if (uploadRunning_) |
4240 | 1698 { |
1699 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1700 } | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1701 else if (allowUpload_) |
4240 | 1702 { |
1703 LOG(INFO) << "Starting the WebDAV upload thread"; | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1704 uploadRunning_ = true; |
4240 | 1705 uploadThread_ = boost::thread(UploadWorker, this); |
1706 } | |
1707 } | |
1708 | |
1709 | |
1710 void OrthancWebDav::Stop() | |
1711 { | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1712 if (uploadRunning_) |
4240 | 1713 { |
1714 LOG(INFO) << "Stopping the WebDAV upload thread"; | |
4243
64f57c9d5f79
configuration options for webdav
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4242
diff
changeset
|
1715 uploadRunning_ = false; |
4240 | 1716 if (uploadThread_.joinable()) |
1717 { | |
1718 uploadThread_.join(); | |
1719 } | |
1720 } | |
1721 } | |
1722 } |