using System; using System.IO; using System.Net; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; namespace Gallery.Util { public static class Store { public static readonly string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); public static readonly string CacheFolder = FileSystem.CacheDirectory; private const string imageFolder = "img-original"; private const string previewFolder = "img-preview"; public static async Task LoadRawImage(string url) { return await LoadImageAsync(url, null, PersonalFolder, imageFolder, force: true); } public static async Task LoadPreviewImage(string url, bool downloading, bool force = false) { return await LoadImage(url, CacheFolder, previewFolder, downloading, force: force); } private static async Task LoadImage(string url, string working, string folder, bool downloading, bool force = false) { var file = Path.Combine(working, folder, Path.GetFileName(url)); ImageSource image; if (!force && File.Exists(file)) { image = ImageSource.FromFile(file); } else { image = null; } if (downloading && image == null) { file = await NetHelper.DownloadImage(url, working, folder); if (file != null) { return ImageSource.FromFile(file); } } return image; } private static async Task LoadImageAsync(string url, string id, string working, string folder, bool force = false) { var file = Path.Combine(working, folder, Path.GetFileName(url)); ImageSource image; if (!force && File.Exists(file)) { image = ImageSource.FromFile(file); } else { image = null; } if (image == null) { file = await NetHelper.DownloadImageAsync(url, id, working, folder); if (file != null) { image = ImageSource.FromFile(file); } } return image; } } public static class Config { public static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30); public const string IsProxiedKey = "is_proxied"; public const string ProxyHostKey = "proxy_host"; public const string ProxyPortKey = "proxy_port"; public const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"; public const string AcceptLanguage = "zh-cn"; public const string AcceptImage = "image/png,image/*,*/*;q=0.8"; public static WebProxy Proxy; } public static class Routes { public const string Gallery = "gallery"; public const string Option = "option"; } }