diff --git a/src/Extensions/CopyAndMoveExtensions.cs b/src/Extensions/CopyAndMoveExtensions.cs index b163df9..226c592 100644 --- a/src/Extensions/CopyAndMoveExtensions.cs +++ b/src/Extensions/CopyAndMoveExtensions.cs @@ -41,7 +41,7 @@ public static async Task CreateCopyOfAsync(this IModifiableFolder } // Open the source file - using var sourceStream = await fileToCopy.OpenStreamAsync(cancellationToken: cancellationToken); + using var sourceStream = await fileToCopy.OpenStreamAsync(FileAccess.Read, cancellationToken: cancellationToken); // Create the destination file var newFile = await destinationFolder.CreateFileAsync(fileToCopy.Name, overwrite, cancellationToken); diff --git a/src/Extensions/FileOpenExtensions.cs b/src/Extensions/FileOpenExtensions.cs new file mode 100644 index 0000000..9c3b551 --- /dev/null +++ b/src/Extensions/FileOpenExtensions.cs @@ -0,0 +1,35 @@ +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace OwlCore.Storage.Extensions; + +/// +/// Extension methods for . +/// +public static partial class FileExtensions +{ + /// + /// Opens the file for reading. + /// + /// The file to open. + /// A token that can be used to cancel the ongoing operation. + /// A task containing the requested stream. + public static Task OpenReadAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Read, cancellationToken); + + /// + /// Opens the file for writing. + /// + /// The file to open. + /// A token that can be used to cancel the ongoing operation. + /// A task containing the requested stream. + public static Task OpenWriteAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Write, cancellationToken); + + /// + /// Opens the file for reading and writing. + /// + /// The file to open. + /// A token that can be used to cancel the ongoing operation. + /// A task containing the requested stream. + public static Task OpenReadWriteAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.ReadWrite, cancellationToken); +} \ No newline at end of file diff --git a/src/IFile.cs b/src/IFile.cs index 7973a91..43384f0 100644 --- a/src/IFile.cs +++ b/src/IFile.cs @@ -15,5 +15,5 @@ public interface IFile : IStorable /// A value that specifies the operations that can be performed on the file. /// A token that can be used to cancel the ongoing operation. /// A stream that provides access to this file, with the specified . - Task OpenStreamAsync(FileAccess accessMode = FileAccess.Read, CancellationToken cancellationToken = default); + Task OpenStreamAsync(FileAccess accessMode, CancellationToken cancellationToken = default); } diff --git a/src/Memory/MemoryFolder.cs b/src/Memory/MemoryFolder.cs index f04bbce..669a7ac 100644 --- a/src/Memory/MemoryFolder.cs +++ b/src/Memory/MemoryFolder.cs @@ -93,7 +93,7 @@ public async Task CreateCopyOfAsync(IFile fileToCopy, bool overwrite { cancellationToken.ThrowIfCancellationRequested(); - using var stream = await fileToCopy.OpenStreamAsync(cancellationToken: cancellationToken); + using var stream = await fileToCopy.OpenStreamAsync(FileAccess.Read, cancellationToken: cancellationToken); cancellationToken.ThrowIfCancellationRequested(); if (stream.CanSeek) diff --git a/src/OwlCore.Storage.csproj b/src/OwlCore.Storage.csproj index b686c1d..00d2a60 100644 --- a/src/OwlCore.Storage.csproj +++ b/src/OwlCore.Storage.csproj @@ -23,6 +23,16 @@ LICENSE.txt logo.png +--- 0.10.0 --- +THIS IS A BREAKING RELEASE +Update asap, and do not use older versions or implementations. + +[Breaking] +The default FileAccess.Read parameter for IFile.OpenStreamAsync has been removed. This is primarily to avoid confusion about why the default returned stream can't be written to, but follows the concept that simply opening a stream implies nothing about read/write preferences for that resource, so no default value should be used. + +[New] +Extensions methods for IFile.OpenReadAsync, IFile.OpenWriteAsync and IFile.OpenReadWriteAsync have been added. + --- 0.9.3 --- [Improvements] The DirectoryInfo for SystemFolder is now exposed as a public Info property.