|
| 1 | +using Microsoft.JSInterop; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | + |
| 4 | +namespace KristofferStrube.Blazor.FileAPI; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// <see href="https://www.w3.org/TR/FileAPI/#dfn-filereader">FileReader browser specs</see> |
| 8 | +/// </summary> |
| 9 | +public class FileReaderInProcess : FileReader |
| 10 | +{ |
| 11 | + public new IJSInProcessObjectReference JSReference; |
| 12 | + protected readonly IJSInProcessObjectReference inProcessHelper; |
| 13 | + /// <summary> |
| 14 | + /// Constructs a wrapper instance for a given JS Instance of a <see cref="FileReader"/>. |
| 15 | + /// </summary> |
| 16 | + /// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param> |
| 17 | + /// <param name="jSReference">A JS reference to an existing <see cref="FileReader"/>.</param> |
| 18 | + /// <returns>A wrapper instance for a <see cref="FileReader"/>.</returns> |
| 19 | + public static async Task<FileReaderInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference) |
| 20 | + { |
| 21 | + IJSInProcessObjectReference inProcesshelper = await jSRuntime.GetInProcessHelperAsync(); |
| 22 | + return new FileReaderInProcess(jSRuntime, inProcesshelper, jSReference); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Constructs a wrapper instance using the standard constructor. |
| 27 | + /// </summary> |
| 28 | + /// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param> |
| 29 | + /// <returns>A wrapper instance for a <see cref="FileReader"/>.</returns> |
| 30 | + public static new async Task<FileReaderInProcess> CreateAsync(IJSRuntime jSRuntime) |
| 31 | + { |
| 32 | + IJSInProcessObjectReference inProcesshelper = await jSRuntime.GetInProcessHelperAsync(); |
| 33 | + IJSInProcessObjectReference jSInstance = await inProcesshelper.InvokeAsync<IJSInProcessObjectReference>("constructFileReader"); |
| 34 | + var fileReaderInProcess = new FileReaderInProcess(jSRuntime, inProcesshelper, jSInstance); |
| 35 | + await inProcesshelper.InvokeVoidAsync("registerEventHandlers", DotNetObjectReference.Create(fileReaderInProcess), jSInstance); |
| 36 | + return fileReaderInProcess; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Constructs a wrapper instance for a given JS Instance of a <see cref="File"/>. |
| 41 | + /// </summary> |
| 42 | + /// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param> |
| 43 | + /// <param name="inProcessHelper">An in process helper instance.</param> |
| 44 | + /// <param name="jSReference">A JS reference to an existing <see cref="File"/>.</param> |
| 45 | + internal FileReaderInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference inProcessHelper, IJSInProcessObjectReference jSReference) : base(jSRuntime, jSReference) |
| 46 | + { |
| 47 | + this.inProcessHelper = inProcessHelper; |
| 48 | + JSReference = jSReference; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Starts a new read for some <see cref="Blob"/> as an <see langword="byte"/>[] which can be read from <see cref="GetResultAsByteArrayAsync"/> once the load has ended which can be checked by setting the action <see cref="OnLoadEnd"/>. |
| 53 | + /// </summary> |
| 54 | + /// <param name="blob">The <see cref="Blob"/> that should be read asynchronously.</param> |
| 55 | + /// <returns></returns> |
| 56 | + public void ReadAsArrayBuffer(Blob blob) |
| 57 | + { |
| 58 | + JSReference.InvokeVoid("readAsArrayBuffer", blob.JSReference); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Starts a new read for some <see cref="Blob"/> as a binarily encoded <see langword="string"/> which can be read from <see cref="GetResultAsStringAsync"/> once the load has ended which can be checked by setting the action <see cref="OnLoadEnd"/>. |
| 63 | + /// </summary> |
| 64 | + /// <param name="blob">The <see cref="Blob"/> that should be read asynchronously.</param> |
| 65 | + /// <returns></returns> |
| 66 | + public void ReadAsBinaryString(Blob blob) |
| 67 | + { |
| 68 | + JSReference.InvokeVoid("readAsBinaryString", blob.JSReference); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Starts a new read for some <see cref="Blob"/> as a <see langword="string"/> which can be read from <see cref="GetResultAsStringAsync"/> once the load has ended which can be checked by setting the action <see cref="OnLoadEnd"/>. |
| 73 | + /// </summary> |
| 74 | + /// <param name="blob">The <see cref="Blob"/> that should be read asynchronously.</param> |
| 75 | + /// <param name="encoding">An optional encoding for the text. The default is UTF-8.</param> |
| 76 | + /// <returns></returns> |
| 77 | + public void ReadAsText(Blob blob, string? encoding = null) |
| 78 | + { |
| 79 | + JSReference.InvokeVoid("readAsText", blob.JSReference, encoding); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Starts a new read for some <see cref="Blob"/> as a base64 encoded Data URL which can be read from <see cref="GetResultAsStringAsync"/> once the load has ended which can be checked by setting the action <see cref="OnLoadEnd"/>. |
| 84 | + /// </summary> |
| 85 | + /// <param name="blob">The <see cref="Blob"/> that should be read asynchronously.</param> |
| 86 | + /// <returns></returns> |
| 87 | + public void ReadAsDataURL(Blob blob) |
| 88 | + { |
| 89 | + JSReference.InvokeVoid("readAsDataURL", blob.JSReference); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Terminates the load if the <see cref="GetReadyStateAsync"/> is <see cref="LOADING"/> else it sets the result to <see langword="null"/>. |
| 94 | + /// </summary> |
| 95 | + /// <param name="blob">The <see cref="Blob"/> read that should be terminated.</param> |
| 96 | + /// <returns></returns> |
| 97 | + public void Abort(Blob blob) |
| 98 | + { |
| 99 | + JSReference.InvokeVoid("abort", blob.JSReference); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Gets the state of the <see cref="FileReader"/>. |
| 104 | + /// </summary> |
| 105 | + /// <returns>As a standard either <see cref="EMPTY"/>, <see cref="LOADING"/> or <see cref="DONE"/></returns> |
| 106 | + public ushort ReadyState => inProcessHelper.Invoke<ushort>("getAttribute", JSReference, "readyState"); |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Checks whether the result is a either a <see langword="string"/> or a byte array. |
| 110 | + /// </summary> |
| 111 | + /// <returns>Either the type of <see langword="string"/> or type of <see cref="byte"/>[].</returns> |
| 112 | + public Type? ResultType => inProcessHelper.Invoke<bool>("isArrayBuffer", JSReference) ? typeof(byte[]) : typeof(string); |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Gets the result of the read a <see langword="string"/>. |
| 116 | + /// </summary> |
| 117 | + /// <returns>A <see langword="string"/> representing the read. If there was no result from the read or if the read has not ended yet then it will be <see langword="null"/>.</returns> |
| 118 | + public string? ResultAsString => inProcessHelper.Invoke<string?>("getAttribute", JSReference, "result"); |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets the result of the read a <see langword="byte"/>[]. |
| 122 | + /// </summary> |
| 123 | + /// <returns>A <see langword="byte"/>[] representing the read. If there was no result from the read or if the read has not ended yet then it will be <see langword="null"/>.</returns> |
| 124 | + public byte[]? ResultAsByteArray => inProcessHelper.Invoke<byte[]?>("arrayBuffer", inProcessHelper.Invoke<IJSObjectReference>("getAttribute", JSReference, "result")); |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Gets the error object reference which will be <see langword="null"/> if no error occured. |
| 128 | + /// </summary> |
| 129 | + /// <returns>A nullable IJSObjectReference because it was out of scope to wrap the Exception API.</returns> |
| 130 | + public IJSObjectReference? Error => inProcessHelper.Invoke<IJSObjectReference?>("getAttribute", JSReference, "error"); |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Invoked when a load starts. |
| 134 | + /// </summary> |
| 135 | + public new Action<ProgressEventInProcess>? OnLoadStart { get; set; } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Invoked when the progress of a load changes which includes when it ends. |
| 139 | + /// </summary> |
| 140 | + [JsonIgnore] |
| 141 | + public new Action<ProgressEventInProcess>? OnProgress { get; set; } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Invoked when a load ends successfully. |
| 145 | + /// </summary> |
| 146 | + [JsonIgnore] |
| 147 | + public new Action<ProgressEventInProcess>? OnLoad { get; set; } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Invoked when a load is aborted. |
| 151 | + /// </summary> |
| 152 | + [JsonIgnore] |
| 153 | + public new Action<ProgressEventInProcess>? OnAbort { get; set; } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Invoked when a load fails due to an error. |
| 157 | + /// </summary> |
| 158 | + [JsonIgnore] |
| 159 | + public new Action<ProgressEventInProcess>? OnError { get; set; } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Invoked when a load finishes successfully or not. |
| 163 | + /// </summary> |
| 164 | + [JsonIgnore] |
| 165 | + public new Action<ProgressEventInProcess>? OnLoadEnd { get; set; } |
| 166 | + |
| 167 | + [JSInvokable] |
| 168 | + public void InvokeOnLoadStart(IJSInProcessObjectReference jsProgressEvent) |
| 169 | + { |
| 170 | + if (OnLoadStart is null) return; |
| 171 | + OnLoadStart.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 172 | + } |
| 173 | + |
| 174 | + [JSInvokable] |
| 175 | + public void InvokeOnProgress(IJSInProcessObjectReference jsProgressEvent) |
| 176 | + { |
| 177 | + if (OnProgress is null) return; |
| 178 | + OnProgress.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 179 | + } |
| 180 | + |
| 181 | + [JSInvokable] |
| 182 | + public void InvokeOnLoad(IJSInProcessObjectReference jsProgressEvent) |
| 183 | + { |
| 184 | + if (OnLoad is null) return; |
| 185 | + OnLoad.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 186 | + } |
| 187 | + |
| 188 | + [JSInvokable] |
| 189 | + public void InvokeOnAbort(IJSInProcessObjectReference jsProgressEvent) |
| 190 | + { |
| 191 | + if (OnAbort is null) return; |
| 192 | + OnAbort.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 193 | + } |
| 194 | + |
| 195 | + [JSInvokable] |
| 196 | + public void InvokeOnError(IJSInProcessObjectReference jsProgressEvent) |
| 197 | + { |
| 198 | + if (OnError is null) return; |
| 199 | + OnError.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 200 | + } |
| 201 | + |
| 202 | + [JSInvokable] |
| 203 | + public void InvokeOnLoadEnd(IJSInProcessObjectReference jsProgressEvent) |
| 204 | + { |
| 205 | + if (OnLoadEnd is null) return; |
| 206 | + OnLoadEnd.Invoke(new ProgressEventInProcess(jSRuntime, inProcessHelper, jsProgressEvent)); |
| 207 | + } |
| 208 | +} |
0 commit comments