Skip to content

Commit d81bb18

Browse files
Merge pull request #60 from KristofferStrube/feature/prepare-for-version-4
Initial preparation for next major version.
2 parents 9b8793d + 05d3905 commit d81bb18

58 files changed

Lines changed: 1053 additions & 978 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.EditorConfig

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[*]
2+
# All files
3+
dotnet_style_qualification_for_field = false
4+
dotnet_style_qualification_for_property = false
5+
dotnet_style_qualification_for_method = false
6+
dotnet_style_qualification_for_event = false
7+
dotnet_diagnostic.IDE0003.severity = warning
8+
dotnet_style_predefined_type_for_locals_parameters_members = true
9+
dotnet_style_predefined_type_for_member_access = true
10+
dotnet_diagnostic.IDE0049.severity = suggestion
11+
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async
12+
dotnet_diagnostic.IDE0036.severity = error
13+
dotnet_style_require_accessibility_modifiers = always
14+
dotnet_diagnostic.IDE0040.severity = warning
15+
dotnet_style_readonly_field = true
16+
dotnet_diagnostic.IDE0044.severity = error
17+
csharp_prefer_static_local_function = true
18+
dotnet_diagnostic.IDE0062.severity = warning
19+
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
20+
dotnet_diagnostic.IDE0047.severity = warning
21+
dotnet_diagnostic.IDE0048.severity = warning
22+
dotnet_diagnostic.IDE0010.severity = error
23+
dotnet_style_object_initializer = true
24+
dotnet_diagnostic.IDE0017.severity = suggestion
25+
csharp_style_inlined_variable_declaration = true
26+
dotnet_diagnostic.IDE0018.severity = suggestion
27+
dotnet_style_collection_initializer = true
28+
dotnet_diagnostic.IDE0028.severity = warning
29+
dotnet_style_prefer_auto_properties = true
30+
dotnet_diagnostic.IDE0032.severity = suggestion
31+
dotnet_style_explicit_tuple_names = true
32+
dotnet_diagnostic.IDE0033.severity = warning
33+
csharp_prefer_simple_default_expression = false
34+
dotnet_diagnostic.IDE0034.severity = warning
35+
dotnet_style_prefer_inferred_tuple_names = true
36+
dotnet_style_prefer_inferred_anonymous_type_member_names = true
37+
dotnet_diagnostic.IDE0037.severity = suggestion
38+
csharp_style_prefer_local_over_anonymous_function = true
39+
dotnet_diagnostic.IDE0039.severity = warning
40+
csharp_style_deconstructed_variable_declaration = true
41+
dotnet_diagnostic.IDE0042.severity = suggestion
42+
dotnet_style_prefer_conditional_expression_over_assignment = true
43+
dotnet_diagnostic.IDE0045.severity = warning
44+
dotnet_style_prefer_conditional_expression_over_return = true
45+
dotnet_diagnostic.IDE0046.severity = warning
46+
dotnet_style_prefer_compound_assignment = true
47+
dotnet_diagnostic.IDE0054.severity = warning
48+
dotnet_diagnostic.IDE0074.severity = warning
49+
csharp_style_prefer_index_operator = true
50+
dotnet_diagnostic.IDE0056.severity = warning
51+
csharp_style_prefer_range_operator = true
52+
dotnet_diagnostic.IDE0057.severity = warning
53+
dotnet_diagnostic.IDE0070.severity = error
54+
dotnet_style_prefer_simplified_interpolation = true
55+
dotnet_diagnostic.IDE0071.severity = warning
56+
dotnet_diagnostic.IDE0072.severity = error
57+
dotnet_style_prefer_simplified_boolean_expressions = true
58+
dotnet_diagnostic.IDE0075.severity = warning
59+
dotnet_diagnostic.IDE0082.severity = error
60+
csharp_style_implicit_object_creation_when_type_is_apparent = true
61+
dotnet_diagnostic.IDE0090.severity = error
62+
dotnet_diagnostic.IDE0180.severity = warning
63+
csharp_style_namespace_declarations = file_scoped
64+
dotnet_diagnostic.IDE0160.severity = error
65+
dotnet_diagnostic.IDE0161.severity = error
66+
csharp_style_throw_expression = true
67+
dotnet_diagnostic.IDE0016.severity = warning
68+
dotnet_style_coalesce_expression = true
69+
dotnet_diagnostic.IDE0029.severity = warning
70+
dotnet_diagnostic.IDE0030.severity = warning
71+
dotnet_style_null_propagation = true
72+
dotnet_diagnostic.IDE0031.severity = warning
73+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
74+
dotnet_diagnostic.IDE0041.severity = warning
75+
csharp_style_prefer_null_check_over_type_check = true
76+
dotnet_diagnostic.IDE0150.severity = warning
77+
csharp_style_conditional_delegate_call = false
78+
dotnet_diagnostic.IDE1005.severity = warning
79+
csharp_style_var_for_built_in_types = false
80+
csharp_style_var_when_type_is_apparent = true
81+
csharp_style_var_elsewhere = false
82+
dotnet_diagnostic.IDE0007.severity = warning
83+
dotnet_diagnostic.IDE0008.severity = warning
84+
dotnet_diagnostic.IDE0001.severity = error
85+
dotnet_diagnostic.IDE0002.severity = error
86+
dotnet_diagnostic.IDE0004.severity = error
87+
dotnet_diagnostic.IDE0005.severity = error
88+
dotnet_diagnostic.IDE0035.severity = warning
89+
dotnet_diagnostic.IDE0051.severity = warning
90+
dotnet_diagnostic.IDE0052.severity = warning
91+
csharp_style_unused_value_expression_statement_preference = discard_variable
92+
dotnet_diagnostic.IDE0058.severity = silent
93+
csharp_style_unused_value_assignment_preference = discard_variable
94+
dotnet_diagnostic.IDE0059.severity = silent
95+
# Ignore namespace mismatch as this is a library
96+
dotnet_diagnostic.IDE0130.severity = none
97+
# We don't want to adopt primary constructors yet
98+
dotnet_diagnostic.IDE0290.severity = none

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Then you can use `IFileSystemAccessService` to open one of the three dialogs ava
7171
FileSystemFileHandle? fileHandle = null;
7272
try
7373
{
74-
OpenFilePickerOptionsStartInWellKnownDirectory options = new()
74+
OpenFilePickerOptions options = new()
7575
{
7676
Multiple = false,
7777
StartIn = WellKnownDirectory.Downloads
@@ -97,6 +97,71 @@ Then you can use `IFileSystemAccessService` to open one of the three dialogs ava
9797
}
9898
```
9999

100+
# Upgrade to version 4
101+
Version 4 of this library made several breaking changes. To make the transition from version 3 or earlier to this version easier, I have made a small guide below for how you can upgrade.
102+
## File/Directory picker options
103+
We changed the method signatures for opening the directory and file picker dialogs to make them simpler to use.
104+
105+
To migrate replace the following:
106+
- `OpenFilePickerOptionsStartInWellKnownDirectory` or `OpenFilePickerOptionsStartInFileSystemHandle` with `OpenFilePickerOptions`.
107+
- `SaveFilePickerOptionsStartInWellKnownDirectory` or `SaveFilePickerOptionsStartInFileSystemHandle` with `SaveFilePickerOptions`.
108+
- `DirectoryPickerOptionsStartInWellKnownDirectory` or `DirectoryPickerOptionsStartInFileSystemHandle` with `DirectoryPickerOptions`.
109+
110+
As an example, if you had the following before:
111+
```csharp
112+
OpenFilePickerOptionsStartInWellKnownDirectory options = new()
113+
{
114+
Multiple = false,
115+
StartIn = WellKnownDirectory.Downloads
116+
};
117+
var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
118+
```
119+
Update it as follows:
120+
```csharp
121+
OpenFilePickerOptions options = new()
122+
{
123+
Multiple = false,
124+
StartIn = WellKnownDirectory.Downloads
125+
};
126+
var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
127+
```
128+
129+
## Removed `FileSystemAccessOptions`
130+
We removed the `FileSystemAccessOptions` parameter and all methods that previously accepted it as it duplicated functionality that could be achived in other ways.
131+
132+
Instead of using them, you need to configure an [importmap](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type/importmap) if you want to define custom paths for loading the helper modules from this library.
133+
134+
Blazor also has a native [ImportMap component](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-10.0#importmap-component) that that plays well with fingerprinted resources.
135+
136+
## Blazor.FileSystem removed `FileSystemDirectoryHandle.ValuesAsync`
137+
The `ValuesAsync` method was removed from Blazor.FileSystem, and instead you should now use the `ValuesAsync` extension method available from Blazor.WebIDL.
138+
139+
This change was made to make easier to handle memory safely.
140+
141+
So if you had the following before:
142+
```csharp
143+
var values = await directoryHandle.ValuesAsync();
144+
for (int i = 0; i < values.Count(); i++)
145+
{
146+
var value = values[i];
147+
string name = await value.GetNameAsync();
148+
FileSystemHandleKind kind = await value.GetKindAsync();
149+
Console.WriteLine($"'{name}' is a {kind}");
150+
await value.DisposeAsync();
151+
}
152+
```
153+
Then you should change it to the following:
154+
```csharp
155+
await using var valuesIterator = await directoryHandle.ValuesAsync(disposePreviousValueWhenMovingToNextValue: true);
156+
await foreach (FileSystemHandle value in valuesIterator)
157+
{
158+
string name = await value.GetNameAsync();
159+
FileSystemHandleKind kind = await value.GetKindAsync();
160+
Console.WriteLine($"'{name}' is a {kind}");
161+
}
162+
```
163+
In the above example, we pass `true` for the `disposePreviousValueWhenMovingToNextValue` parameter, which is also the default. This means that it will dispose of each handle once it iterates past it. If you need the handles after iterating, you can pass `false` for this parameter instead.
164+
100165
# Issues
101166
Feel free to open issues on the repository if you find any errors with the package or have wishes for features.
102167

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
@@ -12,13 +12,8 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<ProjectReference Include="..\..\..\Blazor.Streams\src\KristofferStrube.Blazor.Streams\KristofferStrube.Blazor.Streams.csproj" />
1516
<ProjectReference Include="..\..\src\KristofferStrube.Blazor.FileSystemAccess\KristofferStrube.Blazor.FileSystemAccess.csproj" />
1617
</ItemGroup>
1718

18-
<ItemGroup>
19-
<Content Update="Shared\NavMenu.razor">
20-
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
21-
</Content>
22-
</ItemGroup>
23-
2419
</Project>

samples/KristofferStrube.Blazor.FileSystemAccess.ServerExample/Pages/AppendOnly.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ else
3131
{
3232
try
3333
{
34-
var options = new OpenFilePickerOptionsStartInWellKnownDirectory() { Multiple = false, StartIn = WellKnownDirectory.Downloads };
34+
OpenFilePickerOptions options = new() { Multiple = false, StartIn = WellKnownDirectory.Downloads };
3535
var fileHandles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
3636
fileHandle = fileHandles.Single();
3737
}

samples/KristofferStrube.Blazor.FileSystemAccess.ServerExample/Pages/IndexedDB.razor

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ In this sample we store the references to files that we have previously opened i
4242
StoredFileHandles = (await Task.WhenAll(Enumerable
4343
.Range(0, length)
4444
.Select(async i => {
45-
var fileHandle = FileSystemFileHandle.Create(JSRuntime, await entries.InvokeAsync<IJSObjectReference>("at", i));
45+
var fileHandle = await FileSystemFileHandle.CreateAsync(JSRuntime, await entries.InvokeAsync<IJSObjectReference>("at", i));
4646
return (false, fileHandle, await fileHandle.GetNameAsync());
4747
})
4848
)).ToList();
@@ -53,10 +53,7 @@ In this sample we store the references to files that we have previously opened i
5353
{
5454
try
5555
{
56-
var options = new OpenFilePickerOptionsStartInWellKnownDirectory()
57-
{
58-
Multiple = false
59-
};
56+
OpenFilePickerOptions options = new() { Multiple = false };
6057
var handles = await FileSystemAccessService.ShowOpenFilePickerAsync(options);
6158
FileHandle = handles.Single();
6259
}

samples/KristofferStrube.Blazor.FileSystemAccess.ServerExample/Pages/InputFileExample.razor

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,62 @@
11
@page "/InputFileExample"
22
@inject IFileSystemAccessService FileSystemAccessService
3+
@inject IJSRuntime JSRuntime
34

45
<PageTitle>File System Access - InputFile Example</PageTitle>
56

67
We can read the bytes from a <code>IBrowserFile</code> from the native <code>InputFile</code> component and write these to the <code>FileSystemWritableFileStream</code>.
78
<br />
89
<br />
9-
@if (readFile is not null)
10+
11+
@if (done)
1012
{
11-
<h3>Currently streaming...</h3>
13+
<h3>Done streaming file</h3>
14+
return;
1215
}
13-
else if (SaveFileHandle is not null)
16+
17+
@if (SaveFileHandle is null)
1418
{
15-
<text>Choose file to Stream From</text>
16-
<br />
17-
<InputFile OnChange="OnChange" />
19+
<button class="btn btn-primary" @onclick=Save>Choose File to Stream To</button>
20+
return;
1821
}
19-
else
22+
23+
@if (readFile is not null)
2024
{
21-
<button class="btn btn-primary" @onclick=Save>Choose File to Stream To</button>
25+
<h3>Currently streaming...</h3>
26+
return;
2227
}
2328

29+
<text>Choose file to Stream From</text>
30+
<br />
31+
<InputFile OnChange="OnChange" />
32+
2433
@code {
2534
FileSystemFileHandle? SaveFileHandle;
2635
IBrowserFile? readFile;
36+
bool done = false;
2737

2838
public async Task OnChange(InputFileChangeEventArgs args)
2939
{
30-
if (SaveFileHandle is not null)
40+
if (SaveFileHandle is null) return;
41+
42+
readFile = args.File;
43+
44+
using MemoryStream tempBuffer = new();
45+
46+
using (var stream = readFile.OpenReadStream(maxAllowedSize: 10000000000))
3147
{
32-
readFile = args.File;
33-
34-
using var stream = readFile.OpenReadStream(maxAllowedSize: 10000000000);
35-
await using var writable = await SaveFileHandle.CreateWritableAsync(new() { KeepExistingData = false });
36-
await stream.CopyToAsync(writable);
37-
SaveFileHandle = null;
38-
readFile = null;
39-
Console.WriteLine("Done streaming!");
48+
await stream.CopyToAsync(tempBuffer);
4049
}
50+
51+
await using var writable = await SaveFileHandle.CreateWritableAsync(new() { KeepExistingData = false });
52+
53+
tempBuffer.Position = 0;
54+
await tempBuffer.CopyToAsync(writable);
55+
56+
SaveFileHandle = null;
57+
readFile = null;
58+
Console.WriteLine("Done streaming!");
59+
done = true;
4160
}
4261

4362
public async Task Save()

0 commit comments

Comments
 (0)