changeset 702:7592a48e97e4

delete custom attachment
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Feb 2014 17:28:17 +0100
parents f9052558eada
children fd86dfe8f584
files OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 3 files changed, 63 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi.cpp	Mon Feb 10 14:30:56 2014 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Tue Feb 11 17:28:17 2014 +0100
@@ -2028,13 +2028,36 @@
 
     const void* data = call.GetPutBody().size() ? &call.GetPutBody()[0] : NULL;
 
-    if (context.AddAttachment(publicId, StringToContentType(name), data, call.GetPutBody().size()))
+    FileContentType contentType = StringToContentType(name);
+    if (contentType >= FileContentType_StartUser &&  // It is forbidden to modify internal attachments
+        contentType <= FileContentType_EndUser &&
+        context.AddAttachment(publicId, StringToContentType(name), data, call.GetPutBody().size()))
     {
       call.GetOutput().AnswerBuffer("{}", "application/json");
     }
   }
 
 
+  static void DeleteAttachment(RestApi::DeleteCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    CheckValidResourceType(call);
+
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string name = call.GetUriComponent("name", "");
+    FileContentType contentType = StringToContentType(name);
+
+    if (contentType >= FileContentType_StartUser &&
+        contentType <= FileContentType_EndUser)
+    {
+      // It is forbidden to delete internal attachments
+      context.GetIndex().DeleteAttachment(publicId, contentType);
+      call.GetOutput().AnswerBuffer("{}", "application/json");
+    }
+  }
+
+
+
 
   // Registration of the various REST handlers --------------------------------
 
@@ -2127,6 +2150,7 @@
     Register("/{resourceType}/{id}/metadata/{name}", SetMetadata);
 
     Register("/{resourceType}/{id}/attachments", ListAttachments);
+    Register("/{resourceType}/{id}/attachments/{name}", DeleteAttachment);
     Register("/{resourceType}/{id}/attachments/{name}", GetAttachmentOperations);
     Register("/{resourceType}/{id}/attachments/{name}/compressed-data", GetAttachmentData<0>);
     Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5);
--- a/OrthancServer/ServerIndex.cpp	Mon Feb 10 14:30:56 2014 +0100
+++ b/OrthancServer/ServerIndex.cpp	Tue Feb 11 17:28:17 2014 +0100
@@ -1339,22 +1339,22 @@
 
       ResourceType thisType = db_->GetResourceType(resource);
 
+      std::list<FileContentType> f;
+      db_->ListAvailableAttachments(f, resource);
+
+      for (std::list<FileContentType>::const_iterator
+             it = f.begin(); it != f.end(); ++it)
+      {
+        FileInfo attachment;
+        if (db_->LookupAttachment(attachment, resource, *it))
+        {
+          compressedSize += attachment.GetCompressedSize();
+          uncompressedSize += attachment.GetUncompressedSize();
+        }
+      }
+
       if (thisType == ResourceType_Instance)
       {
-        std::list<FileContentType> f;
-        db_->ListAvailableAttachments(f, resource);
-
-        for (std::list<FileContentType>::const_iterator
-               it = f.begin(); it != f.end(); ++it)
-        {
-          FileInfo attachment;
-          if (db_->LookupAttachment(attachment, resource, *it))
-          {
-            compressedSize += attachment.GetCompressedSize();
-            uncompressedSize += attachment.GetUncompressedSize();
-          }
-        }
-
         countInstances++;
       }
       else
@@ -1618,4 +1618,26 @@
     return StoreStatus_Success;
   }
 
+
+  void ServerIndex::DeleteAttachment(const std::string& publicId,
+                                     FileContentType type)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    listener_->Reset();
+
+    Transaction t(*this);
+
+    ResourceType rtype;
+    int64_t id;
+    if (!db_->LookupResource(publicId, id, rtype))
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    db_->DeleteAttachment(id, type);
+
+    t.Commit(0);
+  }
+
+
 }
--- a/OrthancServer/ServerIndex.h	Mon Feb 10 14:30:56 2014 +0100
+++ b/OrthancServer/ServerIndex.h	Tue Feb 11 17:28:17 2014 +0100
@@ -221,5 +221,7 @@
     StoreStatus AddAttachment(const FileInfo& attachment,
                               const std::string& publicId);
 
+    void DeleteAttachment(const std::string& publicId,
+                          FileContentType type);
   };
 }