-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
410 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.IO; | ||
|
||
using Foundation; | ||
|
||
using UIKit; | ||
|
||
#nullable enable | ||
|
||
namespace Avalonia.iOS.Storage | ||
{ | ||
internal class IOSSecurityScopedStream : Stream | ||
{ | ||
private readonly UIDocument _document; | ||
private readonly FileStream _stream; | ||
private readonly NSUrl _url; | ||
|
||
internal IOSSecurityScopedStream(NSUrl url, FileAccess access) | ||
{ | ||
_document = new UIDocument(url); | ||
var path = _document.FileUrl.Path; | ||
_stream = File.Open(path, FileMode.Open, access); | ||
_url = url; | ||
_url.StartAccessingSecurityScopedResource(); | ||
} | ||
|
||
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(); | ||
_document.Dispose(); | ||
_url.StopAccessingSecurityScopedResource(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Avalonia.Logging; | ||
using Avalonia.Platform.Storage; | ||
using Foundation; | ||
|
||
using UIKit; | ||
|
||
#nullable enable | ||
|
||
namespace Avalonia.iOS.Storage | ||
{ | ||
internal class IOSStorageItem : IStorageBookmarkItem | ||
{ | ||
private readonly NSUrl _url; | ||
private readonly string _filePath; | ||
|
||
public IOSStorageItem(NSUrl url) | ||
{ | ||
_url = url; | ||
|
||
using (var doc = new UIDocument(url)) | ||
{ | ||
_filePath = doc.FileUrl?.Path ?? url.FilePathUrl.Path; | ||
Name = doc.LocalizedName ?? Path.GetFileName(_filePath) ?? url.FilePathUrl.LastPathComponent; | ||
} | ||
} | ||
|
||
internal NSUrl Url => _url; | ||
|
||
public bool CanBookmark => true; | ||
|
||
public string Name { get; } | ||
|
||
public Task<StorageItemProperties> GetBasicPropertiesAsync() | ||
{ | ||
var attributes = NSFileManager.DefaultManager.GetAttributes(_filePath, out var error); | ||
if (error is not null) | ||
{ | ||
Logger.TryGet(LogEventLevel.Error, LogArea.IOSPlatform)?. | ||
Log(this, "GetBasicPropertiesAsync returned an error: {ErrorCode} {ErrorMessage}", error.Code, error.LocalizedFailureReason); | ||
} | ||
return Task.FromResult(new StorageItemProperties(attributes?.Size, (DateTime)attributes?.CreationDate, (DateTime)attributes?.ModificationDate)); | ||
} | ||
|
||
public Task<IStorageFolder?> GetParentAsync() | ||
{ | ||
return Task.FromResult<IStorageFolder?>(new IOSStorageFolder(_url.RemoveLastPathComponent())); | ||
} | ||
|
||
public Task<string?> SaveBookmark() | ||
{ | ||
try | ||
{ | ||
_url.StartAccessingSecurityScopedResource(); | ||
|
||
var newBookmark = _url.CreateBookmarkData(0, Array.Empty<string>(), null, out var bookmarkError); | ||
if (bookmarkError is not null) | ||
{ | ||
Logger.TryGet(LogEventLevel.Error, LogArea.IOSPlatform)?. | ||
Log(this, "SaveBookmark returned an error: {ErrorCode} {ErrorMessage}", bookmarkError.Code, bookmarkError.LocalizedFailureReason); | ||
return Task.FromResult<string?>(null); | ||
} | ||
|
||
return Task.FromResult<string?>( | ||
newBookmark.GetBase64EncodedString(NSDataBase64EncodingOptions.None)); | ||
} | ||
finally | ||
{ | ||
_url.StopAccessingSecurityScopedResource(); | ||
} | ||
} | ||
|
||
public bool TryGetFullPath([NotNullWhen(true)] out string path) | ||
{ | ||
path = _filePath; | ||
return true; | ||
} | ||
} | ||
|
||
internal class IOSStorageFile : IOSStorageItem, IStorageBookmarkFile | ||
{ | ||
public IOSStorageFile(NSUrl url) : base(url) | ||
{ | ||
} | ||
|
||
public bool CanOpenRead => true; | ||
|
||
public bool CanOpenWrite => true; | ||
|
||
public Task<Stream> OpenRead() | ||
{ | ||
return Task.FromResult<Stream>(new IOSSecurityScopedStream(Url, FileAccess.Read)); | ||
} | ||
|
||
public Task<Stream> OpenWrite() | ||
{ | ||
return Task.FromResult<Stream>(new IOSSecurityScopedStream(Url, FileAccess.Write)); | ||
} | ||
} | ||
|
||
internal class IOSStorageFolder : IOSStorageItem, IStorageBookmarkFolder | ||
{ | ||
public IOSStorageFolder(NSUrl url) : base(url) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.