|
|
@@ -6,33 +6,97 @@ namespace PicView.Core.Navigation;
|
|
|
public static class ErrorHelper
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// Determines the type of resource based on the provided string.
|
|
|
+ /// Represents a structure containing a loadable file type and its associated data.
|
|
|
/// </summary>
|
|
|
- /// <param name="s">The input string representing a URL, base64 data, file path, or directory path.</param>
|
|
|
+ /// <param name="type">The type of the loadable file.</param>
|
|
|
+ /// <param name="data">The data associated with the loadable file.</param>
|
|
|
+ public readonly struct FileTypeStruct(LoadAbleFileType type, string data)
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the type of the loadable file.
|
|
|
+ /// </summary>
|
|
|
+ public LoadAbleFileType Type => type;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the data associated with the loadable file.
|
|
|
+ /// </summary>
|
|
|
+ public string Data => data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Specifies the different types of loadable files.
|
|
|
+ /// </summary>
|
|
|
+ public enum LoadAbleFileType
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a regular file.
|
|
|
+ /// </summary>
|
|
|
+ File,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a directory.
|
|
|
+ /// </summary>
|
|
|
+ Directory,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a web URL.
|
|
|
+ /// </summary>
|
|
|
+ Web,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a Base64 encoded string.
|
|
|
+ /// </summary>
|
|
|
+ Base64,
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Represents a zip archive.
|
|
|
+ /// </summary>
|
|
|
+ Zip
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Checks if the provided string is a loadable file type and returns its type and associated data.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="s">The string to check.</param>
|
|
|
/// <returns>
|
|
|
- /// - If the input represents a web URL, returns "web".
|
|
|
- /// - If the input represents base64-encoded data, returns "base64".
|
|
|
- /// - If the input represents a valid file path, returns the file path.
|
|
|
- /// - If the input represents a directory path, returns "directory".
|
|
|
- /// - Otherwise, returns an empty string.
|
|
|
+ /// A <see cref="FileTypeStruct"/> containing the type and data of the loadable file if the string is loadable, otherwise null.
|
|
|
/// </returns>
|
|
|
- public static string CheckIfLoadableString(string s)
|
|
|
+ public static FileTypeStruct? CheckIfLoadableString(string s)
|
|
|
{
|
|
|
+ if (s.StartsWith('"') && s.EndsWith('"'))
|
|
|
+ {
|
|
|
+ s = s[1..^1];
|
|
|
+ }
|
|
|
+
|
|
|
if (s.StartsWith("file:///"))
|
|
|
{
|
|
|
s = s.Replace("file:///", "");
|
|
|
s = s.Replace("%20", " ");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (File.Exists(s))
|
|
|
- return Path.GetExtension(s).IsArchive() ? "zip" : s;
|
|
|
+ {
|
|
|
+ var type = s.IsArchive() ? LoadAbleFileType.Zip : LoadAbleFileType.File;
|
|
|
+ return new FileTypeStruct(type, s);
|
|
|
+ }
|
|
|
|
|
|
if (Directory.Exists(s))
|
|
|
- return "directory";
|
|
|
+ {
|
|
|
+ return new FileTypeStruct(LoadAbleFileType.Directory, s);
|
|
|
+ }
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(s.GetURL()))
|
|
|
- return "web";
|
|
|
+ {
|
|
|
+ return new FileTypeStruct(LoadAbleFileType.Web, s);
|
|
|
+ }
|
|
|
+
|
|
|
+ var base64String = Base64Helper.IsBase64String(s);
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(base64String))
|
|
|
+ {
|
|
|
+ return new FileTypeStruct(LoadAbleFileType.Base64, base64String);
|
|
|
+ }
|
|
|
|
|
|
- return Base64Helper.IsBase64String(s) ? "base64" : string.Empty;
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|