Skip to content

A Swiss Army knife for developers.

Updated IFileStorage API to return FileStream

Etienne Baudouxc7a098df7c94
diff --git a/src/app/dev/DevToys.Api/Core/IFileStorage.cs b/src/app/dev/DevToys.Api/Core/IFileStorage.cs
index b03225627..52e072c3f 100644
--- a/src/app/dev/DevToys.Api/Core/IFileStorage.cs
+++ b/src/app/dev/DevToys.Api/Core/IFileStorage.cs
@@ -12,13 +12,12 @@ public interface IFileStorage
/// </summary>
string AppCacheDirectory { get; }
- // TODO: There's a problem with this API. It's not possible to know what file type the user selected. The file type can impact the data written into the file (i.e PNG vs JPEG).
/// <summary>
/// Prompt the user to select a location to save a file, and decide of the file name.
/// </summary>
/// <param name="fileTypes">The list of file types the user can choose. For example, ".txt". Use "*" for any file type.</param>
/// <returns>If succeeded, returns a write-only stream corresponding to the file the user selected, otherwise, returns null.</returns>
- ValueTask<Stream?> PickSaveFileAsync(params string[] fileTypes);
+ ValueTask<FileStream?> PickSaveFileAsync(params string[] fileTypes);
/// <summary>
/// Prompt the user to select a file to open.
@@ -56,7 +55,7 @@ public interface IFileStorage
/// </summary>
/// <param name="relativeOrAbsoluteFilePath">The path to the file to read.</param>
/// <returns>Returns a read-only stream if the file exist and can be read, otherwise, raise an exception.</returns>
- Stream OpenReadFile(string relativeOrAbsoluteFilePath);
+ FileStream OpenReadFile(string relativeOrAbsoluteFilePath);
/// <summary>
/// Tries to open the given <paramref name="relativeOrAbsoluteFilePath"/> with write access rights. The file will be created if it doesn't exist.
@@ -65,7 +64,7 @@ public interface IFileStorage
/// <param name="relativeOrAbsoluteFilePath">The path to the file to write.</param>
/// <param name="replaceIfExist">If true and that the file indicated by <paramref name="relativeOrAbsoluteFilePath"/> already exist, overwrite it. Otherwise, open it without replacing it.</param>
/// <returns>Returns a write-only stream.</returns>
- Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist);
+ FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist);
/// <summary>
/// Creates a new temporary file in <see cref="AppCacheDirectory"/> that will be deleted when the app stops, or the next time is starts.
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
index 6fb2df010..969fac9e4 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
@@ -25,7 +25,7 @@ internal sealed class FileStorage : IFileStorage
return File.Exists(relativeOrAbsoluteFilePath);
}
- public Stream OpenReadFile(string relativeOrAbsoluteFilePath)
+ public FileStream OpenReadFile(string relativeOrAbsoluteFilePath)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -40,7 +40,7 @@ internal sealed class FileStorage : IFileStorage
return new FileStream(relativeOrAbsoluteFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, SandboxedFileReader.BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
}
- public Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
+ public FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -61,17 +61,18 @@ internal sealed class FileStorage : IFileStorage
return File.OpenWrite(relativeOrAbsoluteFilePath);
}
- public ValueTask<Stream?> PickSaveFileAsync(params string[] fileTypes)
+ public ValueTask<FileStream?> PickSaveFileAsync(params string[] fileTypes)
{
+ // TODO: Limit input to the indicated file types.
Console.WriteLine(CliStrings.PromptSaveFile);
string? filePath = Console.ReadLine();
if (string.IsNullOrWhiteSpace(filePath))
{
- return new ValueTask<Stream?>(Task.FromResult<Stream?>(null));
+ return new ValueTask<FileStream?>(Task.FromResult<FileStream?>(null));
}
- return new ValueTask<Stream?>(File.OpenWrite(filePath!));
+ return new ValueTask<FileStream?>(File.OpenWrite(filePath!));
}
public ValueTask<SandboxedFileReader?> PickOpenFileAsync(params string[] fileTypes)
diff --git a/src/app/dev/platforms/desktop/DevToys.Linux/Core/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.Linux/Core/FileStorage.cs
index 3d86826a7..8240f755d 100644
--- a/src/app/dev/platforms/desktop/DevToys.Linux/Core/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Linux/Core/FileStorage.cs
@@ -30,7 +30,7 @@ internal sealed class FileStorage : GObject.Object, IFileStorage
return File.Exists(relativeOrAbsoluteFilePath);
}
- public Stream OpenReadFile(string relativeOrAbsoluteFilePath)
+ public FileStream OpenReadFile(string relativeOrAbsoluteFilePath)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -45,7 +45,7 @@ internal sealed class FileStorage : GObject.Object, IFileStorage
return new FileStream(relativeOrAbsoluteFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, SandboxedFileReader.BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
}
- public Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
+ public FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -135,7 +135,7 @@ internal sealed class FileStorage : GObject.Object, IFileStorage
return PickOpenFileInternalAsync(fileTypes, allowMultiple: true);
}
- public async ValueTask<Stream?> PickSaveFileAsync(params string[] fileTypes)
+ public async ValueTask<FileStream?> PickSaveFileAsync(params string[] fileTypes)
{
Guard.IsNotNull(MainWindow);
@@ -157,7 +157,7 @@ internal sealed class FileStorage : GObject.Object, IFileStorage
fileChooser.AddFilter(filters[i]);
}
- var taskCompletionSource = new TaskCompletionSource<Stream?>();
+ var taskCompletionSource = new TaskCompletionSource<FileStream?>();
fileChooser.OnResponse += (_, e) =>
{
// Handle the result of the window.
diff --git a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/FileStorage.cs
index f2418a088..9ced90abd 100644
--- a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/FileStorage.cs
@@ -21,7 +21,7 @@ internal sealed class FileStorage : IFileStorage
return File.Exists(relativeOrAbsoluteFilePath);
}
- public Stream OpenReadFile(string relativeOrAbsoluteFilePath)
+ public FileStream OpenReadFile(string relativeOrAbsoluteFilePath)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -37,7 +37,7 @@ internal sealed class FileStorage : IFileStorage
SandboxedFileReader.BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
}
- public Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
+ public FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -58,7 +58,7 @@ internal sealed class FileStorage : IFileStorage
return File.OpenWrite(relativeOrAbsoluteFilePath);
}
- public async ValueTask<Stream?> PickSaveFileAsync(string[] fileTypes)
+ public async ValueTask<FileStream?> PickSaveFileAsync(string[] fileTypes)
{
return await ThreadHelper.RunOnUIThreadAsync(() =>
{
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/Core/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.Windows/Core/FileStorage.cs
index 9d5ceff9a..6855b8bc4 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/Core/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/Core/FileStorage.cs
@@ -24,7 +24,7 @@ internal sealed class FileStorage : IFileStorage
return File.Exists(relativeOrAbsoluteFilePath);
}
- public Stream OpenReadFile(string relativeOrAbsoluteFilePath)
+ public FileStream OpenReadFile(string relativeOrAbsoluteFilePath)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -39,7 +39,7 @@ internal sealed class FileStorage : IFileStorage
return new FileStream(relativeOrAbsoluteFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, SandboxedFileReader.BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
}
- public Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
+ public FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
{
if (!Path.IsPathRooted(relativeOrAbsoluteFilePath))
{
@@ -60,7 +60,7 @@ internal sealed class FileStorage : IFileStorage
return File.OpenWrite(relativeOrAbsoluteFilePath);
}
- public async ValueTask<Stream?> PickSaveFileAsync(params string[] fileTypes)
+ public async ValueTask<FileStream?> PickSaveFileAsync(params string[] fileTypes)
{
return await ThreadHelper.RunOnUIThreadAsync(() =>
{
@@ -84,7 +84,7 @@ internal sealed class FileStorage : IFileStorage
fileStream.Close();
}
- return saveFileDialog.OpenFile();
+ return saveFileDialog.OpenFile() as FileStream;
}
return null;
diff --git a/src/app/tests/DevToys.UnitTests/Mocks/MockIFileStorage.cs b/src/app/tests/DevToys.UnitTests/Mocks/MockIFileStorage.cs
index 2c41cccb9..1421dc8f9 100644
--- a/src/app/tests/DevToys.UnitTests/Mocks/MockIFileStorage.cs
+++ b/src/app/tests/DevToys.UnitTests/Mocks/MockIFileStorage.cs
@@ -18,17 +18,17 @@ internal class MockIFileStorage : IFileStorage
return FileExistsResult;
}
- public Stream OpenReadFile(string relativeOrAbsoluteFilePath)
+ public FileStream OpenReadFile(string relativeOrAbsoluteFilePath)
{
return new FileStream(relativeOrAbsoluteFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
}
- public Stream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
+ public FileStream OpenWriteFile(string relativeOrAbsoluteFilePath, bool replaceIfExist)
{
throw new NotImplementedException();
}
- public ValueTask<Stream> PickSaveFileAsync(params string[] fileTypes)
+ public ValueTask<FileStream> PickSaveFileAsync(params string[] fileTypes)
{
// TODO: prompt the user to type in the console a relative or absolute file path that has one of the file types indicated.
throw new NotImplementedException();