-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Formatted Storage (again) #8963
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e527ced
storage
cinqmilleans 11f4026
remove FilesystemErrorCodeExtensions
cinqmilleans 114a71e
Merge branch 'main' into Storage
cinqmilleans 6187742
Merge branch 'main' into Storage
cinqmilleans 625c2b5
Merge branch 'files-community:main' into Storage
cinqmilleans 0d666a4
fix merge #9089
cinqmilleans e4bcc58
Merge branch 'main' into Storage
cinqmilleans 0edb895
fix merge
cinqmilleans cc792c1
Merge branch 'main' of https://github.com/files-community/Files into …
gave92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
143 changes: 9 additions & 134 deletions
143
src/Files.Uwp/Filesystem/StorageFileHelpers/FilesystemResult.cs
This file contains hidden or 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 |
---|---|---|
@@ -1,151 +1,26 @@ | ||
using Files.Shared.Enums; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
|
||
namespace Files.Uwp.Filesystem | ||
{ | ||
public static class FilesystemErrorCodeExtensions | ||
{ | ||
public static bool HasFlag(this FileSystemStatusCode errorCode, FileSystemStatusCode flag) | ||
{ | ||
return (errorCode & flag) != FileSystemStatusCode.Success; | ||
} | ||
} | ||
|
||
public class FilesystemResult<T> : FilesystemResult | ||
{ | ||
public FilesystemResult(T result, FileSystemStatusCode errorCode) : base(errorCode) | ||
{ | ||
this.Result = result; | ||
} | ||
|
||
public T Result { get; private set; } | ||
|
||
public static implicit operator T(FilesystemResult<T> res) => res.Result; | ||
} | ||
|
||
public class FilesystemResult | ||
{ | ||
public FilesystemResult(FileSystemStatusCode errorCode) | ||
{ | ||
this.ErrorCode = errorCode; | ||
} | ||
public FileSystemStatusCode ErrorCode { get; } | ||
|
||
public FileSystemStatusCode ErrorCode { get; private set; } | ||
public FilesystemResult(FileSystemStatusCode errorCode) => ErrorCode = errorCode; | ||
|
||
public static implicit operator FileSystemStatusCode(FilesystemResult res) => res.ErrorCode; | ||
public static implicit operator FilesystemResult(FileSystemStatusCode res) => new(res); | ||
|
||
public static implicit operator FilesystemResult(FileSystemStatusCode res) => new FilesystemResult(res); | ||
|
||
public static implicit operator bool(FilesystemResult res) => | ||
res != null && res.ErrorCode == FileSystemStatusCode.Success; | ||
|
||
public static explicit operator FilesystemResult(bool res) => | ||
new FilesystemResult(res ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic); | ||
public static implicit operator bool(FilesystemResult res) => res is not null && res.ErrorCode is FileSystemStatusCode.Success; | ||
public static explicit operator FilesystemResult(bool res) => new(res ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic); | ||
} | ||
|
||
public static class FilesystemTasks | ||
public class FilesystemResult<T> : FilesystemResult | ||
{ | ||
public static FileSystemStatusCode GetErrorCode(Exception ex, Type T = null) | ||
{ | ||
if (ex is UnauthorizedAccessException) | ||
{ | ||
return FileSystemStatusCode.Unauthorized; | ||
} | ||
else if (ex is FileNotFoundException // Item was deleted | ||
|| ex is System.Runtime.InteropServices.COMException // Item's drive was ejected | ||
|| (uint)ex.HResult == 0x8007000F) // The system cannot find the drive specified | ||
{ | ||
return FileSystemStatusCode.NotFound; | ||
} | ||
else if (ex is PathTooLongException) | ||
{ | ||
return FileSystemStatusCode.NameTooLong; | ||
} | ||
else if (ex is IOException || ex is FileLoadException) | ||
{ | ||
return FileSystemStatusCode.InUse; | ||
} | ||
else if (ex is ArgumentException) // Item was invalid | ||
{ | ||
return (typeof(IStorageFolder).IsAssignableFrom(T) || T == typeof(StorageFolderWithPath)) ? | ||
FileSystemStatusCode.NotAFolder : FileSystemStatusCode.NotAFile; | ||
} | ||
else if ((uint)ex.HResult == 0x800700B7) | ||
{ | ||
return FileSystemStatusCode.AlreadyExists; | ||
} | ||
else if ((uint)ex.HResult == 0x80071779) | ||
{ | ||
return FileSystemStatusCode.ReadOnly; | ||
} | ||
else if ((uint)ex.HResult == 0x800700A1 // The specified path is invalid (usually an mtp device was disconnected) | ||
|| (uint)ex.HResult == 0x8007016A // The cloud file provider is not running | ||
|| (uint)ex.HResult == 0x8000000A) // The data necessary to complete this operation is not yet available) | ||
{ | ||
return FileSystemStatusCode.Generic; | ||
} | ||
else | ||
{ | ||
return FileSystemStatusCode.Generic; | ||
} | ||
} | ||
public T Result { get; } | ||
|
||
public async static Task<FilesystemResult<T>> Wrap<T>(Func<Task<T>> wrapped) | ||
{ | ||
try | ||
{ | ||
return new FilesystemResult<T>(await wrapped(), FileSystemStatusCode.Success); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new FilesystemResult<T>(default, GetErrorCode(ex, typeof(T))); | ||
} | ||
} | ||
public FilesystemResult(T result, FileSystemStatusCode errorCode) : base(errorCode) => Result = result; | ||
|
||
public async static Task<FilesystemResult> Wrap(Func<Task> wrapped) | ||
{ | ||
try | ||
{ | ||
await wrapped(); | ||
return new FilesystemResult(FileSystemStatusCode.Success); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new FilesystemResult(GetErrorCode(ex)); | ||
} | ||
} | ||
|
||
public async static Task<FilesystemResult<V>> OnSuccess<V, T>(this Task<FilesystemResult<T>> wrapped, Func<T, Task<V>> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
return await Wrap(() => func(res.Result)); | ||
} | ||
return new FilesystemResult<V>(default, res.ErrorCode); | ||
} | ||
|
||
public async static Task<FilesystemResult> OnSuccess<T>(this Task<FilesystemResult<T>> wrapped, Func<T, Task> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
return await Wrap(() => func(res.Result)); | ||
} | ||
return res; | ||
} | ||
|
||
public async static Task<FilesystemResult> OnSuccess<T>(this Task<FilesystemResult<T>> wrapped, Action<T> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
func(res.Result); | ||
} | ||
return res; | ||
} | ||
public static implicit operator T(FilesystemResult<T> res) => res.Result; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Files.Uwp/Filesystem/StorageFileHelpers/FilesystemTasks.cs
This file contains hidden or 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,83 @@ | ||
using Files.Shared.Enums; | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
using Windows.Storage; | ||
|
||
namespace Files.Uwp.Filesystem | ||
{ | ||
public static class FilesystemTasks | ||
{ | ||
public static FileSystemStatusCode GetErrorCode(Exception ex, Type T = null) => (ex, (uint)ex.HResult) switch | ||
{ | ||
(UnauthorizedAccessException, _) => FileSystemStatusCode.Unauthorized, | ||
(FileNotFoundException, _) => FileSystemStatusCode.NotFound, // Item was deleted | ||
(COMException, _) => FileSystemStatusCode.NotFound, // Item's drive was ejected | ||
(_, 0x8007000F) => FileSystemStatusCode.NotFound, // The system cannot find the drive specified | ||
(PathTooLongException, _) => FileSystemStatusCode.NameTooLong, | ||
(IOException, _) => FileSystemStatusCode.InUse, | ||
(ArgumentException, _) => ToStatusCode(T), // Item was invalid | ||
(_, 0x800700B7) => FileSystemStatusCode.AlreadyExists, | ||
(_, 0x80071779) => FileSystemStatusCode.ReadOnly, | ||
_ => FileSystemStatusCode.Generic, | ||
}; | ||
|
||
public async static Task<FilesystemResult> Wrap(Func<Task> wrapped) | ||
{ | ||
try | ||
{ | ||
await wrapped(); | ||
return new FilesystemResult(FileSystemStatusCode.Success); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new FilesystemResult(GetErrorCode(ex)); | ||
} | ||
} | ||
public async static Task<FilesystemResult<T>> Wrap<T>(Func<Task<T>> wrapped) | ||
{ | ||
try | ||
{ | ||
return new FilesystemResult<T>(await wrapped(), FileSystemStatusCode.Success); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new FilesystemResult<T>(default, GetErrorCode(ex, typeof(T))); | ||
} | ||
} | ||
|
||
public async static Task<FilesystemResult> OnSuccess<T>(this Task<FilesystemResult<T>> wrapped, Action<T> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
func(res.Result); | ||
} | ||
return res; | ||
} | ||
public async static Task<FilesystemResult> OnSuccess<T>(this Task<FilesystemResult<T>> wrapped, Func<T, Task> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
return await Wrap(() => func(res.Result)); | ||
} | ||
return res; | ||
} | ||
public async static Task<FilesystemResult<V>> OnSuccess<V, T>(this Task<FilesystemResult<T>> wrapped, Func<T, Task<V>> func) | ||
{ | ||
var res = await wrapped; | ||
if (res) | ||
{ | ||
return await Wrap(() => func(res.Result)); | ||
} | ||
return new FilesystemResult<V>(default, res.ErrorCode); | ||
} | ||
|
||
private static FileSystemStatusCode ToStatusCode(Type T) | ||
=> T == typeof(StorageFolderWithPath) || typeof(IStorageFolder).IsAssignableFrom(T) | ||
? FileSystemStatusCode.NotAFolder | ||
: FileSystemStatusCode.NotAFile; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.