| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.IO;
- using System.Security;
- using System.Threading.Tasks;
- namespace Avalonia.Platform.Storage.FileIO;
- internal class BclStorageFile : IStorageBookmarkFile
- {
- public BclStorageFile(string fileName)
- {
- FileInfo = new FileInfo(fileName);
- }
- public BclStorageFile(FileInfo fileInfo)
- {
- FileInfo = fileInfo ?? throw new ArgumentNullException(nameof(fileInfo));
- }
- public FileInfo FileInfo { get; }
-
- public string Name => FileInfo.Name;
- public virtual bool CanBookmark => true;
- public Uri Path
- {
- get
- {
- try
- {
- if (FileInfo.Directory is not null)
- {
- return StorageProviderHelpers.FilePathToUri(FileInfo.FullName);
- }
- }
- catch (SecurityException)
- {
- }
- return new Uri(FileInfo.Name, UriKind.Relative);
- }
- }
-
- public Task<StorageItemProperties> GetBasicPropertiesAsync()
- {
- if (FileInfo.Exists)
- {
- return Task.FromResult(new StorageItemProperties(
- (ulong)FileInfo.Length,
- FileInfo.CreationTimeUtc,
- FileInfo.LastAccessTimeUtc));
- }
- return Task.FromResult(new StorageItemProperties());
- }
- public Task<IStorageFolder?> GetParentAsync()
- {
- if (FileInfo.Directory is { } directory)
- {
- return Task.FromResult<IStorageFolder?>(new BclStorageFolder(directory));
- }
- return Task.FromResult<IStorageFolder?>(null);
- }
- public Task<Stream> OpenReadAsync()
- {
- return Task.FromResult<Stream>(FileInfo.OpenRead());
- }
- public Task<Stream> OpenWriteAsync()
- {
- return Task.FromResult<Stream>(FileInfo.OpenWrite());
- }
- public virtual Task<string?> SaveBookmarkAsync()
- {
- return Task.FromResult<string?>(FileInfo.FullName);
- }
- public Task ReleaseBookmarkAsync()
- {
- // No-op
- return Task.CompletedTask;
- }
- protected virtual void Dispose(bool disposing)
- {
- }
- ~BclStorageFile()
- {
- Dispose(disposing: false);
- }
- public void Dispose()
- {
- Dispose(disposing: true);
- GC.SuppressFinalize(this);
- }
- }
|