changeset 5169:8b74aa4f3515

enforce the existence of the patient/study/instance while creating its archive
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 24 Feb 2023 18:07:35 +0100
parents 688b83f915bc
children 3e6dbf4a018d
files NEWS OrthancServer/Sources/OrthancRestApi/OrthancRestArchive.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.cpp OrthancServer/Sources/ServerJobs/ArchiveJob.h
diffstat 4 files changed, 38 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Feb 22 11:10:40 2023 +0100
+++ b/NEWS	Fri Feb 24 18:07:35 2023 +0100
@@ -53,6 +53,8 @@
 
 * Fix decoding of RLE images for which the "Planar Configuration" tag (0028,0006) equals 1
 * Fix issue #212 (Anonymization process transcodes data and loses resource link).
+* Enforce the existence of the patient/study/instance while creating its archive
+
 
 version 1.11.2 (2022-08-30)
 ===========================
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestArchive.cpp	Wed Feb 22 11:10:40 2023 +0100
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestArchive.cpp	Fri Feb 24 18:07:35 2023 +0100
@@ -61,7 +61,7 @@
       }
       else
       {
-        job.AddResource(resources[i].asString());
+        job.AddResource(resources[i].asString(), false, ResourceType_Patient /* dummy value */);
       }
     }
   }
@@ -576,7 +576,8 @@
   }
   
 
-  template <bool IS_MEDIA>
+  template <ResourceType LEVEL,
+            bool IS_MEDIA>
   static void CreateSingleGet(RestApiGetCall& call)
   {
     static const char* const TRANSCODE = "transcode";
@@ -627,7 +628,7 @@
     }
 
     std::unique_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended));
-    job->AddResource(id);
+    job->AddResource(id, true, LEVEL);
 
     if (call.HasArgument(TRANSCODE))
     {
@@ -645,7 +646,8 @@
   }
 
 
-  template <bool IS_MEDIA>
+  template <ResourceType LEVEL,
+            bool IS_MEDIA>
   static void CreateSinglePost(RestApiPostCall& call)
   {
     if (call.IsDocumentation())
@@ -678,7 +680,7 @@
                        priority, loaderThreads, body, false /* by default, not extented */);
       
       std::unique_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended));
-      job->AddResource(id);
+      job->AddResource(id, true, LEVEL);
 
       if (transcode)
       {
@@ -698,18 +700,18 @@
     
   void OrthancRestApi::RegisterArchive()
   {
-    Register("/patients/{id}/archive", CreateSingleGet<false /* ZIP */>);
-    Register("/patients/{id}/archive", CreateSinglePost<false /* ZIP */>);
-    Register("/patients/{id}/media",   CreateSingleGet<true /* media */>);
-    Register("/patients/{id}/media",   CreateSinglePost<true /* media */>);
-    Register("/series/{id}/archive",   CreateSingleGet<false /* ZIP */>);
-    Register("/series/{id}/archive",   CreateSinglePost<false /* ZIP */>);
-    Register("/series/{id}/media",     CreateSingleGet<true /* media */>);
-    Register("/series/{id}/media",     CreateSinglePost<true /* media */>);
-    Register("/studies/{id}/archive",  CreateSingleGet<false /* ZIP */>);
-    Register("/studies/{id}/archive",  CreateSinglePost<false /* ZIP */>);
-    Register("/studies/{id}/media",    CreateSingleGet<true /* media */>);
-    Register("/studies/{id}/media",    CreateSinglePost<true /* media */>);
+    Register("/patients/{id}/archive", CreateSingleGet<ResourceType_Patient, false /* ZIP */>);
+    Register("/patients/{id}/archive", CreateSinglePost<ResourceType_Patient, false /* ZIP */>);
+    Register("/patients/{id}/media",   CreateSingleGet<ResourceType_Patient, true /* media */>);
+    Register("/patients/{id}/media",   CreateSinglePost<ResourceType_Patient, true /* media */>);
+    Register("/series/{id}/archive",   CreateSingleGet<ResourceType_Series, false /* ZIP */>);
+    Register("/series/{id}/archive",   CreateSinglePost<ResourceType_Series, false /* ZIP */>);
+    Register("/series/{id}/media",     CreateSingleGet<ResourceType_Series, true /* media */>);
+    Register("/series/{id}/media",     CreateSinglePost<ResourceType_Series, true /* media */>);
+    Register("/studies/{id}/archive",  CreateSingleGet<ResourceType_Study, false /* ZIP */>);
+    Register("/studies/{id}/archive",  CreateSinglePost<ResourceType_Study, false /* ZIP */>);
+    Register("/studies/{id}/media",    CreateSingleGet<ResourceType_Study, true /* media */>);
+    Register("/studies/{id}/media",    CreateSinglePost<ResourceType_Study, true /* media */>);
 
     Register("/tools/create-archive",
              CreateBatch<false /* ZIP */,  false /* extended makes no sense in ZIP */>);
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Wed Feb 22 11:10:40 2023 +0100
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.cpp	Fri Feb 24 18:07:35 2023 +0100
@@ -1155,7 +1155,9 @@
   }
 
   
-  void ArchiveJob::AddResource(const std::string& publicId)
+  void ArchiveJob::AddResource(const std::string& publicId,
+                               bool mustExist,
+                               ResourceType expectedType)
   {
     if (writer_.get() != NULL)   // Already started
     {
@@ -1163,6 +1165,17 @@
     }
     else
     {
+      if (mustExist)
+      {
+        ResourceType type;
+        if (!context_.GetIndex().LookupResourceType(type, publicId) ||
+            type != expectedType)
+        {
+          throw OrthancException(ErrorCode_InexistentItem,
+                                 "Missing resource while creating an archive: " + publicId);
+        }
+      }
+      
       ResourceIdentifiers resource(context_.GetIndex(), publicId);
       archive_->Add(context_.GetIndex(), resource);
     }
--- a/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Wed Feb 22 11:10:40 2023 +0100
+++ b/OrthancServer/Sources/ServerJobs/ArchiveJob.h	Fri Feb 24 18:07:35 2023 +0100
@@ -89,7 +89,9 @@
       return description_;
     }
 
-    void AddResource(const std::string& publicId);
+    void AddResource(const std::string& publicId,
+                     bool mustExist,
+                     ResourceType expectedType);
 
     void SetTranscode(DicomTransferSyntax transferSyntax);