Skip to content

Commit 6dcbac9

Browse files
Updated CHANGELOG, Added Create method to FileSystemWritableFileStream and applied Code Cleanup.
1 parent c6c977a commit 6dcbac9

14 files changed

Lines changed: 203 additions & 74 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Added `FileSystemAccessOptions` class that can customize the helper script path. By [@datvm](https://github.com/datvm).
10+
- Added overloads for `FileSystemAccessService` and `FileSystemAccessServiceInProcess` methods that takes a `FileSystemAccessOptions`. By [@datvm](https://github.com/datvm).
11+
- Added overloads for creator methods on `FileSystemHandle`, `FileSystemFileHandle`, and `FileSystemDirectoryHandle` that takes a `FileSystemAccessOptions`. By [@datvm](https://github.com/datvm).
12+
- Added `Create` method to `FileSystemWritableFileStream`.
813

914
## [2.0.0] - 2022-11-10
1015
### Added

src/KristofferStrube.Blazor.FileSystemAccess/BaseFileSystemAccessService.cs

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,68 @@ public BaseFileSystemAccessService(IJSRuntime jSRuntime)
2424
/// <param name="openFilePickerOptions"></param>
2525
/// <returns></returns>
2626
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInWellKnownDirectory? openFilePickerOptions)
27-
=> await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable());
27+
{
28+
return await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable());
29+
}
2830

2931
/// <summary>
3032
/// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see>
3133
/// </summary>
3234
/// <param name="openFilePickerOptions"></param>
3335
/// <returns></returns>
3436
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInWellKnownDirectory? openFilePickerOptions, FileSystemAccessOptions fsaOptions)
35-
=> await InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions);
37+
{
38+
return await InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions);
39+
}
3640

3741
/// <summary>
3842
/// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see>
3943
/// </summary>
4044
/// <param name="openFilePickerOptions"></param>
4145
/// <returns></returns>
4246
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInFileSystemHandle? openFilePickerOptions)
43-
=> await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable());
47+
{
48+
return await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable());
49+
}
4450

4551
/// <summary>
4652
/// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see>
4753
/// </summary>
4854
/// <param name="openFilePickerOptions"></param>
4955
/// <returns></returns>
5056
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInFileSystemHandle? openFilePickerOptions, FileSystemAccessOptions fsaOptions)
51-
=> await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions);
57+
{
58+
return await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions);
59+
}
5260

5361
/// <summary>
5462
/// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see>
5563
/// </summary>
5664
/// <returns></returns>
5765
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync()
58-
=> await InternalShowOpenFilePickerAsync(null);
66+
{
67+
return await InternalShowOpenFilePickerAsync(null);
68+
}
5969

6070
/// <summary>
6171
/// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see>
6272
/// </summary>
6373
/// <returns></returns>
6474
public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(FileSystemAccessOptions fsaOptions)
65-
=> await InternalShowOpenFilePickerAsync(null, fsaOptions);
75+
{
76+
return await InternalShowOpenFilePickerAsync(null, fsaOptions);
77+
}
6678

6779
protected async Task<TFsFileHandle[]> InternalShowOpenFilePickerAsync(object? options)
68-
=> await InternalShowOpenFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance);
80+
{
81+
return await InternalShowOpenFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance);
82+
}
83+
6984
protected async Task<TFsFileHandle[]> InternalShowOpenFilePickerAsync(object? options, FileSystemAccessOptions fsaOptions)
7085
{
71-
var helper = await helperTask.Value;
72-
var jSFileHandles = await jSRuntime.InvokeAsync<TObjReference>("window.showOpenFilePicker", options);
73-
var length = await helper.InvokeAsync<int>("size", jSFileHandles);
86+
IJSObjectReference helper = await helperTask.Value;
87+
TObjReference jSFileHandles = await jSRuntime.InvokeAsync<TObjReference>("window.showOpenFilePicker", options);
88+
int length = await helper.InvokeAsync<int>("size", jSFileHandles);
7489

7590
return await Task.WhenAll(
7691
Enumerable
@@ -95,52 +110,66 @@ await jSFileHandles.InvokeAsync<TObjReference>("at", i),
95110
/// <param name="saveFilePickerOptions"></param>
96111
/// <returns></returns>
97112
public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInWellKnownDirectory? saveFilePickerOptions)
98-
=> await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable());
113+
{
114+
return await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable());
115+
}
99116

100117
/// <summary>
101118
/// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see>
102119
/// </summary>
103120
/// <param name="saveFilePickerOptions"></param>
104121
/// <returns></returns>
105122
public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInWellKnownDirectory? saveFilePickerOptions, FileSystemAccessOptions fsaOptions)
106-
=> await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions);
123+
{
124+
return await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions);
125+
}
107126

