This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 505
/
FileSystem.ios.cs
224 lines (174 loc) · 6.39 KB
/
FileSystem.ios.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Foundation;
using Photos;
using UIKit;
namespace Xamarin.Essentials
{
public partial class FileSystem
{
internal static async Task<FileResult[]> EnsurePhysicalFileResultsAsync(params NSUrl[] urls)
{
if (urls == null || urls.Length == 0)
return Array.Empty<FileResult>();
var opts = NSFileCoordinatorReadingOptions.WithoutChanges;
var intents = urls.Select(x => NSFileAccessIntent.CreateReadingIntent(x, opts)).ToArray();
using var coordinator = new NSFileCoordinator();
var tcs = new TaskCompletionSource<FileResult[]>();
coordinator.CoordinateAccess(intents, new NSOperationQueue(), error =>
{
if (error != null)
{
tcs.TrySetException(new NSErrorException(error));
return;
}
var bookmarks = new List<FileResult>();
foreach (var intent in intents)
{
var url = intent.Url;
var result = new BookmarkDataFileResult(url);
bookmarks.Add(result);
}
tcs.TrySetResult(bookmarks.ToArray());
});
return await tcs.Task;
}
}
class BookmarkDataFileResult : FileResult
{
NSData bookmark;
internal BookmarkDataFileResult(NSUrl url)
: base()
{
try
{
url.StartAccessingSecurityScopedResource();
var newBookmark = url.CreateBookmarkData(0, Array.Empty<string>(), null, out var bookmarkError);
if (bookmarkError != null)
throw new NSErrorException(bookmarkError);
UpdateBookmark(url, newBookmark);
}
finally
{
url.StopAccessingSecurityScopedResource();
}
}
void UpdateBookmark(NSUrl url, NSData newBookmark)
{
bookmark = newBookmark;
var doc = new UIDocument(url);
FullPath = doc.FileUrl?.Path;
FileName = doc.LocalizedName ?? Path.GetFileName(FullPath);
}
internal override Task<Stream> PlatformOpenReadAsync()
{
var url = NSUrl.FromBookmarkData(bookmark, 0, null, out var isStale, out var error);
if (error != null)
throw new NSErrorException(error);
url.StartAccessingSecurityScopedResource();
if (isStale)
{
var newBookmark = url.CreateBookmarkData(NSUrlBookmarkCreationOptions.SuitableForBookmarkFile, Array.Empty<string>(), null, out error);
if (error != null)
throw new NSErrorException(error);
UpdateBookmark(url, newBookmark);
}
var fileStream = File.OpenRead(FullPath);
Stream stream = new SecurityScopedStream(fileStream, url);
return Task.FromResult(stream);
}
class SecurityScopedStream : Stream
{
FileStream stream;
NSUrl url;
internal SecurityScopedStream(FileStream stream, NSUrl url)
{
this.stream = stream;
this.url = url;
}
public override bool CanRead => stream.CanRead;
public override bool CanSeek => stream.CanSeek;
public override bool CanWrite => stream.CanWrite;
public override long Length => stream.Length;
public override long Position
{
get => stream.Position;
set => stream.Position = value;
}
public override void Flush() =>
stream.Flush();
public override int Read(byte[] buffer, int offset, int count) =>
stream.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) =>
stream.Seek(offset, origin);
public override void SetLength(long value) =>
stream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) =>
stream.Write(buffer, offset, count);
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
stream?.Dispose();
stream = null;
url?.StopAccessingSecurityScopedResource();
url = null;
}
}
}
}
class UIDocumentFileResult : FileResult
{
internal UIDocumentFileResult(NSUrl url)
: base()
{
var doc = new UIDocument(url);
FullPath = doc.FileUrl?.Path;
FileName = doc.LocalizedName ?? Path.GetFileName(FullPath);
}
internal override Task<Stream> PlatformOpenReadAsync()
{
Stream fileStream = File.OpenRead(FullPath);
return Task.FromResult(fileStream);
}
}
class UIImageFileResult : FileResult
{
readonly UIImage uiImage;
NSData data;
internal UIImageFileResult(UIImage image)
: base()
{
uiImage = image;
FullPath = Guid.NewGuid().ToString() + FileSystem.Extensions.Png;
FileName = FullPath;
}
internal override Task<Stream> PlatformOpenReadAsync()
{
data ??= uiImage.AsPNG();
return Task.FromResult(data.AsStream());
}
}
class PHAssetFileResult : FileResult
{
readonly PHAsset phAsset;
internal PHAssetFileResult(NSUrl url, PHAsset asset, string originalFilename)
: base()
{
phAsset = asset;
FullPath = url?.AbsoluteString;
FileName = originalFilename;
}
internal override Task<Stream> PlatformOpenReadAsync()
{
var tcsStream = new TaskCompletionSource<Stream>();
PHImageManager.DefaultManager.RequestImageData(phAsset, null, new PHImageDataHandler((data, str, orientation, dict) =>
tcsStream.TrySetResult(data.AsStream())));
return tcsStream.Task;
}
}
}