Skip to content

Commit 692f808

Browse files
Added implicit converters for BlobPart and fixed saturating creator for FileReader to register event handlers.
1 parent 55cf9eb commit 692f808

File tree

10 files changed

+61
-50
lines changed

10 files changed

+61
-50
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ An example could be to create an instance of a `Blob` that contains the text `"H
4444
Blob blob = await Blob.CreateAsync(
4545
JSRuntime,
4646
blobParts: new BlobPart[] {
47-
new("Hello "),
48-
new(new byte[] { 0X57, 0X6f, 0X72, 0X6c, 0X64, 0X21 })
47+
"Hello ",
48+
new byte[] { 0X57, 0X6f, 0X72, 0X6c, 0X64, 0X21 }
4949
},
5050
options: new() { Type = "text/plain" }
5151
);
@@ -60,8 +60,8 @@ All creator methods take an `IJSRuntime` instance as the first parameter. The ab
6060
BlobInProcess blob = await BlobInProcess.CreateAsync(
6161
JSRuntime,
6262
blobParts: new BlobPart[] {
63-
new("Hello "),
64-
new(new byte[] { 0X57, 0X6f, 0X72, 0X6c, 0X64, 0X21 })
63+
"Hello ",
64+
new byte[] { 0X57, 0X6f, 0X72, 0X6c, 0X64, 0X21 }
6565
},
6666
options: new() { Type = "text/plain" }
6767
);

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/FileReaderSample.razor

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
2525
@code {
2626
string log = "";
2727
private string imageUrl = "";
28-
private Blob blob;
28+
private Blob? blob;
2929

3030
protected override async Task OnInitializedAsync()
3131
{
3232
var imageBytes = await HttpClient.GetByteArrayAsync($"images/mountain.jpg");
3333
blob = await Blob.CreateAsync(
3434
JSRuntime,
35-
blobParts: new BlobPart[] { new(imageBytes) },
35+
blobParts: new BlobPart[] { imageBytes },
3636
options: new() { Type = "image/png" }
3737
);
3838
}
@@ -66,7 +66,7 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
6666
imageUrl = "data:image/png;base64," + Convert.ToBase64String(await fileReader.GetResultAsByteArrayAsync() ?? new byte[0]);
6767
await GetProgressAsync(e, "OnLoadEnd");
6868
};
69-
await fileReader.ReadAsArrayBufferAsync(blob);
69+
await fileReader.ReadAsArrayBufferAsync(blob!);
7070
}
7171

7272
public async Task ReadAsArrayBufferInProcessAsync()
@@ -83,7 +83,7 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
8383
imageUrl = "data:image/png;base64," + Convert.ToBase64String(fileReader.ResultAsByteArray ?? new byte[0]);
8484
GetProgress(e, "OnLoadEnd");
8585
};
86-
fileReader.ReadAsArrayBuffer(blob);
86+
fileReader.ReadAsArrayBuffer(blob!);
8787
}
8888

8989
public async Task ReadAsBinaryStringAsync()
@@ -101,7 +101,7 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
101101
imageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
102102
await GetProgressAsync(e, "OnLoadEnd");
103103
};
104-
await fileReader.ReadAsBinaryStringAsync(blob);
104+
await fileReader.ReadAsBinaryStringAsync(blob!);
105105
}
106106

107107
public void ReadAsText()
@@ -125,6 +125,6 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
125125
imageUrl = await fileReader.GetResultAsStringAsync() ?? "";
126126
await GetProgressAsync(e, "OnLoadEnd");
127127
};
128-
await fileReader.ReadAsDataURLAsync(blob);
128+
await fileReader.ReadAsDataURLAsync(blob!);
129129
}
130130
}

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ content type: @file?.Type
3131
var imageBytes = await HttpClient.GetByteArrayAsync($"images/{imageName}");
3232
file = await FileInProcess.CreateAsync(
3333
JSRuntime,
34-
fileBits: new BlobPart[] { new(imageBytes) },
34+
fileBits: new BlobPart[] { imageBytes },
3535
fileName: imageName,
3636
options: new() { Type = "image/png", LastModified = DateTime.Now }
3737
);

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/Slice.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Here we can see how we can slice a <code>Blob</code>. We construct a <code>Blob<
3030
{
3131
var blob = await BlobInProcess.CreateAsync(
3232
JSRuntime,
33-
blobParts: new BlobPart[] { new(Enumerable.Range(0, 50).Select(i => (byte)i).ToArray()) },
33+
blobParts: new BlobPart[] { Enumerable.Range(0, 50).Select(i => (byte)i).ToArray() },
3434
options: new() { Type = "text/plain" }
3535
);
3636

src/KristofferStrube.Blazor.FileAPI/Blob.InProcess.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public static async Task<BlobInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInP
3333
public static new async Task<BlobInProcess> CreateAsync(IJSRuntime jSRuntime, IList<BlobPart>? blobParts = null, BlobPropertyBag? options = null)
3434
{
3535
IJSInProcessObjectReference inProcessHelper = await jSRuntime.GetInProcessHelperAsync();
36-
object?[]? jsBlobParts = blobParts?.Select<BlobPart, object?>(blobPart => blobPart.type switch
36+
object?[]? jsBlobParts = blobParts?.Select<BlobPart, object?>(blobPart => blobPart.Part switch
3737
{
38-
BlobPartType.BufferSource => blobPart.byteArrayPart,
39-
BlobPartType.Blob => blobPart.blobPart?.JSReference,
40-
_ => blobPart.stringPart
38+
byte[] part => part,
39+
Blob part => part.JSReference,
40+
_ => blobPart.Part
4141
})
4242
.ToArray();
4343
IJSInProcessObjectReference jSInstance = await inProcessHelper.InvokeAsync<IJSInProcessObjectReference>("constructBlob", jsBlobParts, options);

src/KristofferStrube.Blazor.FileAPI/Blob.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public static Blob Create(IJSRuntime jSRuntime, IJSObjectReference jSReference)
2929
public static async Task<Blob> CreateAsync(IJSRuntime jSRuntime, IList<BlobPart>? blobParts = null, BlobPropertyBag? options = null)
3030
{
3131
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
32-
object?[]? jsBlobParts = blobParts?.Select<BlobPart, object?>(blobPart => blobPart.type switch
32+
object?[]? jsBlobParts = blobParts?.Select<BlobPart, object?>(blobPart => blobPart.Part switch
3333
{
34-
BlobPartType.BufferSource => blobPart.byteArrayPart,
35-
BlobPartType.Blob => blobPart.blobPart?.JSReference,
36-
_ => blobPart.stringPart
34+
byte[] part => part,
35+
Blob part => part.JSReference,
36+
_ => blobPart.Part
3737
})
3838
.ToArray();
3939
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructBlob", jsBlobParts, options);

src/KristofferStrube.Blazor.FileAPI/File.InProcess.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public static async Task<FileInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInP
3434
public static new async Task<FileInProcess> CreateAsync(IJSRuntime jSRuntime, IList<BlobPart> fileBits, string fileName, FilePropertyBag? options = null)
3535
{
3636
IJSInProcessObjectReference inProcessHelper = await jSRuntime.GetInProcessHelperAsync();
37-
object?[]? jsFileBits = fileBits.Select<BlobPart, object?>(blobPart => blobPart.type switch
37+
object?[]? jsFileBits = fileBits.Select<BlobPart, object?>(blobPart => blobPart.Part switch
3838
{
39-
BlobPartType.BufferSource => blobPart.byteArrayPart,
40-
BlobPartType.Blob => blobPart.stringPart,
41-
_ => blobPart.blobPart?.JSReference
39+
byte[] part => part,
40+
Blob part => part.JSReference,
41+
_ => blobPart.Part
4242
})
4343
.ToArray();
4444
IJSInProcessObjectReference jSInstance = await inProcessHelper.InvokeAsync<IJSInProcessObjectReference>("constructFile", jsFileBits, fileName, options);

src/KristofferStrube.Blazor.FileAPI/File.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public class File : Blob
2929
public static async Task<File> CreateAsync(IJSRuntime jSRuntime, IList<BlobPart> fileBits, string fileName, FilePropertyBag? options = null)
3030
{
3131
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
32-
object?[]? jsFileBits = fileBits.Select<BlobPart, object?>(blobPart => blobPart.type switch
32+
object?[]? jsFileBits = fileBits.Select<BlobPart, object?>(blobPart => blobPart.Part switch
3333
{
34-
BlobPartType.BufferSource => blobPart.byteArrayPart,
35-
BlobPartType.Blob => blobPart.stringPart,
36-
_ => blobPart.blobPart?.JSReference
34+
byte[] part => part,
35+
Blob part => part.JSReference,
36+
_ => blobPart.Part
3737
})
3838
.ToArray();
3939
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructFile", jsFileBits, fileName, options);

src/KristofferStrube.Blazor.FileAPI/FileReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public class FileReader : BaseJSWrapper
1414
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
1515
/// <param name="jSReference">A JS reference to an existing <see cref="FileReader"/>.</param>
1616
/// <returns>A wrapper instance for a <see cref="FileReader"/>.</returns>
17-
public static FileReader Create(IJSRuntime jSRuntime, IJSObjectReference jSReference)
17+
public static async Task<FileReader> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference)
1818
{
19-
return new FileReader(jSRuntime, jSReference);
19+
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
20+
FileReader fileReader = new(jSRuntime, jSReference);
21+
await helper.InvokeVoidAsync("registerEventHandlersAsync", DotNetObjectReference.Create(fileReader), jSReference);
22+
return fileReader;
2023
}
2124

2225
/// <summary>
@@ -28,7 +31,7 @@ public static async Task<FileReader> CreateAsync(IJSRuntime jSRuntime)
2831
{
2932
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
3033
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructFileReader");
31-
FileReader fileReader = new FileReader(jSRuntime, jSInstance);
34+
FileReader fileReader = new(jSRuntime, jSInstance);
3235
await helper.InvokeVoidAsync("registerEventHandlersAsync", DotNetObjectReference.Create(fileReader), jSInstance);
3336
return fileReader;
3437
}
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
1-
namespace KristofferStrube.Blazor.FileAPI;
1+
using System.Runtime.Serialization;
2+
3+
namespace KristofferStrube.Blazor.FileAPI;
24

35
/// <summary>
46
/// <see href="https://www.w3.org/TR/FileAPI/#typedefdef-blobpart">BlobPart browser specs</see>
57
/// </summary>
68
public class BlobPart
79
{
8-
internal readonly byte[]? byteArrayPart;
9-
internal readonly Blob? blobPart;
10-
internal readonly string? stringPart;
11-
internal readonly BlobPartType type;
10+
internal readonly object Part;
1211

12+
internal BlobPart(object part)
13+
{
14+
Part = part;
15+
}
16+
[Obsolete("We added implicit converters from byte[] to BlobPart so you can parse it directly without using this constructor first.")]
1317
public BlobPart(byte[] part)
1418
{
15-
byteArrayPart = part;
16-
type = BlobPartType.BufferSource;
19+
Part = part;
1720
}
18-
21+
[Obsolete("We added implicit converters from Blob to BlobPart so you can parse it directly without using this constructor first.")]
1922
public BlobPart(Blob part)
2023
{
21-
blobPart = part;
22-
type = BlobPartType.Blob;
24+
Part = part;
2325
}
24-
26+
[Obsolete("We added implicit converters from string to BlobPart so you can parse it directly without using this constructor first.")]
2527
public BlobPart(string part)
2628
{
27-
stringPart = part;
28-
type = BlobPartType.String;
29+
Part = part;
2930
}
30-
}
3131

32-
internal enum BlobPartType
33-
{
34-
BufferSource,
35-
Blob,
36-
String
32+
33+
public static implicit operator BlobPart(byte[] part)
34+
{
35+
return new((object)part);
36+
}
37+
public static implicit operator BlobPart(Blob part)
38+
{
39+
return new((object)part);
40+
}
41+
public static implicit operator BlobPart(string part)
42+
{
43+
return new((object)part);
44+
}
3745
}

0 commit comments

Comments
 (0)