diff --git a/starsky/starsky.foundation.storage/Interfaces/IStorage.cs b/starsky/starsky.foundation.storage/Interfaces/IStorage.cs index cfca155725..f4c4c85066 100644 --- a/starsky/starsky.foundation.storage/Interfaces/IStorage.cs +++ b/starsky/starsky.foundation.storage/Interfaces/IStorage.cs @@ -12,6 +12,7 @@ public interface IStorage bool ExistFolder(string path); FolderOrFileModel.FolderOrFileTypeList IsFolderOrFile(string path); void FolderMove(string fromPath, string toPath); + void FolderCopy(string fromPath, string toPath); bool FileMove(string fromPath, string toPath); void FileCopy(string fromPath, string toPath); bool FileDelete(string path); diff --git a/starsky/starsky.foundation.storage/Storage/StorageHostFullPathFilesystem.cs b/starsky/starsky.foundation.storage/Storage/StorageHostFullPathFilesystem.cs index ad92bf4f00..ad5d79fa4a 100644 --- a/starsky/starsky.foundation.storage/Storage/StorageHostFullPathFilesystem.cs +++ b/starsky/starsky.foundation.storage/Storage/StorageHostFullPathFilesystem.cs @@ -320,6 +320,39 @@ public void FolderMove(string fromPath, string toPath) Directory.Move(fromPath, toPath); } + public void FolderCopy(string fromPath, string toPath) + { + var dirs = new Stack(); + dirs.Push(fromPath); + + while (dirs.Count > 0) + { + var currentDir = dirs.Pop(); + var destinationSubDirectory = currentDir.Replace(fromPath, toPath); + + if (!Directory.Exists(destinationSubDirectory)) + { + Directory.CreateDirectory(destinationSubDirectory); + } + + var files = Directory.GetFiles(currentDir); + + foreach (var file in files) + { + var fileName = Path.GetFileName(file); + var destinationFile = Path.Combine(destinationSubDirectory, fileName); + File.Copy(file, destinationFile); + } + + var subdirectories = Directory.GetDirectories(currentDir); + + foreach (var subDirectory in subdirectories) + { + dirs.Push(subDirectory); + } + } + } + /// /// Move file on real filesystem /// diff --git a/starsky/starsky.foundation.storage/Storage/StorageSubPathFilesystem.cs b/starsky/starsky.foundation.storage/Storage/StorageSubPathFilesystem.cs index 695bc37f91..c282542464 100644 --- a/starsky/starsky.foundation.storage/Storage/StorageSubPathFilesystem.cs +++ b/starsky/starsky.foundation.storage/Storage/StorageSubPathFilesystem.cs @@ -101,6 +101,14 @@ public void FolderMove(string fromPath, string toPath) toFileFullPath); } + public void FolderCopy(string fromPath, string toPath) + { + var inputFileFullPath = _appSettings.DatabasePathToFilePath(fromPath); + var toFileFullPath = _appSettings.DatabasePathToFilePath(toPath); + new StorageHostFullPathFilesystem(_logger).FolderCopy(inputFileFullPath, + toFileFullPath); + } + /// /// Move a file /// diff --git a/starsky/starsky.foundation.storage/Storage/StorageThumbnailFilesystem.cs b/starsky/starsky.foundation.storage/Storage/StorageThumbnailFilesystem.cs index 27fac215a1..1e3bff458d 100644 --- a/starsky/starsky.foundation.storage/Storage/StorageThumbnailFilesystem.cs +++ b/starsky/starsky.foundation.storage/Storage/StorageThumbnailFilesystem.cs @@ -69,6 +69,11 @@ public void FolderMove(string fromPath, string toPath) throw new System.NotImplementedException(); } + public void FolderCopy(string fromPath, string toPath) + { + throw new NotImplementedException(); + } + public bool FileMove(string fromPath, string toPath) { var oldThumbPath = CombinePath(fromPath); diff --git a/starsky/starsky.foundation.writemeta/Helpers/CreateFolderIfNotExists.cs b/starsky/starsky.foundation.writemeta/Helpers/CreateFolderIfNotExists.cs new file mode 100644 index 0000000000..b8f767e316 --- /dev/null +++ b/starsky/starsky.foundation.writemeta/Helpers/CreateFolderIfNotExists.cs @@ -0,0 +1,51 @@ +using starsky.foundation.platform.Interfaces; +using starsky.foundation.platform.Models; +using starsky.foundation.storage.Storage; + +namespace starsky.foundation.writemeta.Helpers; + +public class CreateFolderIfNotExists +{ + private readonly StorageHostFullPathFilesystem _hostFileSystemStorage; + private readonly IWebLogger _logger; + private readonly AppSettings _appSettings; + + public CreateFolderIfNotExists(IWebLogger logger, AppSettings appSettings) + { + _hostFileSystemStorage = new StorageHostFullPathFilesystem(logger); + _logger = logger; + _appSettings = appSettings; + } + + internal void CreateDirectoryDependenciesTempFolderIfNotExists() + { + CreateDirectoryDependenciesFolderIfNotExists(); + CreateDirectoryTempFolderIfNotExists(); + } + + private void CreateDirectoryDependenciesFolderIfNotExists() + { + if ( _hostFileSystemStorage.ExistFolder(_appSettings + .DependenciesFolder) ) + { + return; + } + + _logger.LogInformation("[DownloadExifTool] Create Directory: " + + _appSettings.DependenciesFolder); + _hostFileSystemStorage.CreateDirectory(_appSettings.DependenciesFolder); + } + + private void CreateDirectoryTempFolderIfNotExists() + { + if ( _hostFileSystemStorage.ExistFolder(_appSettings + .TempFolder) ) + { + return; + } + + _logger.LogInformation("[DownloadExifTool] Create Directory: " + + _appSettings.TempFolder); + _hostFileSystemStorage.CreateDirectory(_appSettings.TempFolder); + } +} diff --git a/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs b/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs index 45f40cc2d3..8f19c71420 100644 --- a/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs +++ b/starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs @@ -17,6 +17,7 @@ using starsky.foundation.storage.ArchiveFormats; using starsky.foundation.storage.Interfaces; using starsky.foundation.storage.Storage; +using starsky.foundation.writemeta.Helpers; using starsky.foundation.writemeta.Interfaces; [assembly: InternalsVisibleTo("starskytest")] @@ -84,7 +85,8 @@ public async Task DownloadExifTool(bool isWindows, int minimumSize = 30) return false; } - CreateDirectoryDependenciesFolderIfNotExists(); + new CreateFolderIfNotExists(_logger, _appSettings) + .CreateDirectoryDependenciesTempFolderIfNotExists(); if ( isWindows && ( !_hostFileSystemStorage.ExistFile(ExeExifToolWindowsFullFilePath()) || @@ -116,14 +118,6 @@ public async Task DownloadExifTool(bool isWindows, int minimumSize = 30) return await RunChmodOnExifToolUnixExe(); } - private void CreateDirectoryDependenciesFolderIfNotExists() - { - if ( _hostFileSystemStorage.ExistFolder(_appSettings - .DependenciesFolder) ) return; - _logger.LogInformation("[DownloadExifTool] Create Directory: " + - _appSettings.DependenciesFolder); - _hostFileSystemStorage.CreateDirectory(_appSettings.DependenciesFolder); - } internal async Task?> DownloadCheckSums() { @@ -175,7 +169,10 @@ private string ExeExifToolUnixFullFilePath() internal async Task DownloadForUnix(string matchExifToolForUnixName, IEnumerable getChecksumsFromTextFile, bool downloadFromMirror = false) { - if ( _hostFileSystemStorage.ExistFile(ExeExifToolUnixFullFilePath()) ) return true; + if ( _hostFileSystemStorage.ExistFile(ExeExifToolUnixFullFilePath()) ) + { + return true; + } var tarGzArchiveFullFilePath = Path.Combine(_appSettings.TempFolder, "exiftool.tar.gz"); @@ -199,24 +196,31 @@ internal async Task DownloadForUnix(string matchExifToolForUnixName, await new TarBal(_hostFileSystemStorage).ExtractTarGz( _hostFileSystemStorage.ReadStream(tarGzArchiveFullFilePath), - _appSettings.DependenciesFolder, CancellationToken.None); + _appSettings.TempFolder, CancellationToken.None); var imageExifToolVersionFolder = _hostFileSystemStorage - .GetDirectories(_appSettings.DependenciesFolder) + .GetDirectories(_appSettings.TempFolder) .FirstOrDefault(p => - p.StartsWith(Path.Combine(_appSettings.DependenciesFolder, "Image-ExifTool-"))); + p.StartsWith(Path.Combine(_appSettings.TempFolder, "Image-ExifTool-"))); if ( imageExifToolVersionFolder != null ) { - var exifToolUnixFolderFullFilePath = - Path.Combine(_appSettings.DependenciesFolder, "exiftool-unix"); - if ( _hostFileSystemStorage.ExistFolder(exifToolUnixFolderFullFilePath) ) + var exifToolUnixFolderFullFilePathTempFolder = + Path.Combine(_appSettings.TempFolder, "exiftool-unix"); + + if ( _hostFileSystemStorage.ExistFolder(exifToolUnixFolderFullFilePathTempFolder) ) { _hostFileSystemStorage.FolderDelete( - exifToolUnixFolderFullFilePath); + exifToolUnixFolderFullFilePathTempFolder); } _hostFileSystemStorage.FolderMove(imageExifToolVersionFolder, - exifToolUnixFolderFullFilePath); + exifToolUnixFolderFullFilePathTempFolder); + + var exifToolUnixFolderFullFilePath = + Path.Combine(_appSettings.DependenciesFolder, "exiftool-unix"); + + _hostFileSystemStorage.FileCopy(imageExifToolVersionFolder, + exifToolUnixFolderFullFilePathTempFolder); } else { diff --git a/starsky/starskytest/FakeMocks/FakeIStorage.cs b/starsky/starskytest/FakeMocks/FakeIStorage.cs index 398d5f77ad..55bec3f6f5 100644 --- a/starsky/starskytest/FakeMocks/FakeIStorage.cs +++ b/starsky/starskytest/FakeMocks/FakeIStorage.cs @@ -130,6 +130,11 @@ public void FolderMove(string fromPath, string toPath) _outputSubPathFolders[indexOfFolders] = toPath; } + public void FolderCopy(string fromPath, string toPath) + { + throw new NotImplementedException(); + } + public bool FileMove(string fromPath, string toPath) { var existOldFile = ExistFile(fromPath); diff --git a/starsky/starskytest/starsky.foundation.storage/Storage/StorageHostFullPathFilesystemTest.cs b/starsky/starskytest/starsky.foundation.storage/Storage/StorageHostFullPathFilesystemTest.cs index d5f7c31867..c4164519ac 100644 --- a/starsky/starskytest/starsky.foundation.storage/Storage/StorageHostFullPathFilesystemTest.cs +++ b/starsky/starskytest/starsky.foundation.storage/Storage/StorageHostFullPathFilesystemTest.cs @@ -273,6 +273,68 @@ public void IsFileReady_HostService() Assert.IsFalse(hostStorage.ExistFile(filePathNewFile)); } + + [TestMethod] + public void MoveFolder() + { + var hostStorage = new StorageHostFullPathFilesystem(new FakeIWebLogger()); + var beforeDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "28934283492349_before"); + var afterDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "28934283492349_after"); + hostStorage.FolderDelete(afterDirectoryFullPath); + + hostStorage.CreateDirectory(beforeDirectoryFullPath); + hostStorage.FolderMove(beforeDirectoryFullPath, afterDirectoryFullPath); + + Assert.IsFalse(hostStorage.ExistFolder(beforeDirectoryFullPath)); + Assert.IsTrue(hostStorage.ExistFolder(afterDirectoryFullPath)); + + hostStorage.FolderDelete(beforeDirectoryFullPath); + hostStorage.FolderDelete(afterDirectoryFullPath); + } + + [TestMethod] + public void CopyFolder_FlatFolder() + { + var hostStorage = new StorageHostFullPathFilesystem(new FakeIWebLogger()); + var beforeDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "453984583495_before"); + var afterDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "453984583495_after"); + hostStorage.FolderDelete(afterDirectoryFullPath); + + hostStorage.CreateDirectory(beforeDirectoryFullPath); + hostStorage.FolderCopy(beforeDirectoryFullPath, afterDirectoryFullPath); + + Assert.IsTrue(hostStorage.ExistFolder(beforeDirectoryFullPath)); + Assert.IsTrue(hostStorage.ExistFolder(afterDirectoryFullPath)); + + hostStorage.FolderDelete(beforeDirectoryFullPath); + hostStorage.FolderDelete(afterDirectoryFullPath); + } + + [TestMethod] + public async Task CopyFolder_ChildItems() + { + var hostStorage = new StorageHostFullPathFilesystem(new FakeIWebLogger()); + var beforeDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "9057678234_before"); + var childFullPath = Path.Combine(beforeDirectoryFullPath, "child"); + var childFullPathFile = Path.Combine(childFullPath, "child.test"); + + var afterDirectoryFullPath = Path.Combine(new CreateAnImage().BasePath, "9057678234_after"); + hostStorage.FolderDelete(afterDirectoryFullPath); + + hostStorage.CreateDirectory(beforeDirectoryFullPath); + hostStorage.CreateDirectory(childFullPath); + await hostStorage.WriteStreamAsync(new MemoryStream(new byte[1]), childFullPathFile); + + hostStorage.FolderCopy(beforeDirectoryFullPath, afterDirectoryFullPath); + + Assert.IsTrue(hostStorage.ExistFolder(beforeDirectoryFullPath)); + Assert.IsTrue(hostStorage.ExistFolder(afterDirectoryFullPath)); + Assert.IsTrue(hostStorage.ExistFile(childFullPathFile.Replace("_before", "_after"))); + Assert.IsTrue(hostStorage.ExistFile(childFullPathFile)); + + hostStorage.FolderDelete(beforeDirectoryFullPath); + hostStorage.FolderDelete(afterDirectoryFullPath); + } [TestMethod] public void GetDirectoryRecursive_NotFound()