/
StorageFile.Base.cs
102 lines (76 loc) · 3.51 KB
/
StorageFile.Base.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#nullable enable
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;
using Buffer = Windows.Storage.Streams.Buffer;
namespace Windows.Storage
{
partial class StorageFile
{
private abstract class ImplementationBase
{
protected ImplementationBase(string path)
=> Path = path;
public void InitOwner(StorageFile owner)
=> Owner = owner; // Lazy initialized to avoid delegate in StorageFile ctor
protected StorageFile Owner { get; private set; } = null!; // Should probably be private
public virtual string Path { get; protected set; }
public virtual string FileType => global::System.IO.Path.GetExtension(Path);
public virtual string Name => global::System.IO.Path.GetFileName(Path);
public virtual string DisplayName => global::System.IO.Path.GetFileNameWithoutExtension(Path);
public virtual string ContentType => GetContentTypeFromFileType(FileType);
public abstract DateTimeOffset DateCreated { get; }
public bool IsEqual(IStorageItem item)
=> item is StorageFile sf && IsEqual(sf._impl);
protected abstract bool IsEqual(ImplementationBase impl);
public abstract Task<StorageFolder> GetParent(CancellationToken ct);
public abstract Task<BasicProperties> GetBasicProperties(CancellationToken ct);
public abstract Task<IRandomAccessStreamWithContentType> Open(CancellationToken ct, FileAccessMode accessMode, StorageOpenOptions options);
public virtual async Task<Stream> OpenStream(CancellationToken ct, FileAccessMode accessMode, StorageOpenOptions options)
=> (await Open(ct, accessMode, options).AsTask(ct)).AsStream();
public abstract Task<StorageStreamTransaction> OpenTransactedWrite(CancellationToken ct, StorageOpenOptions option);
public abstract Task Delete(CancellationToken ct, StorageDeleteOption options);
public virtual async Task Rename(CancellationToken ct, string desiredName, NameCollisionOption option)
{
var parent = await GetParent(ct);
await Move(ct, parent, desiredName, option);
}
public virtual async Task<StorageFile> Copy(CancellationToken ct, IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option)
{
var dst = await CreateDestination(ct, destinationFolder, desiredNewName, option);
await CopyAndReplace(ct, dst);
return dst;
}
public virtual async Task CopyAndReplace(CancellationToken ct, IStorageFile target)
{
using (var src = await Owner.OpenStreamForReadAsync())
using (var dst = await target.OpenStreamForReadAsync())
{
await src.CopyToAsync(dst, Buffer.DefaultCapacity, ct);
}
}
public virtual async Task Move(CancellationToken ct, IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option)
{
var dst = await CreateDestination(ct, destinationFolder, desiredNewName, option);
await MoveAndReplace(ct, dst);
}
public virtual async Task MoveAndReplace(CancellationToken ct, IStorageFile target)
{
using (var src = await Owner.OpenStreamForReadAsync())
using (var dst = await target.OpenStreamForReadAsync())
{
await src.CopyToAsync(dst, Buffer.DefaultCapacity, ct);
}
await Delete(ct, StorageDeleteOption.PermanentDelete);
Path = target.Path;
}
protected Exception NotImplemented([CallerMemberName] string? method = null)
=> new NotSupportedException($"{method} is not supported yet for {GetType().Name}");
}
}
}