Esempio n. 1
0
        private bool SelfDownloadImage(IllustrationInfo info, ImageSize size, NameType type, string dir)
        {
            if (info == null || info.imageUrls == null || info.imageUrls.Length == 0)
            {
                return(false);
            }
            string downloadUrl = size switch
            {
                ImageSize.SquareMedium => info.imageUrls[0].squareMedium,
                ImageSize.Medium => info.imageUrls[0].medium,
                ImageSize.Large => info.imageUrls[0].large,
                ImageSize.Original => info.imageUrls[0].original,
                _ => null,
            };
            string name = type switch
            {
                NameType.ById => info.id + Path.GetExtension(downloadUrl),
                NameType.ByUrl => Path.GetFileName(downloadUrl),
                NameType.ByTitle => info.title + Path.GetExtension(downloadUrl),
                NameType.ByArtistAndTitle => info.artistPreView.name + info.title + Path.GetExtension(downloadUrl),
                _ => null,
            };
            string path = Path.Combine(dir, name);

            try { _ = new FileInfo(path); } catch (Exception) { return(false); }
            ProcessUrl(ref downloadUrl);
            using (FileStream fs = File.OpenWrite(path))
            {
                HttpWebRequest request = HttpHelper.CreateGetRequest(downloadUrl);
                using Stream stream = HttpHelper.GetResponseStream(request);
                stream.CopyTo(fs);
            }
            return(true);
        }
Esempio n. 2
0
 public bool DownloadImage(IllustrationInfo info, ImageSize size, NameType type, string dir)
 {
     if (!Directory.Exists(dir))
     {
         return(false);
     }
     return(SelfDownloadImage(info, size, type, dir));
 }