Browse Source

Add FilePath.CopyToAsync, rename MoveToDirectoryAsync

pull/165/head
Ionite 1 year ago
parent
commit
8a46a1f52e
No known key found for this signature in database
  1. 20
      StabilityMatrix.Core/Models/FileInterfaces/FilePath.cs

20
StabilityMatrix.Core/Models/FileInterfaces/FilePath.cs

@ -118,9 +118,9 @@ public class FilePath : FileSystemPath, IPathObject
/// <summary> /// <summary>
/// Move the file to a directory. /// Move the file to a directory.
/// </summary> /// </summary>
public async Task<FilePath> MoveToAsync(DirectoryPath directory) public async Task<FilePath> MoveToDirectoryAsync(DirectoryPath directory)
{ {
await Task.Run(() => Info.MoveTo(directory.FullPath)); await Task.Run(() => Info.MoveTo(directory.FullPath)).ConfigureAwait(false);
// Return the new path // Return the new path
return directory.JoinFile(this); return directory.JoinFile(this);
} }
@ -130,7 +130,7 @@ public class FilePath : FileSystemPath, IPathObject
/// </summary> /// </summary>
public async Task<FilePath> MoveToAsync(FilePath destinationFile) public async Task<FilePath> MoveToAsync(FilePath destinationFile)
{ {
await Task.Run(() => Info.MoveTo(destinationFile.FullPath)); await Task.Run(() => Info.MoveTo(destinationFile.FullPath)).ConfigureAwait(false);
// Return the new path // Return the new path
return destinationFile; return destinationFile;
} }
@ -145,6 +145,20 @@ public class FilePath : FileSystemPath, IPathObject
return destinationFile; return destinationFile;
} }
/// <summary>
/// Copy the file to a target path asynchronously.
/// </summary>
public async Task<FilePath> CopyToAsync(FilePath destinationFile, bool overwrite = false)
{
await using var sourceStream = Info.OpenRead();
await using var destinationStream = destinationFile.Info.OpenWrite();
await sourceStream.CopyToAsync(destinationStream).ConfigureAwait(false);
// Return the new path
return destinationFile;
}
// Implicit conversions to and from string // Implicit conversions to and from string
public static implicit operator string(FilePath path) => path.FullPath; public static implicit operator string(FilePath path) => path.FullPath;
public static implicit operator FilePath(string path) => new(path); public static implicit operator FilePath(string path) => new(path);

Loading…
Cancel
Save