Skip to content

Commit

Permalink
feat(Clients)!: Added remote instructions (#25)
Browse files Browse the repository at this point in the history
* docs(IClient.cs): Correction of path descriptions

* feat(IClient.cs): Added remote instructions method

* feat(Clients): Added remote instructions
  • Loading branch information
FRFlo authored Mar 12, 2024
1 parent 4b073e0 commit 6c4ce2b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Copy/Clients/Exchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public void PutFile(string path, Stream stream)
throw new NotImplementedException();
}

public void MoveFile(string sourcePath, string destinationPath)
{
throw new NotImplementedException();
}

public void CopyFile(string sourcePath, string destinationPath)
{
throw new NotImplementedException();
}

public void DeleteFile(string path)
{
throw new NotImplementedException();
Expand Down
36 changes: 36 additions & 0 deletions Copy/Clients/FTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ public void PutFile(string path, Stream stream)
FtpClient.UploadStream(stream, path, FtpRemoteExists.Overwrite);
}

public void MoveFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!FtpClient.DirectoryExists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
FtpClient.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

FtpClient.MoveFile(sourcePath, destinationPath, FtpRemoteExists.Overwrite);
}

public void CopyFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!FtpClient.DirectoryExists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
FtpClient.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

FtpClient.TransferFile(sourcePath, FtpClient, destinationPath, existsMode: FtpRemoteExists.Overwrite);
}

public void DeleteFile(string path)
{
string directory = Path.GetDirectoryName(path) ?? throw new ArgumentNullException($"Impossible to get directory from {path}");
Expand Down
36 changes: 36 additions & 0 deletions Copy/Clients/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,42 @@ public void PutFile(string path, Stream stream)
stream.CopyTo(fileStream);
}

public void MoveFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!Directory.Exists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
Directory.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

File.Move(sourcePath, destinationPath);
}

public void CopyFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!Directory.Exists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
Directory.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

File.Copy(sourcePath, destinationPath, true);
}

public void DeleteFile(string path)
{
string directory = Path.GetDirectoryName(path) ?? throw new ArgumentNullException($"Impossible to get directory from {path}");
Expand Down
36 changes: 36 additions & 0 deletions Copy/Clients/SFTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@ public void PutFile(string path, Stream stream)
SftpClient.UploadFile(stream, path, true);
}

public void MoveFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!SftpClient.Exists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
SftpClient.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

SftpClient.RenameFile(sourcePath, destinationPath);
}

public void CopyFile(string sourcePath, string destinationPath)
{
string directory = Path.GetDirectoryName(destinationPath) ?? throw new ArgumentNullException($"Impossible to get directory from {destinationPath}");
if (!SftpClient.Exists(directory))
{
Logger.Warn($"Directory {directory} does not exist, creating");
SftpClient.CreateDirectory(directory);
}
if (!DoFileExist(sourcePath))
{
Logger.Error($"File {sourcePath} does not exist");
throw new FileNotFoundException($"File {sourcePath} does not exist");
}
if (DoFileExist(destinationPath)) Logger.Warn($"File {destinationPath} already exists, overwriting");

SftpClient.SynchronizeDirectories(Path.GetDirectoryName(sourcePath), directory, Path.GetFileName(sourcePath));
}

public void DeleteFile(string path)
{
string directory = Path.GetDirectoryName(path) ?? throw new ArgumentNullException($"Impossible to get directory from {path}");
Expand Down
18 changes: 15 additions & 3 deletions Copy/Types/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,31 @@ internal interface IClient : IDisposable
/// <summary>
/// Get a file.
/// </summary>
/// <param name="path">Path to the directory.</param>
/// <param name="path">Path to the file.</param>
/// <returns>Stream of the file.</returns>
Stream GetFile(string path);
/// <summary>
/// Put a file.
/// </summary>
/// <param name="path">Path to the directory.</param>
/// <param name="path">Path to the file.</param>
/// <param name="stream">Stream of the file.</param>
void PutFile(string path, Stream stream);
/// <summary>
/// Move a file.
/// </summary>
/// <param name="sourcePath">Path to the source file.</param>
/// <param name="destinationPath">Path to the destination file.</param>
void MoveFile(string sourcePath, string destinationPath);
/// <summary>
/// Copy a file.
/// </summary>
/// <param name="sourcePath">Path to the source file.</param>
/// <param name="destinationPath">Path to the destination file.</param>
void CopyFile(string sourcePath, string destinationPath);
/// <summary>
/// Delete a file.
/// </summary>
/// <param name="path">Path to the directory.</param>
/// <param name="path">Path to the file.</param>
void DeleteFile(string path);
}
}

0 comments on commit 6c4ce2b

Please sign in to comment.