# HG changeset patch
# User Alain Mazy <am@osimis.io>
# Date 1696947078 -7200
# Node ID 407bd022b0cf11050e5bd6820b0e448a1f08a995
# Parent  37a4b8e2577f7ad314009d21c515d38d0894788a
in /move-storage: now detecting if file should be moved or not

diff -r 37a4b8e2577f -r 407bd022b0cf Aws/AwsS3StoragePlugin.cpp
--- a/Aws/AwsS3StoragePlugin.cpp	Tue Oct 10 15:40:36 2023 +0200
+++ b/Aws/AwsS3StoragePlugin.cpp	Tue Oct 10 16:11:18 2023 +0200
@@ -62,6 +62,7 @@
   virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
+  virtual bool HasFileExists() {return false;};
 };
 
 
diff -r 37a4b8e2577f -r 407bd022b0cf Azure/AzureBlobStoragePlugin.cpp
--- a/Azure/AzureBlobStoragePlugin.cpp	Tue Oct 10 15:40:36 2023 +0200
+++ b/Azure/AzureBlobStoragePlugin.cpp	Tue Oct 10 16:11:18 2023 +0200
@@ -46,6 +46,7 @@
   virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
+  virtual bool HasFileExists() {return false;};
 };
 
 
diff -r 37a4b8e2577f -r 407bd022b0cf Common/FileSystemStorage.cpp
--- a/Common/FileSystemStorage.cpp	Tue Oct 10 15:40:36 2023 +0200
+++ b/Common/FileSystemStorage.cpp	Tue Oct 10 16:11:18 2023 +0200
@@ -146,4 +146,12 @@
 
 }
 
+bool FileSystemStoragePlugin::FileExists(const std::string& uuid, OrthancPluginContentType type, bool encryptionEnabled)
+{
+  namespace fs = boost::filesystem;
 
+  fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_);
+
+  return Orthanc::SystemToolbox::IsExistingFile(path.string());
+}
+
diff -r 37a4b8e2577f -r 407bd022b0cf Common/FileSystemStorage.h
--- a/Common/FileSystemStorage.h	Tue Oct 10 15:40:36 2023 +0200
+++ b/Common/FileSystemStorage.h	Tue Oct 10 16:11:18 2023 +0200
@@ -70,4 +70,7 @@
   virtual IStorage::IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual IStorage::IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
+
+  virtual bool HasFileExists() {return true;};
+  virtual bool FileExists(const std::string& uuid, OrthancPluginContentType type, bool encryptionEnabled);
 };
\ No newline at end of file
diff -r 37a4b8e2577f -r 407bd022b0cf Common/IStorage.h
--- a/Common/IStorage.h	Tue Oct 10 15:40:36 2023 +0200
+++ b/Common/IStorage.h	Tue Oct 10 16:11:18 2023 +0200
@@ -80,4 +80,7 @@
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) = 0;  // returns true only if 100% sure that the file has been deleted, false otherwise
 
   const std::string& GetNameForLogs() {return nameForLogs_;}
+
+  virtual bool HasFileExists() = 0;
+  virtual bool FileExists(const std::string& uuid, OrthancPluginContentType type, bool encryptionEnabled) {return false;}
 };
diff -r 37a4b8e2577f -r 407bd022b0cf Common/MoveStorageJob.cpp
--- a/Common/MoveStorageJob.cpp	Tue Oct 10 15:40:36 2023 +0200
+++ b/Common/MoveStorageJob.cpp	Tue Oct 10 16:11:18 2023 +0200
@@ -67,7 +67,21 @@
   // read from source storage
   try
   {
-    OrthancPlugins::LogInfo("Move attachment: " + sourceStorage->GetNameForLogs() + ": reading attachment " + std::string(uuid) + " of type " + boost::lexical_cast<std::string>(type));
+    if (sourceStorage->HasFileExists() && !sourceStorage->FileExists(uuid, static_cast<OrthancPluginContentType>(type), cryptoEnabled))
+    {
+      OrthancPlugins::LogInfo("Move attachment: " + sourceStorage->GetNameForLogs() + " " + std::string(uuid) + " of type " + boost::lexical_cast<std::string>(type) + ", skipping, file is not on the source anymore");
+      return true;
+    }
+    else if (targetStorage->HasFileExists() && targetStorage->FileExists(uuid, static_cast<OrthancPluginContentType>(type), cryptoEnabled))
+    {
+      OrthancPlugins::LogInfo("Move attachment: " + targetStorage->GetNameForLogs() + " " + std::string(uuid) + " of type " + boost::lexical_cast<std::string>(type) + ", skipping, file already on the target");
+      return true;
+    }
+    else
+    {
+      OrthancPlugins::LogInfo("Move attachment: " + sourceStorage->GetNameForLogs() + ": reading attachment " + std::string(uuid) + " of type " + boost::lexical_cast<std::string>(type));
+    }
+
     std::unique_ptr<IStorage::IReader> reader(sourceStorage->GetReaderForObject(uuid.c_str(), static_cast<OrthancPluginContentType>(type), cryptoEnabled));
 
     size_t fileSize = reader->GetSize();
@@ -97,7 +111,7 @@
     }
   }
 
-  // everything went well so fare, we can delete from source storage
+  // everything went well so far, we can delete from source storage
   if (buffer.size() > 0)
   {
     try
diff -r 37a4b8e2577f -r 407bd022b0cf Google/GoogleStoragePlugin.cpp
--- a/Google/GoogleStoragePlugin.cpp	Tue Oct 10 15:40:36 2023 +0200
+++ b/Google/GoogleStoragePlugin.cpp	Tue Oct 10 16:11:18 2023 +0200
@@ -45,6 +45,7 @@
   virtual IWriter* GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual IReader* GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
   virtual void DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled);
+  virtual bool HasFileExists() {return false;};
 };
 
 
diff -r 37a4b8e2577f -r 407bd022b0cf NEWS
--- a/NEWS	Tue Oct 10 15:40:36 2023 +0200
+++ b/NEWS	Tue Oct 10 16:11:18 2023 +0200
@@ -9,6 +9,7 @@
     especially for large files.
   * now using the latest AWS SDK C++ (1.11.178).
 
+* in /move-storage: now detecting if file should be moved or not.
 
 2023-07-20 - v 2.2.0
 ====================