comparison OrthancServer/Plugins/Samples/WebDavFilesystem/Plugin.cpp @ 4943:47d734fa30f6

adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Mar 2022 17:21:02 +0100
parents
children 0ea402b4d901
comparison
equal deleted inserted replaced
4942:bd7ad1cb40b6 4943:47d734fa30f6
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "../Common/OrthancPluginCppWrapper.h"
24
25 #include <boost/thread/mutex.hpp>
26
27
28 class Resource : public boost::noncopyable
29 {
30 private:
31 boost::posix_time::ptime dateTime_;
32
33 public:
34 Resource() :
35 dateTime_(boost::posix_time::second_clock::universal_time())
36 {
37 }
38
39 virtual ~Resource()
40 {
41 }
42
43 const boost::posix_time::ptime& GetDateTime() const
44 {
45 return dateTime_;
46 }
47
48 virtual bool IsFolder() const = 0;
49
50 virtual Resource* LookupPath(const std::vector<std::string>& path) = 0;
51 };
52
53
54 class File : public Resource
55 {
56 private:
57 std::string content_;
58
59 public:
60 File(const void* data,
61 size_t size) :
62 content_(reinterpret_cast<const char*>(data), size)
63 {
64 }
65
66 const std::string& GetContent() const
67 {
68 return content_;
69 }
70
71 virtual bool IsFolder() const
72 {
73 return false;
74 }
75
76 virtual Resource* LookupPath(const std::vector<std::string>& path)
77 {
78 if (path.empty())
79 {
80 return this;
81 }
82 else
83 {
84 return NULL;
85 }
86 }
87 };
88
89
90 class Folder : public Resource
91 {
92 private:
93 typedef std::map<std::string, Resource*> Content;
94
95 Content content_;
96
97 public:
98 virtual ~Folder()
99 {
100 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
101 {
102 assert(it->second != NULL);
103 delete it->second;
104 }
105 }
106
107 virtual bool IsFolder() const
108 {
109 return true;
110 }
111
112 virtual Resource* LookupPath(const std::vector<std::string>& path)
113 {
114 if (path.empty())
115 {
116 return this;
117 }
118 else
119 {
120 Content::const_iterator found = content_.find(path[0]);
121 if (found == content_.end())
122 {
123 return NULL;
124 }
125 else
126 {
127 std::vector<std::string> childPath(path.size() - 1);
128
129 for (size_t i = 0; i < childPath.size(); i++)
130 {
131 childPath[i] = path[i + 1];
132 }
133
134 return found->second->LookupPath(childPath);
135 }
136 }
137 }
138
139 void ListContent(std::list<OrthancPlugins::IWebDavCollection::FileInfo>& files,
140 std::list<OrthancPlugins::IWebDavCollection::FolderInfo>& subfolders) const
141 {
142 for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it)
143 {
144 assert(it->second != NULL);
145
146 const std::string dateTime = boost::posix_time::to_iso_string(it->second->GetDateTime());
147
148 if (it->second->IsFolder())
149 {
150 subfolders.push_back(OrthancPlugins::IWebDavCollection::FolderInfo(it->first, dateTime));
151 }
152 else
153 {
154 const File& f = dynamic_cast<const File&>(*it->second);
155 files.push_back(OrthancPlugins::IWebDavCollection::FileInfo(it->first, f.GetContent().size(), dateTime));
156 }
157 }
158 }
159
160 void StoreFile(const std::string& name,
161 File* f)
162 {
163 std::unique_ptr<File> protection(f);
164
165 if (content_.find(name) != content_.end())
166 {
167 OrthancPlugins::LogError("Already existing: " + name);
168 ORTHANC_PLUGINS_THROW_EXCEPTION(BadRequest);
169 }
170 else
171 {
172 content_[name] = protection.release();
173 }
174 }
175
176 void CreateSubfolder(const std::string& name)
177 {
178 if (content_.find(name) != content_.end())
179 {
180 OrthancPlugins::LogError("Already existing: " + name);
181 ORTHANC_PLUGINS_THROW_EXCEPTION(BadRequest);
182 }
183 else
184 {
185 content_[name] = new Folder;
186 }
187 }
188
189 void DeleteItem(const std::string& name)
190 {
191 Content::iterator found = content_.find(name);
192
193 if (found == content_.end())
194 {
195 OrthancPlugins::LogError("Cannot delete inexistent path: " + name);
196 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentItem);
197 }
198 else
199 {
200 assert(found->second != NULL);
201 delete found->second;
202 content_.erase(found);
203 }
204 }
205 };
206
207
208 class WebDavFilesystem : public OrthancPlugins::IWebDavCollection
209 {
210 private:
211 boost::mutex mutex_;
212 std::unique_ptr<Resource> root_;
213
214 static std::vector<std::string> GetParentPath(const std::vector<std::string>& path)
215 {
216 if (path.empty())
217 {
218 OrthancPlugins::LogError("Empty path");
219 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
220 }
221 else
222 {
223 std::vector<std::string> p(path.size() - 1);
224
225 for (size_t i = 0; i < p.size(); i++)
226 {
227 p[i] = path[i];
228 }
229
230 return p;
231 }
232 }
233
234 public:
235 WebDavFilesystem() :
236 root_(new Folder)
237 {
238 }
239
240 virtual bool IsExistingFolder(const std::vector<std::string>& path)
241 {
242 boost::mutex::scoped_lock lock(mutex_);
243
244 Resource* resource = root_->LookupPath(path);
245 return (resource != NULL &&
246 resource->IsFolder());
247 }
248
249 virtual bool ListFolder(std::list<FileInfo>& files,
250 std::list<FolderInfo>& subfolders,
251 const std::vector<std::string>& path)
252 {
253 boost::mutex::scoped_lock lock(mutex_);
254
255 Resource* resource = root_->LookupPath(path);
256 if (resource != NULL &&
257 resource->IsFolder())
258 {
259 dynamic_cast<Folder&>(*resource).ListContent(files, subfolders);
260 return true;
261 }
262 else
263 {
264 return false;
265 }
266 }
267
268 virtual bool GetFile(std::string& content /* out */,
269 std::string& mime /* out */,
270 std::string& dateTime /* out */,
271 const std::vector<std::string>& path)
272 {
273 boost::mutex::scoped_lock lock(mutex_);
274
275 Resource* resource = root_->LookupPath(path);
276 if (resource != NULL &&
277 !resource->IsFolder())
278 {
279 const File& file = dynamic_cast<const File&>(*resource);
280 content = file.GetContent();
281 mime = ""; // Let the Orthanc core autodetect the MIME type
282 dateTime = boost::posix_time::to_iso_string(file.GetDateTime());
283 return true;
284 }
285 else
286 {
287 return false;
288 }
289 }
290
291 virtual bool StoreFile(const std::vector<std::string>& path,
292 const void* data,
293 size_t size)
294 {
295 boost::mutex::scoped_lock lock(mutex_);
296
297 Resource* parent = root_->LookupPath(GetParentPath(path));
298 if (parent != NULL &&
299 parent->IsFolder())
300 {
301 dynamic_cast<Folder&>(*parent).StoreFile(path.back(), new File(data, size));
302 return true;
303 }
304 else
305 {
306 return false;
307 }
308 }
309
310 virtual bool CreateFolder(const std::vector<std::string>& path)
311 {
312 boost::mutex::scoped_lock lock(mutex_);
313
314 Resource* parent = root_->LookupPath(GetParentPath(path));
315 if (parent != NULL &&
316 parent->IsFolder())
317 {
318 dynamic_cast<Folder&>(*parent).CreateSubfolder(path.back());
319 return true;
320 }
321 else
322 {
323 return false;
324 }
325 }
326
327 virtual bool DeleteItem(const std::vector<std::string>& path)
328 {
329 boost::mutex::scoped_lock lock(mutex_);
330
331 Resource* parent = root_->LookupPath(GetParentPath(path));
332 if (parent != NULL &&
333 parent->IsFolder())
334 {
335 dynamic_cast<Folder&>(*parent).DeleteItem(path.back());
336 return true;
337 }
338 else
339 {
340 return false;
341 }
342 }
343 };
344
345
346
347 extern "C"
348 {
349 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
350 {
351 OrthancPlugins::SetGlobalContext(c);
352 OrthancPluginLogWarning(c, "WebDAV plugin is initializing");
353
354 /* Check the version of the Orthanc core */
355 if (OrthancPluginCheckVersion(c) == 0)
356 {
357 char info[1024];
358 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
359 c->orthancVersion,
360 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
361 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
362 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
363 OrthancPluginLogError(c, info);
364 return -1;
365 }
366
367 static WebDavFilesystem filesystem;
368 OrthancPlugins::IWebDavCollection::Register("/webdav-plugin", filesystem);
369
370 return 0;
371 }
372
373
374 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
375 {
376 OrthancPluginLogWarning(OrthancPlugins::GetGlobalContext(), "WebDAV plugin is finalizing");
377 }
378
379
380 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
381 {
382 return "webdav-sample";
383 }
384
385
386 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
387 {
388 return "0.0";
389 }
390 }