diff OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp @ 4252:f047e2734655

fix webdav
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 15 Oct 2020 17:13:35 +0200
parents 112951171852
children d9473bd5ed43
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp	Tue Oct 13 15:42:10 2020 +0200
+++ b/OrthancFramework/Sources/HttpServer/IWebDavBucket.cpp	Thu Oct 15 17:13:35 2020 +0200
@@ -50,25 +50,20 @@
 
 namespace Orthanc
 {
-  void IWebDavBucket::Resource::SetNameInternal(const std::string& name)
-  {
-    if (name.find('/') != std::string::npos ||
-        name.find('\\') != std::string::npos ||
-        name.find('\0') != std::string::npos)
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange,
-                             "Bad resource name for WebDAV: " + name);
-    }
-        
-    name_ = name;
-  }
-
-
-  IWebDavBucket::Resource::Resource() :
+  IWebDavBucket::Resource::Resource(const std::string& displayName) :
+    displayName_(displayName),
     hasModificationTime_(false),
     creationTime_(GetNow()),
     modificationTime_(GetNow())
   {
+    if (displayName.empty() ||
+        displayName.find('/') != std::string::npos ||
+        displayName.find('\\') != std::string::npos ||
+        displayName.find('\0') != std::string::npos)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange,
+                             "Bad resource name for WebDAV: " + displayName);
+    }
   }
 
 
@@ -154,17 +149,11 @@
   }
 
   
-  IWebDavBucket::File::File(const std::string& name) :
+  IWebDavBucket::File::File(const std::string& displayName) :
+    Resource(displayName),
     contentLength_(0),
     mime_(MimeType_Binary)
   {
-    if (name.empty())
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange,
-                             "Cannot use an empty filename in WebDAV");          
-    }
-        
-    SetNameInternal(name);
   }
 
   
@@ -172,8 +161,8 @@
                                    const std::string& parentPath) const
   {
     std::string href;
-    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetName());
-    FormatInternal(node, href, GetName(), GetCreationTime(), GetModificationTime());
+    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetDisplayName());
+    FormatInternal(node, href, GetDisplayName(), GetCreationTime(), GetModificationTime());
 
     pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
     prop.append_child("D:resourcetype");
@@ -190,8 +179,8 @@
                                      const std::string& parentPath) const
   {
     std::string href;
-    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetName());
-    FormatInternal(node, href, GetName(), GetCreationTime(), GetModificationTime());
+    Toolbox::UriEncode(href, AddTrailingSlash(parentPath) + GetDisplayName());
+    FormatInternal(node, href, GetDisplayName(), GetCreationTime(), GetModificationTime());
         
     pugi::xml_node prop = node.first_element_by_path("D:propstat/D:prop");
     prop.append_child("D:resourcetype").append_child("D:collection");
@@ -223,6 +212,16 @@
   }
 
 
+  void IWebDavBucket::Collection::ListDisplayNames(std::set<std::string>& target)
+  {
+    for (std::list<Resource*>::iterator it = resources_.begin(); it != resources_.end(); ++it)
+    {
+      assert(*it != NULL);
+      target.insert((*it)->GetDisplayName());
+    }
+  }
+
+
   void IWebDavBucket::Collection::Format(std::string& target,
                                          const std::string& parentPath) const
   {
@@ -234,19 +233,17 @@
     {
       pugi::xml_node self = root.append_child();
 
+      std::vector<std::string> tokens;
+      Toolbox::SplitUriComponents(tokens, parentPath);
+      
       std::string folder;
-      size_t lastSlash = parentPath.rfind('/');
-      if (lastSlash == std::string::npos)
+      if (!tokens.empty())
       {
-        folder = parentPath;
+        folder = tokens.back();
       }
-      else
-      {
-        folder = parentPath.substr(lastSlash + 1);
-      }
-      
+       
       std::string href;
-      Toolbox::UriEncode(href, AddTrailingSlash(parentPath));
+      Toolbox::UriEncode(href, Toolbox::FlattenUri(tokens) + "/");
 
       boost::posix_time::ptime now = GetNow();
       FormatInternal(self, href, folder, now, now);