예제 #1
0
            public override async Task <StorageFile> CreateFileAsync(string desiredName, CreationCollisionOption option, CancellationToken cancellationToken)
            {
                var actualName = desiredName;

                var existingItem = await TryGetItemAsync(desiredName, cancellationToken);

                switch (option)
                {
                case CreationCollisionOption.ReplaceExisting:
                    if (existingItem is StorageFolder)
                    {
                        throw new UnauthorizedAccessException("There is already a folder with the same name.");
                    }

                    if (existingItem is StorageFile)
                    {
                        // Delete existing file
                        await existingItem.DeleteAsync();
                    }
                    break;

                case CreationCollisionOption.FailIfExists:
                    if (existingItem != null)
                    {
                        throw new UnauthorizedAccessException("There is already an item with the same name.");
                    }
                    break;

                case CreationCollisionOption.OpenIfExists:
                    if (existingItem is StorageFile existingFile)
                    {
                        return(existingFile);
                    }

                    if (existingItem is StorageFolder)
                    {
                        throw new UnauthorizedAccessException("There is already a file with the same name.");
                    }
                    break;

                case CreationCollisionOption.GenerateUniqueName:
                    actualName = await FindAvailableNumberedFileNameAsync(desiredName);

                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(option));
                }

                var newFolderNativeInfo = await WebAssemblyRuntime.InvokeAsync($"{JsType}.createFileAsync(\"{_id}\", \"{WebAssemblyRuntime.EscapeJs(actualName)}\")");

                if (newFolderNativeInfo == null)
                {
                    throw new UnauthorizedAccessException("Could not create file.");
                }

                var info = JsonHelper.Deserialize <NativeStorageItemInfo>(newFolderNativeInfo);

                return(StorageFile.GetFromNativeInfo(info, Owner));
            }
예제 #2
0
            public override async Task <IReadOnlyList <StorageFile> > GetFilesAsync(CancellationToken ct)
            {
                var itemInfosJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.getFilesAsync(\"{_id}\")");

                var itemInfos = JsonHelper.Deserialize <NativeStorageItemInfo[]>(itemInfosJson);
                var results   = new List <StorageFile>();

                foreach (var info in itemInfos)
                {
                    results.Add(StorageFile.GetFromNativeInfo(info));
                }
                return(results.AsReadOnly());
            }
예제 #3
0
            public override async Task <StorageFile> GetFileAsync(string name, CancellationToken token)
            {
                var fileInfoJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.tryGetFileAsync(\"{_id}\", \"{WebAssemblyRuntime.EscapeJs(name)}\")");

                if (fileInfoJson == null)
                {
                    throw new FileNotFoundException($"There is no file with name '{name}'.");
                }

                // File exists
                var fileInfo = JsonHelper.Deserialize <NativeStorageItemInfo>(fileInfoJson);

                return(StorageFile.GetFromNativeInfo(fileInfo, Owner));
            }
예제 #4
0
            public override async Task <IStorageItem?> TryGetItemAsync(string name, CancellationToken token)
            {
                var fileInfoJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.tryGetFileAsync(\"{_id}\", \"{WebAssemblyRuntime.EscapeJs(name)}\")");

                if (fileInfoJson != null)
                {
                    // File exists
                    var fileInfo = JsonHelper.Deserialize <NativeStorageItemInfo>(fileInfoJson);
                    return(StorageFile.GetFromNativeInfo(fileInfo, Owner));
                }

                var folderInfoJson = await WebAssemblyRuntime.InvokeAsync($"{JsType}.tryGetFolderAsync(\"{_id}\", \"{WebAssemblyRuntime.EscapeJs(name)}\")");

                if (folderInfoJson != null)
                {
                    // Folder exists
                    var folderInfo = JsonHelper.Deserialize <NativeStorageItemInfo>(folderInfoJson);
                    return(GetFromNativeInfo(folderInfo, Owner));
                }

                return(null);
            }