Skip to content

Commit

Permalink
add folder copy
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Apr 4, 2024
1 parent 70eb824 commit fe6b200
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 18 deletions.
1 change: 1 addition & 0 deletions starsky/starsky.foundation.storage/Interfaces/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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);
}
}
}

/// <summary>
/// Move file on real filesystem
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Move a file
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
40 changes: 22 additions & 18 deletions starsky/starsky.foundation.writemeta/Services/ExifToolDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -84,7 +85,8 @@ public async Task<bool> DownloadExifTool(bool isWindows, int minimumSize = 30)
return false;
}

CreateDirectoryDependenciesFolderIfNotExists();
new CreateFolderIfNotExists(_logger, _appSettings)
.CreateDirectoryDependenciesTempFolderIfNotExists();

if ( isWindows &&
( !_hostFileSystemStorage.ExistFile(ExeExifToolWindowsFullFilePath()) ||
Expand Down Expand Up @@ -116,14 +118,6 @@ public async Task<bool> 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<KeyValuePair<bool, string>?> DownloadCheckSums()
{
Expand Down Expand Up @@ -175,7 +169,10 @@ private string ExeExifToolUnixFullFilePath()
internal async Task<bool> DownloadForUnix(string matchExifToolForUnixName,
IEnumerable<string> 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");
Expand All @@ -199,24 +196,31 @@ internal async Task<bool> 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
{
Expand Down
5 changes: 5 additions & 0 deletions starsky/starskytest/FakeMocks/FakeIStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fe6b200

Please sign in to comment.