Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 20 additions & 4 deletions src/Essentials/src/FileSystem/FileSystem.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ namespace Microsoft.Maui.Storage
{
partial class FileSystemImplementation : IFileSystem
{
private readonly string _platformAppDataDirectory;

public FileSystemImplementation()
{
if (AppInfoUtils.IsPackagedApp)
{
_platformAppDataDirectory = ApplicationData.Current.LocalFolder.Path;
}
else
{
_platformAppDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Data");

if (!File.Exists(_platformAppDataDirectory))
{
Directory.CreateDirectory(_platformAppDataDirectory);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not great that this method can throw an exception in the constructor.

}
}
}

static string CleanPath(string path) =>
string.Join("_", path.Split(Path.GetInvalidFileNameChars()));

Expand All @@ -20,10 +39,7 @@ string PlatformCacheDirectory
? ApplicationData.Current.LocalCacheFolder.Path
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppSpecificPath, "Cache");

string PlatformAppDataDirectory
=> AppInfoUtils.IsPackagedApp
? ApplicationData.Current.LocalFolder.Path
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), AppSpecificPath, "Data");
string PlatformAppDataDirectory => _platformAppDataDirectory;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of moving the logic the constructor, it might be better to do all the checks and creating in this property? So this property will throw vs the ctor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then you would have some perf penalty on every access. Would that be fine or not?

Ah you probably meant using Lazy. That might be a viable way indeed.


Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
Expand Down
24 changes: 10 additions & 14 deletions src/Essentials/test/DeviceTests/Tests/FileSystem_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Maui.Essentials.DeviceTests
[Category("FileSystem")]
public class FileSystem_Tests
{
const string bundleFileContents = "This file was in the app bundle.";
const string BundleFileContents = "This file was in the app bundle.";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just code style changes in this file.


[Fact]
public void CacheDirectory_Is_Valid()
Expand All @@ -23,23 +23,19 @@ public void AppDataDirectory_Is_Valid()
}

[Theory]
[InlineData("AppBundleFile.txt", bundleFileContents)]
[InlineData("AppBundleFile_NoExtension", bundleFileContents)]
[InlineData("Folder/AppBundleFile_Nested.txt", bundleFileContents)]
[InlineData("Folder\\AppBundleFile_Nested.txt", bundleFileContents)]
[InlineData("AppBundleFile.txt", BundleFileContents)]
[InlineData("AppBundleFile_NoExtension", BundleFileContents)]
[InlineData("Folder/AppBundleFile_Nested.txt", BundleFileContents)]
[InlineData("Folder\\AppBundleFile_Nested.txt", BundleFileContents)]
public async Task OpenAppPackageFileAsync_Can_Load_File(string filename, string contents)
{
using (var stream = await FileSystem.OpenAppPackageFileAsync(filename))
{
Assert.NotNull(stream);
using var stream = await FileSystem.OpenAppPackageFileAsync(filename);
Assert.NotNull(stream);

using (var reader = new StreamReader(stream))
{
var text = await reader.ReadToEndAsync();
using var reader = new StreamReader(stream);
var text = await reader.ReadToEndAsync();

Assert.Equal(contents, text);
}
}
Assert.Equal(contents, text);
}

[Fact]
Expand Down