Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed default FileAccess.Read parameter for IFile.OpenStreamAsync #49

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Extensions/CopyAndMoveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static async Task<IChildFile> CreateCopyOfAsync<T>(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);
Expand Down
35 changes: 35 additions & 0 deletions src/Extensions/FileOpenExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace OwlCore.Storage.Extensions;

/// <summary>
/// Extension methods for <see cref="IFile"/>.
/// </summary>
public static partial class FileExtensions
{
/// <summary>
/// Opens the file for reading.
/// </summary>
/// <param name="file">The file to open.</param>
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
/// <returns>A task containing the requested stream.</returns>
public static Task<Stream> OpenReadAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Read, cancellationToken);

/// <summary>
/// Opens the file for writing.
/// </summary>
/// <param name="file">The file to open.</param>
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
/// <returns>A task containing the requested stream.</returns>
public static Task<Stream> OpenWriteAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.Write, cancellationToken);

/// <summary>
/// Opens the file for reading and writing.
/// </summary>
/// <param name="file">The file to open.</param>
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
/// <returns>A task containing the requested stream.</returns>
public static Task<Stream> OpenReadWriteAsync(this IFile file, CancellationToken cancellationToken = default) => file.OpenStreamAsync(FileAccess.ReadWrite, cancellationToken);
}
2 changes: 1 addition & 1 deletion src/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface IFile : IStorable
/// <param name="accessMode">A <see cref="FileAccess"/> value that specifies the operations that can be performed on the file.</param>
/// <param name="cancellationToken">A token that can be used to cancel the ongoing operation.</param>
/// <returns>A stream that provides access to this file, with the specified <paramref name="accessMode"/>.</returns>
Task<Stream> OpenStreamAsync(FileAccess accessMode = FileAccess.Read, CancellationToken cancellationToken = default);
Task<Stream> OpenStreamAsync(FileAccess accessMode, CancellationToken cancellationToken = default);
}
2 changes: 1 addition & 1 deletion src/Memory/MemoryFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<IChildFile> 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)
Expand Down
10 changes: 10 additions & 0 deletions src/OwlCore.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>
--- 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.
Expand Down
Loading