# HG changeset patch # User Sebastien Jodogne # Date 1392136097 -3600 # Node ID 7592a48e97e4cd404fd1e17c69a67d0c2cf7d587 # Parent f9052558eada847b5e8654a04d32ca828acc0ef6 delete custom attachment diff -r f9052558eada -r 7592a48e97e4 OrthancServer/OrthancRestApi.cpp --- 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); diff -r f9052558eada -r 7592a48e97e4 OrthancServer/ServerIndex.cpp --- 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 f; + db_->ListAvailableAttachments(f, resource); + + for (std::list::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 f; - db_->ListAvailableAttachments(f, resource); - - for (std::list::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); + } + + } diff -r f9052558eada -r 7592a48e97e4 OrthancServer/ServerIndex.h --- 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); }; }