| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.IO; |
| 4 | using System.Linq; |
| 5 | using System.Net; |
| 6 | using System.Text; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | namespace Core.Librarys.Browser.Favicon |
| 10 | { |
| 11 | public class FaviconDownloader |
| 12 | { |
| 13 | ///<summary> |
| 14 | /// |
| 15 | ///</summary> |
| 16 | ///<param name="url_"></param> |
| 17 | ///<param name="savePath_"></param> |
| 18 | public static async Task<string> DownloadAsync(string url_, string saveName_) |
| 19 | { |
| 20 | if (string.IsNullOrEmpty(url_)) return string.Empty; |
| 21 | |
| 22 | string savePath = Path.Combine(FileHelper.GetRootDirectory(), "WebFavicons", saveName_); |
| 23 | string dir = Path.GetDirectoryName(savePath); |
| 24 | |
| 25 | url_ = url_.Replace(".svg", ".png"); |
| 26 | |
| 27 | if (!Directory.Exists(dir)) |
| 28 | { |
| 29 | Directory.CreateDirectory(dir); |
| 30 | } |
| 31 | if (File.Exists(savePath)) |
| 32 | { |
| 33 | return savePath; |
| 34 | } |
| 35 | try |
| 36 | { |
| 37 | using (var web = new WebClient()) |
| 38 | { |
| 39 | await web.DownloadFileTaskAsync(url_, savePath); |
| 40 | return savePath; |
| 41 | } |
| 42 | } |
| 43 | catch (Exception e) |
| 44 | { |
| 45 | Logger.Error("下载图标失败," + e); |
| 46 | return string.Empty; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |