|
| 1 | +// Copyright (c) 2023 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Runtime.InteropServices.WindowsRuntime; |
| 6 | +using Windows.Foundation; |
| 7 | +using Windows.Storage; |
| 8 | +using Windows.Storage.FileProperties; |
| 9 | +using Windows.Storage.Streams; |
| 10 | +using IO = System.IO; |
| 11 | + |
| 12 | +namespace Files.App.Filesystem.StorageItems |
| 13 | +{ |
| 14 | + public class VirtualStorageFile : BaseStorageFile |
| 15 | + { |
| 16 | + public override string Path { get; } |
| 17 | + public override string Name { get; } |
| 18 | + public override string DisplayName => Name; |
| 19 | + public override string ContentType => "application/octet-stream"; |
| 20 | + public override string FileType => IO.Path.GetExtension(Name); |
| 21 | + public override string FolderRelativeId => $"0\\{Name}"; |
| 22 | + |
| 23 | + public override string DisplayType |
| 24 | + { |
| 25 | + get |
| 26 | + { |
| 27 | + var itemType = "File".GetLocalizedResource(); |
| 28 | + if (Name.Contains('.', StringComparison.Ordinal)) |
| 29 | + { |
| 30 | + itemType = IO.Path.GetExtension(Name).Trim('.') + " " + itemType; |
| 31 | + } |
| 32 | + return itemType; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private Stream Contents { get; init; } |
| 37 | + |
| 38 | + public override DateTimeOffset DateCreated { get; } |
| 39 | + public override Windows.Storage.FileAttributes Attributes { get; } = Windows.Storage.FileAttributes.Normal; |
| 40 | + public override IStorageItemExtraProperties Properties => new BaseBasicStorageItemExtraProperties(this); |
| 41 | + |
| 42 | + public VirtualStorageFile(Stream contents, string cFileName) |
| 43 | + { |
| 44 | + Contents = contents; |
| 45 | + Name = cFileName; |
| 46 | + Path = ""; |
| 47 | + } |
| 48 | + |
| 49 | + private async void StreamedFileWriter(StreamedFileDataRequest request) |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + using (var stream = request.AsStreamForWrite()) |
| 54 | + { |
| 55 | + await Contents.CopyToAsync(stream); |
| 56 | + await stream.FlushAsync(); |
| 57 | + } |
| 58 | + request.Dispose(); |
| 59 | + } |
| 60 | + catch (Exception) |
| 61 | + { |
| 62 | + request.FailAndClose(StreamedFileFailureMode.Incomplete); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public override IAsyncOperation<BaseBasicProperties> GetBasicPropertiesAsync() |
| 67 | + { |
| 68 | + return AsyncInfo.Run(async (cancellationToken) => |
| 69 | + { |
| 70 | + return new BaseBasicProperties(); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + public override bool IsOfType(StorageItemTypes type) |
| 75 | + { |
| 76 | + return Attributes.HasFlag(Windows.Storage.FileAttributes.Directory) ? type == StorageItemTypes.Folder : type == StorageItemTypes.File; |
| 77 | + } |
| 78 | + |
| 79 | + public override IAsyncOperation<StorageFile> ToStorageFileAsync() |
| 80 | + { |
| 81 | + return StorageFile.CreateStreamedFileAsync(Name, StreamedFileWriter, null); |
| 82 | + } |
| 83 | + |
| 84 | + public override bool IsEqual(IStorageItem item) => item?.Path == Path; |
| 85 | + |
| 86 | + public override IAsyncOperation<BaseStorageFolder> GetParentAsync() => throw new NotSupportedException(); |
| 87 | + |
| 88 | + public override IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode) |
| 89 | + { |
| 90 | + return Task.FromResult(Contents.AsRandomAccessStream()).AsAsyncOperation(); |
| 91 | + } |
| 92 | + |
| 93 | + public override IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode, StorageOpenOptions options) => OpenAsync(accessMode); |
| 94 | + |
| 95 | + public override IAsyncOperation<IRandomAccessStreamWithContentType> OpenReadAsync() |
| 96 | + { |
| 97 | + return Task.FromResult<IRandomAccessStreamWithContentType>(new StreamWithContentType(Contents.AsRandomAccessStream())) |
| 98 | + .AsAsyncOperation(); |
| 99 | + } |
| 100 | + |
| 101 | + public override IAsyncOperation<IInputStream> OpenSequentialReadAsync() |
| 102 | + { |
| 103 | + return Task.FromResult(Contents.AsInputStream()).AsAsyncOperation(); |
| 104 | + } |
| 105 | + |
| 106 | + public override IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync() => throw new NotSupportedException(); |
| 107 | + public override IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync(StorageOpenOptions options) => throw new NotSupportedException(); |
| 108 | + |
| 109 | + public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destinationFolder) |
| 110 | + => CopyAsync(destinationFolder, Name, NameCollisionOption.FailIfExists); |
| 111 | + public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName) |
| 112 | + => CopyAsync(destinationFolder, desiredNewName, NameCollisionOption.FailIfExists); |
| 113 | + |
| 114 | + public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option) |
| 115 | + { |
| 116 | + return AsyncInfo.Run(async (cancellationToken) => |
| 117 | + { |
| 118 | + BaseStorageFolder destFolder = destinationFolder.AsBaseStorageFolder(); |
| 119 | + |
| 120 | + if (destFolder is ICreateFileWithStream cwsf) |
| 121 | + { |
| 122 | + using var inStream = await this.OpenStreamForReadAsync(); |
| 123 | + return await cwsf.CreateFileAsync(inStream, desiredNewName, option.Convert()); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert()); |
| 128 | + using (var inStream = await this.OpenStreamForReadAsync()) |
| 129 | + using (var outStream = await destFile.OpenStreamForWriteAsync()) |
| 130 | + { |
| 131 | + await inStream.CopyToAsync(outStream); |
| 132 | + await outStream.FlushAsync(); |
| 133 | + } |
| 134 | + return destFile; |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + public override IAsyncAction MoveAsync(IStorageFolder destinationFolder) => throw new NotSupportedException(); |
| 140 | + public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName) => throw new NotSupportedException(); |
| 141 | + public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option) => throw new NotSupportedException(); |
| 142 | + |
| 143 | + public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace) => throw new NotSupportedException(); |
| 144 | + public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace) => throw new NotSupportedException(); |
| 145 | + |
| 146 | + public override IAsyncAction RenameAsync(string desiredName) |
| 147 | + => RenameAsync(desiredName, NameCollisionOption.FailIfExists); |
| 148 | + |
| 149 | + public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) |
| 150 | + => throw new NotSupportedException(); |
| 151 | + |
| 152 | + public override IAsyncAction DeleteAsync() => throw new NotSupportedException(); |
| 153 | + |
| 154 | + public override IAsyncAction DeleteAsync(StorageDeleteOption option) => throw new NotSupportedException(); |
| 155 | + |
| 156 | + public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode) |
| 157 | + => Task.FromResult<StorageItemThumbnail>(null).AsAsyncOperation(); |
| 158 | + public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize) |
| 159 | + => Task.FromResult<StorageItemThumbnail>(null).AsAsyncOperation(); |
| 160 | + public override IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) |
| 161 | + => Task.FromResult<StorageItemThumbnail>(null).AsAsyncOperation(); |
| 162 | + } |
| 163 | +} |
0 commit comments