| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using PicView.Core.FileHandling;
- using PicView.Core.ImageDecoding;
- namespace PicView.Core.Navigation;
- public static class ErrorHelper
- {
- /// <summary>
- /// Represents a structure containing a loadable file type and its associated data.
- /// </summary>
- /// <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>
- /// A <see cref="FileTypeStruct"/> containing the type and data of the loadable file if the string is loadable, otherwise null.
- /// </returns>
- 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))
- {
- var type = s.IsArchive() ? LoadAbleFileType.Zip : LoadAbleFileType.File;
- return new FileTypeStruct(type, s);
- }
- if (Directory.Exists(s))
- {
- return new FileTypeStruct(LoadAbleFileType.Directory, s);
- }
- if (!string.IsNullOrWhiteSpace(s.GetURL()))
- {
- return new FileTypeStruct(LoadAbleFileType.Web, s);
- }
- var base64String = Base64Helper.IsBase64String(s);
- if (!string.IsNullOrEmpty(base64String))
- {
- return new FileTypeStruct(LoadAbleFileType.Base64, base64String);
- }
- return null;
- }
- }
|