108127
/// <summary>
109128
/// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see>
110129
/// </summary>
111130
/// <param name="saveFilePickerOptions"></param>
112131
/// <returns></returns>
113132
public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInFileSystemHandle? saveFilePickerOptions)
114-
=> await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable());
133+
{
134+
return await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable());
135+
}
115136

116137
/// <summary>
117138
/// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see>
118139
/// </summary>
119140
/// <param name="saveFilePickerOptions"></param>
120141
/// <returns></returns>
121142
public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInFileSystemHandle? saveFilePickerOptions, FileSystemAccessOptions fsaOptions)
122-
=> await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions);
143+
{
144+
return await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions);
145+
}
123146

124147
/// <summary>
125148
/// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see>
126149
/// </summary>
127150
/// <returns></returns>
128151
public async Task<TFsFileHandle> ShowSaveFilePickerAsync()
129-
=> await InternalShowSaveFilePickerAsync(null);
152+
{
153+
return await InternalShowSaveFilePickerAsync(null);
154+
}
130155

131156
/// <summary>
132157
/// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see>
133158
/// </summary>
134159
/// <returns></returns>
135160
public async Task<TFsFileHandle> ShowSaveFilePickerAsync(FileSystemAccessOptions options)
136-
=> await InternalShowSaveFilePickerAsync(null, options);
161+
{
162+
return await InternalShowSaveFilePickerAsync(null, options);
163+
}
137164

138165
protected async Task<TFsFileHandle> InternalShowSaveFilePickerAsync(object? options)
139-
=> await this.InternalShowSaveFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance);
166+
{
167+
return await this.InternalShowSaveFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance);
168+
}
140169

141170
protected async Task<TFsFileHandle> InternalShowSaveFilePickerAsync(object? options, FileSystemAccessOptions fsaOptions)
142171
{
143-
var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showSaveFilePicker", options);
172+
TObjReference jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showSaveFilePicker", options);
144173
return await this.CreateFileHandleAsync(jSRuntime, jSFileHandle, fsaOptions);
145174
}
146175

@@ -154,54 +183,68 @@ protected async Task<TFsFileHandle> InternalShowSaveFilePickerAsync(object? opti
154183
/// <param name="directoryPickerOptions"></param>
155184
/// <returns></returns>
156185
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInWellKnownDirectory? directoryPickerOptions)
157-
=> await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable());
186+
{
187+
return await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable());
188+
}
158189

159190
/// <summary>
160191
/// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see>
161192
/// </summary>
162193
/// <param name="directoryPickerOptions"></param>
163194
/// <returns></returns>
164195
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInWellKnownDirectory? directoryPickerOptions, FileSystemAccessOptions fasOptions)
165-
=> await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions);
196+
{
197+
return await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions);
198+
}
166199

167200
/// <summary>
168201
/// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see>
169202
/// </summary>
170203
/// <param name="directoryPickerOptions"></param>
171204
/// <returns></returns>
172205
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInFileSystemHandle? directoryPickerOptions)
173-
=> await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable());
206+
{
207+
return await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable());
208+
}
174209

175210
/// <summary>
176211
/// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see>
177212
/// </summary>
178213
/// <param name="directoryPickerOptions"></param>
179214
/// <returns></returns>
180215
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInFileSystemHandle? directoryPickerOptions, FileSystemAccessOptions fasOptions)
181-
=> await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions);
216+
{
217+
return await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions);
218+
}
182219

183220
/// <summary>
184221
/// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see>
185222
/// </summary>
186223
/// <param name="directoryPickerOptions"></param>
187224
/// <returns></returns>
188225
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync()
189-
=> await InternalShowDirectoryPickerAsync(null);
226+
{
227+
return await InternalShowDirectoryPickerAsync(null);
228+
}
190229

191230
/// <summary>
192231
/// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see>
193232
/// </summary>
194233
/// <param name="directoryPickerOptions"></param>
195234
/// <returns></returns>
196235
public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(FileSystemAccessOptions fasOptions)
197-
=> await InternalShowDirectoryPickerAsync(null, fasOptions);
236+
{
237+
return await InternalShowDirectoryPickerAsync(null, fasOptions);
238+
}
198239

199240
protected async Task<TFsDirectoryHandle> InternalShowDirectoryPickerAsync(object? options)
200-
=> await InternalShowDirectoryPickerAsync(options, FileSystemAccessOptions.DefaultInstance);
241+
{
242+
return await InternalShowDirectoryPickerAsync(options, FileSystemAccessOptions.DefaultInstance);
243+
}
201244

