Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Essentials.FileSystem refactored to interface and singleton #4633

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Core/src/Fonts/FontRegistrar.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#nullable enable
using System.IO;
using Microsoft.Maui.Essentials;
using Microsoft.Maui.Essentials.Implementations;

namespace Microsoft.Maui
{
public partial class FontRegistrar : IFontRegistrar
{
string? LoadNativeAppFont(string font, string filename, string? alias)
{
if (FileSystem.AppPackageFileExists(filename))
if (FileSystemImplementation.AppPackageFileExists(filename))
return $"ms-appx:///{filename}";

var packagePath = Path.Combine("Assets", filename);
if (FileSystem.AppPackageFileExists(packagePath))
if (FileSystemImplementation.AppPackageFileExists(packagePath))
return $"ms-appx:///Assets/{filename}";

packagePath = Path.Combine("Fonts", filename);
if (FileSystem.AppPackageFileExists(packagePath))
if (FileSystemImplementation.AppPackageFileExists(packagePath))
return $"ms-appx:///Fonts/{filename}";

packagePath = Path.Combine("Assets", "Fonts", filename);
if (FileSystem.AppPackageFileExists(packagePath))
if (FileSystemImplementation.AppPackageFileExists(packagePath))
return $"ms-appx:///Assets/Fonts/{filename}";

// TODO: check other folders as well
Expand Down
5 changes: 4 additions & 1 deletion src/Essentials/src/Email/Email.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task ComposeAsync(EmailMessage message)
{
foreach (var attachment in message.Attachments)
{
var path = FileSystem.NormalizePath(attachment.FullPath);
var path = NormalizePath(attachment.FullPath);
var file = attachment.File ?? await StorageFile.GetFileFromPathAsync(path);

var stream = RandomAccessStreamReference.CreateFromFile(file);
Expand All @@ -53,6 +53,9 @@ public async Task ComposeAsync(EmailMessage message)
await EmailManager.ShowComposeNewEmailAsync(platformEmailMessage);
}

internal static string NormalizePath(string path)
=> path.Replace('/', Path.DirectorySeparatorChar);

public Task ComposeAsync(string subject, string body, params string[] to)
=> ComposeAsync(
new EmailMessage()
Expand Down
31 changes: 18 additions & 13 deletions src/Essentials/src/FileSystem/FileSystem.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using Android.Webkit;
using AndroidUri = Android.Net.Uri;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class FileSystem
public partial class FileSystemImplementation : IFileSystem, IPlatformFileSystem
{
internal const string EssentialsFolderHash = "2203693cc04e0be7f4f024d5f9499e13";

Expand All @@ -31,16 +31,16 @@ public partial class FileSystem
internal const string UriAuthorityDownloads = "com.android.providers.downloads.documents";
internal const string UriAuthorityMedia = "com.android.providers.media.documents";

static string PlatformCacheDirectory
string PlatformCacheDirectory
=> Platform.AppContext.CacheDir.AbsolutePath;

static string PlatformAppDataDirectory
string PlatformAppDataDirectory
=> Platform.AppContext.FilesDir.AbsolutePath;

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename) =>
Task<Stream> PlatformOpenAppPackageFileAsync(string filename) =>
Task.FromResult(PlatformOpenAppPackageFile(filename));

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
try
{
Expand All @@ -53,7 +53,7 @@ static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
}
}

static Stream PlatformOpenAppPackageFile(string filename)
Stream PlatformOpenAppPackageFile(string filename)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));
Expand All @@ -73,7 +73,7 @@ static Stream PlatformOpenAppPackageFile(string filename)
static string NormalizePath(string filename) =>
filename.Replace('\\', Path.DirectorySeparatorChar);

internal static Java.IO.File GetEssentialsTemporaryFile(Java.IO.File root, string fileName)
public Java.IO.File GetTemporaryFile(Java.IO.File root, string fileName)
{
// create the directory for all Essentials files
var rootDir = new Java.IO.File(root, EssentialsFolderHash);
Expand All @@ -92,7 +92,8 @@ internal static Java.IO.File GetEssentialsTemporaryFile(Java.IO.File root, strin
return tmpFile;
}

internal static string EnsurePhysicalPath(AndroidUri uri, bool requireExtendedAccess = true)

public string EnsurePhysicalPath(AndroidUri uri, bool requireExtendedAccess = true)
{
// if this is a file, use that
if (uri.Scheme.Equals(UriSchemeFile, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -245,7 +246,7 @@ static string ResolveContentPath(AndroidUri uri)
return null;
}

static string CacheContentFile(AndroidUri uri)
string CacheContentFile(AndroidUri uri)
{
if (!uri.Scheme.Equals(UriSchemeContent, StringComparison.OrdinalIgnoreCase))
return null;
Expand All @@ -270,7 +271,7 @@ static string CacheContentFile(AndroidUri uri)
var root = hasPermission
? Platform.AppContext.ExternalCacheDir
: Platform.AppContext.CacheDir;
var tmpFile = GetEssentialsTemporaryFile(root, filename);
var tmpFile = GetTemporaryFile(root, filename);

// copy to the destination
using var dstStream = File.Create(tmpFile.CanonicalPath);
Expand Down Expand Up @@ -381,19 +382,23 @@ static string QueryContentResolverColumn(AndroidUri contentUri, string columnNam

return text;
}

}
}

namespace Microsoft.Maui.Essentials
{
public partial class FileBase
{
internal FileBase(Java.IO.File file)
: this(file?.Path)
{
}

internal static string PlatformGetContentType(string extension) =>
string PlatformGetContentType(string extension) =>
MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension.TrimStart('.'));

internal void PlatformInit(FileBase file)
void PlatformInit(FileBase file)
{
}

Expand Down
9 changes: 6 additions & 3 deletions src/Essentials/src/FileSystem/FileSystem.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
using Photos;
using UIKit;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class FileSystem
public partial class FileSystemImplementation : IFileSystem, IPlatformFileSystem
{
internal static async Task<FileResult[]> EnsurePhysicalFileResultsAsync(params NSUrl[] urls)
public async Task<FileResult[]> EnsurePhysicalFileResultsAsync(params NSUrl[] urls)
{
if (urls == null || urls.Length == 0)
return Array.Empty<FileResult>();
Expand Down Expand Up @@ -47,7 +47,10 @@ internal static async Task<FileResult[]> EnsurePhysicalFileResultsAsync(params N
return await tcs.Task;
}
}
}

namespace Microsoft.Maui.Essentials
{
class BookmarkDataFileResult : FileResult
{
NSData bookmark;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
using Foundation;
using MobileCoreServices;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public static partial class FileSystem
public partial class FileSystemImplementation : IFileSystem
{
static string PlatformCacheDirectory
string PlatformCacheDirectory
=> GetDirectory(NSSearchPathDirectory.CachesDirectory);

static string PlatformAppDataDirectory
string PlatformAppDataDirectory
=> GetDirectory(NSSearchPathDirectory.LibraryDirectory);

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult((Stream)File.OpenRead(file));
}

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult(File.Exists(file));
Expand Down Expand Up @@ -54,7 +54,10 @@ static string GetDirectory(NSSearchPathDirectory directory)
return dirs[0];
}
}
}

namespace Microsoft.Maui.Essentials
{
public partial class FileBase
{
internal FileBase(NSUrl file)
Expand All @@ -63,7 +66,7 @@ internal FileBase(NSUrl file)
FileName = NSFileManager.DefaultManager.DisplayName(file?.Path);
}

internal static string PlatformGetContentType(string extension)
string PlatformGetContentType(string extension)
{
// ios does not like the extensions
extension = extension?.TrimStart('.');
Expand All @@ -75,7 +78,7 @@ internal static string PlatformGetContentType(string extension)
return extension;
}

internal void PlatformInit(FileBase file)
void PlatformInit(FileBase file)
{
}

Expand Down
20 changes: 13 additions & 7 deletions src/Essentials/src/FileSystem/FileSystem.netstandard.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class FileSystemImplementation : IFileSystem
/// <include file="../../docs/Microsoft.Maui.Essentials/FileSystem.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FileSystem']/Docs" />
public static partial class FileSystem
{
static string PlatformCacheDirectory
string PlatformCacheDirectory
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static string PlatformAppDataDirectory
string PlatformAppDataDirectory
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
Task<bool> PlatformAppPackageFileExistsAsync(string filename)
=> throw ExceptionUtils.NotSupportedOrImplementedException;
}
}

namespace Microsoft.Maui.Essentials
{
/// <include file="../../docs/Microsoft.Maui.Essentials/FileBase.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FileBase']/Docs" />
public partial class FileBase
{
static string PlatformGetContentType(string extension) =>
throw ExceptionUtils.NotSupportedOrImplementedException;

internal void PlatformInit(FileBase file) =>
internal void Init(FileBase file) =>
throw ExceptionUtils.NotSupportedOrImplementedException;

internal virtual Task<Stream> PlatformOpenReadAsync()
=> throw ExceptionUtils.NotSupportedOrImplementedException;

void PlatformInit(FileBase file)
=> throw ExceptionUtils.NotSupportedOrImplementedException;
}
}
Loading