|
| 1 | +using KristofferStrube.Blazor.FileSystemAccess.Extensions; |
| 2 | +using Microsoft.JSInterop; |
| 3 | + |
| 4 | +namespace KristofferStrube.Blazor.FileSystemAccess; |
| 5 | +internal abstract class BaseFileSystemAccessService<TFsFileHandle, TFsDirectoryHandle, TObjReference> : IFileSystemAccessService<TFsFileHandle, TFsDirectoryHandle, TObjReference> |
| 6 | + where TFsFileHandle : FileSystemFileHandle |
| 7 | + where TFsDirectoryHandle : FileSystemDirectoryHandle |
| 8 | + where TObjReference : IJSObjectReference |
| 9 | +{ |
| 10 | + protected readonly Lazy<Task<IJSObjectReference>> helperTask; |
| 11 | + protected readonly IJSRuntime jSRuntime; |
| 12 | + |
| 13 | + public BaseFileSystemAccessService(IJSRuntime jSRuntime) |
| 14 | + { |
| 15 | + helperTask = new(() => jSRuntime.GetHelperAsync(FileSystemAccessOptions.DefaultInstance)); |
| 16 | + this.jSRuntime = jSRuntime; |
| 17 | + } |
| 18 | + |
| 19 | + #region ShowOpenFilePickerAsync |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 23 | + /// </summary> |
| 24 | + /// <param name="openFilePickerOptions"></param> |
| 25 | + /// <returns></returns> |
| 26 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInWellKnownDirectory? openFilePickerOptions) |
| 27 | + => await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable()); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 31 | + /// </summary> |
| 32 | + /// <param name="openFilePickerOptions"></param> |
| 33 | + /// <returns></returns> |
| 34 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInWellKnownDirectory? openFilePickerOptions, FileSystemAccessOptions fsaOptions) |
| 35 | + => await InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 39 | + /// </summary> |
| 40 | + /// <param name="openFilePickerOptions"></param> |
| 41 | + /// <returns></returns> |
| 42 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInFileSystemHandle? openFilePickerOptions) |
| 43 | + => await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable()); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 47 | + /// </summary> |
| 48 | + /// <param name="openFilePickerOptions"></param> |
| 49 | + /// <returns></returns> |
| 50 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(OpenFilePickerOptionsStartInFileSystemHandle? openFilePickerOptions, FileSystemAccessOptions fsaOptions) |
| 51 | + => await this.InternalShowOpenFilePickerAsync(openFilePickerOptions?.Serializable(), fsaOptions); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 55 | + /// </summary> |
| 56 | + /// <returns></returns> |
| 57 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync() |
| 58 | + => await InternalShowOpenFilePickerAsync(null); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// <see href="https://wicg.github.io/file-system-access/#api-showopenfilepicker">showOpenFilePicker() browser specs</see> |
| 62 | + /// </summary> |
| 63 | + /// <returns></returns> |
| 64 | + public async Task<TFsFileHandle[]> ShowOpenFilePickerAsync(FileSystemAccessOptions fsaOptions) |
| 65 | + => await InternalShowOpenFilePickerAsync(null, fsaOptions); |
| 66 | + |
| 67 | + protected async Task<TFsFileHandle[]> InternalShowOpenFilePickerAsync(object? options) |
| 68 | + => await InternalShowOpenFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance); |
| 69 | + protected async Task<TFsFileHandle[]> InternalShowOpenFilePickerAsync(object? options, FileSystemAccessOptions fsaOptions) |
| 70 | + { |
| 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); |
| 74 | + |
| 75 | + return await Task.WhenAll( |
| 76 | + Enumerable |
| 77 | + .Range(0, length) |
| 78 | + .Select(async i => |
| 79 | + await this.CreateFileHandleAsync( |
| 80 | + jSRuntime, |
| 81 | + await jSFileHandles.InvokeAsync<TObjReference>("at", i), |
| 82 | + fsaOptions) |
| 83 | + ) |
| 84 | + .ToArray() |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + #endregion |
| 89 | + |
| 90 | + #region ShowSaveFilePickerAsync |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 94 | + /// </summary> |
| 95 | + /// <param name="saveFilePickerOptions"></param> |
| 96 | + /// <returns></returns> |
| 97 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInWellKnownDirectory? saveFilePickerOptions) |
| 98 | + => await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable()); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 102 | + /// </summary> |
| 103 | + /// <param name="saveFilePickerOptions"></param> |
| 104 | + /// <returns></returns> |
| 105 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInWellKnownDirectory? saveFilePickerOptions, FileSystemAccessOptions fsaOptions) |
| 106 | + => await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions); |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 110 | + /// </summary> |
| 111 | + /// <param name="saveFilePickerOptions"></param> |
| 112 | + /// <returns></returns> |
| 113 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInFileSystemHandle? saveFilePickerOptions) |
| 114 | + => await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable()); |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 118 | + /// </summary> |
| 119 | + /// <param name="saveFilePickerOptions"></param> |
| 120 | + /// <returns></returns> |
| 121 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync(SaveFilePickerOptionsStartInFileSystemHandle? saveFilePickerOptions, FileSystemAccessOptions fsaOptions) |
| 122 | + => await InternalShowSaveFilePickerAsync(saveFilePickerOptions?.Serializable(), fsaOptions); |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 126 | + /// </summary> |
| 127 | + /// <returns></returns> |
| 128 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync() |
| 129 | + => await InternalShowSaveFilePickerAsync(null); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// <see href="https://wicg.github.io/file-system-access/#api-showsavefilepicker">showSaveFilePicker() browser specs</see> |
| 133 | + /// </summary> |
| 134 | + /// <returns></returns> |
| 135 | + public async Task<TFsFileHandle> ShowSaveFilePickerAsync(FileSystemAccessOptions options) |
| 136 | + => await InternalShowSaveFilePickerAsync(null, options); |
| 137 | + |
| 138 | + protected async Task<TFsFileHandle> InternalShowSaveFilePickerAsync(object? options) |
| 139 | + => await this.InternalShowSaveFilePickerAsync(options, FileSystemAccessOptions.DefaultInstance); |
| 140 | + |
| 141 | + protected async Task<TFsFileHandle> InternalShowSaveFilePickerAsync(object? options, FileSystemAccessOptions fsaOptions) |
| 142 | + { |
| 143 | + var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showSaveFilePicker", options); |
| 144 | + return await this.CreateFileHandleAsync(jSRuntime, jSFileHandle, fsaOptions); |
| 145 | + } |
| 146 | + |
| 147 | + #endregion |
| 148 | + |
| 149 | + #region ShowDirectoryPickerAsync |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 153 | + /// </summary> |
| 154 | + /// <param name="directoryPickerOptions"></param> |
| 155 | + /// <returns></returns> |
| 156 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInWellKnownDirectory? directoryPickerOptions) |
| 157 | + => await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable()); |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 161 | + /// </summary> |
| 162 | + /// <param name="directoryPickerOptions"></param> |
| 163 | + /// <returns></returns> |
| 164 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInWellKnownDirectory? directoryPickerOptions, FileSystemAccessOptions fasOptions) |
| 165 | + => await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions); |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 169 | + /// </summary> |
| 170 | + /// <param name="directoryPickerOptions"></param> |
| 171 | + /// <returns></returns> |
| 172 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInFileSystemHandle? directoryPickerOptions) |
| 173 | + => await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable()); |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 177 | + /// </summary> |
| 178 | + /// <param name="directoryPickerOptions"></param> |
| 179 | + /// <returns></returns> |
| 180 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(DirectoryPickerOptionsStartInFileSystemHandle? directoryPickerOptions, FileSystemAccessOptions fasOptions) |
| 181 | + => await InternalShowDirectoryPickerAsync(directoryPickerOptions?.Serializable(), fasOptions); |
| 182 | + |
| 183 | + /// <summary> |
| 184 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 185 | + /// </summary> |
| 186 | + /// <param name="directoryPickerOptions"></param> |
| 187 | + /// <returns></returns> |
| 188 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync() |
| 189 | + => await InternalShowDirectoryPickerAsync(null); |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// <see href="https://wicg.github.io/file-system-access/#api-showdirectorypicker">showDirectoryPicker() browser specs</see> |
| 193 | + /// </summary> |
| 194 | + /// <param name="directoryPickerOptions"></param> |
| 195 | + /// <returns></returns> |
| 196 | + public async Task<TFsDirectoryHandle> ShowDirectoryPickerAsync(FileSystemAccessOptions fasOptions) |
| 197 | + => await InternalShowDirectoryPickerAsync(null, fasOptions); |
| 198 | + |
| 199 | + protected async Task<TFsDirectoryHandle> InternalShowDirectoryPickerAsync(object? options) |
| 200 | + => await InternalShowDirectoryPickerAsync(options, FileSystemAccessOptions.DefaultInstance); |
| 201 | + |
| 202 | + protected async Task<TFsDirectoryHandle> InternalShowDirectoryPickerAsync(object? options, FileSystemAccessOptions fasOptions) |
| 203 | + { |
| 204 | + var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("window.showDirectoryPicker", options); |
| 205 | + return await this.CreateDirectoryHandleAsync(jSRuntime, jSFileHandle, fasOptions); |
| 206 | + } |
| 207 | + |
| 208 | + #endregion |
| 209 | + |
| 210 | + #region GetOriginPrivateDirectoryAsync |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// <see href="https://wicg.github.io/file-system-access/#dom-storagemanager-getdirectory">getDirectory() for StorageManager browser specs</see> |
| 214 | + /// </summary> |
| 215 | + /// <returns></returns> |
| 216 | + public async Task<TFsDirectoryHandle> GetOriginPrivateDirectoryAsync() |
| 217 | + => await this.GetOriginPrivateDirectoryAsync(FileSystemAccessOptions.DefaultInstance); |
| 218 | + |
| 219 | + /// <summary> |
| 220 | + /// <see href="https://wicg.github.io/file-system-access/#dom-storagemanager-getdirectory">getDirectory() for StorageManager browser specs</see> |
| 221 | + /// </summary> |
| 222 | + /// <returns></returns> |
| 223 | + public async Task<TFsDirectoryHandle> GetOriginPrivateDirectoryAsync(FileSystemAccessOptions fasOptions) |
| 224 | + { |
| 225 | + var jSFileHandle = await jSRuntime.InvokeAsync<TObjReference>("navigator.storage.getDirectory"); |
| 226 | + return await CreateDirectoryHandleAsync(jSRuntime, jSFileHandle, fasOptions); |
| 227 | + } |
| 228 | + |
| 229 | + #endregion |
| 230 | + |
| 231 | + #region Common Handling Methods |
| 232 | + |
| 233 | + protected abstract Task<TFsFileHandle> CreateFileHandleAsync(IJSRuntime jSRuntime, TObjReference jSReference, FileSystemAccessOptions options); |
| 234 | + protected abstract Task<TFsDirectoryHandle> CreateDirectoryHandleAsync(IJSRuntime jSRuntime, TObjReference jSReference, FileSystemAccessOptions options); |
| 235 | + |
| 236 | + #endregion |
| 237 | + |
| 238 | + /// <summary> |
| 239 | + /// Meta method for the wrapper that checks if the API is available in the current browser. |
| 240 | + /// </summary> |
| 241 | + /// <returns></returns> |
| 242 | + public async Task<bool> IsSupportedAsync() |
| 243 | + { |
| 244 | + return |
| 245 | + await jSRuntime.InvokeAsync<bool>("window.hasOwnProperty", "showOpenFilePicker") & |
| 246 | + await jSRuntime.InvokeAsync<bool>("window.hasOwnProperty", "showSaveFilePicker") & |
| 247 | + await jSRuntime.InvokeAsync<bool>("window.hasOwnProperty", "showDirectoryPicker"); |
| 248 | + } |
| 249 | + |
| 250 | + public async ValueTask DisposeAsync() |
| 251 | + { |
| 252 | + if (helperTask.IsValueCreated) |
| 253 | + { |
| 254 | + IJSObjectReference module = await helperTask.Value; |
| 255 | + await module.DisposeAsync(); |
| 256 | + } |
| 257 | + GC.SuppressFinalize(this); |
| 258 | + } |
| 259 | +} |
0 commit comments