202245
protected async Task<TFsDirectoryHandle> InternalShowDirectoryPickerAsync(object? options, FileSystemAccessOptions fasOptions)
203246
{
204-
var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showDirectoryPicker", options);
247+
TObjReference jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showDirectoryPicker", options);
205248
return await this.CreateDirectoryHandleAsync(jSRuntime, jSFileHandle, fasOptions);
206249
}
207250

@@ -214,15 +257,17 @@ protected async Task<TFsDirectoryHandle> InternalShowDirectoryPickerAsync(object
214257
/// </summary>
215258
/// <returns></returns>
216259
public async Task<TFsDirectoryHandle> GetOriginPrivateDirectoryAsync()
217-
=> await this.GetOriginPrivateDirectoryAsync(FileSystemAccessOptions.DefaultInstance);
260+
{
261+
return await this.GetOriginPrivateDirectoryAsync(FileSystemAccessOptions.DefaultInstance);
262+
}
218263

219264
/// <summary>
220265
/// <see href="https://wicg.github.io/file-system-access/#dom-storagemanager-getdirectory">getDirectory() for StorageManager browser specs</see>
221266
/// </summary>
222267
/// <returns></returns>
223268
public async Task<TFsDirectoryHandle> GetOriginPrivateDirectoryAsync(FileSystemAccessOptions fasOptions)
224269
{
225-
var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("navigator.storage.getDirectory");
270+
TObjReference jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("navigator.storage.getDirectory");
226271
return await CreateDirectoryHandleAsync(jSRuntime, jSFileHandle, fasOptions);
227272
}
228273

src/KristofferStrube.Blazor.FileSystemAccess/Extensions/IJSRuntimeExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ namespace KristofferStrube.Blazor.FileSystemAccess.Extensions;
55
internal static class IJSRuntimeExtensions
66
{
77
internal static async Task<IJSObjectReference> GetHelperAsync(this IJSRuntime jSRuntime, FileSystemAccessOptions options)
8-
=> await GetHelperAsync<IJSObjectReference>(jSRuntime, options);
8+
{
9+
return await GetHelperAsync<IJSObjectReference>(jSRuntime, options);
10+
}
911

10-
internal static async Task<IJSInProcessObjectReference> GetInProcessHelperAsync(this IJSRuntime jSRuntime, FileSystemAccessOptions options) =>
11-
await GetHelperAsync<IJSInProcessObjectReference>(jSRuntime, options);
12+
internal static async Task<IJSInProcessObjectReference> GetInProcessHelperAsync(this IJSRuntime jSRuntime, FileSystemAccessOptions options)
13+
{
14+
return await GetHelperAsync<IJSInProcessObjectReference>(jSRuntime, options);
15+
}
1216

13-
static async Task<T> GetHelperAsync<T>(IJSRuntime jSRuntime, FileSystemAccessOptions options) =>
14-
await jSRuntime.InvokeAsync<T>("import", GetScriptPath(options));
17+
private static async Task<T> GetHelperAsync<T>(IJSRuntime jSRuntime, FileSystemAccessOptions options)
18+
{
19+
return await jSRuntime.InvokeAsync<T>("import", GetScriptPath(options));
20+
}
1521

16-
static string GetScriptPath(FileSystemAccessOptions options) => options.FullScriptPath;
22+
private static string GetScriptPath(FileSystemAccessOptions options)
23+
{
24+
return options.FullScriptPath;
25+
}
1726
}

src/KristofferStrube.Blazor.FileSystemAccess/Extensions/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.JSInterop;
3-
using System.Diagnostics;
42

53
namespace KristofferStrube.Blazor.FileSystemAccess;
64

@@ -31,12 +29,12 @@ public static IServiceCollection AddFileSystemAccessServiceInProcess(this IServi
3129
.AddScoped<IFileSystemAccessServiceInProcess, FileSystemAccessServiceInProcess>()
3230
.AddScoped(sp =>
3331
{
34-
var service = sp.GetRequiredService<IFileSystemAccessServiceInProcess>();
32+
IFileSystemAccessServiceInProcess service = sp.GetRequiredService<IFileSystemAccessServiceInProcess>();
3533
return (IFileSystemAccessService)service;
3634
});
3735
}
3836

39-
static void ConfigureFsaOptions(IServiceCollection services, Action<FileSystemAccessOptions>? configure)
37+
private static void ConfigureFsaOptions(IServiceCollection services, Action<FileSystemAccessOptions>? configure)
4038
{
4139
if (configure is null) { return; }
4240

0 commit comments

Comments
 (0)