private async Task <byte[]> GetImageDataAsync(string hash, IdolImageDataType type) { var u = IdolImageData.GetImageUrl(hash, type); using (var wc = new WebClient()) { try { wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0"; return(await wc.DownloadDataTaskAsync(u)); } catch { return(null); } } }
public async Task <HttpResponseMessage> GetImageAsync(string hash, IdolImageDataType type) { using (var db = new ImasCDDbContext()) { var d = await db.IdolImageData.FirstOrDefaultAsync(_ => _.Hash == hash && _.Type == type); if (d != null) { if (d.Data == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(CreateFileResult(d.Data, "image/jpeg")); } var u = IdolImageData.GetImageUrl(hash, type); byte[] data; using (var wc = new WebClient()) { try { data = await wc.DownloadDataTaskAsync(u); } catch { data = null; } } db.IdolImageData.Add(new IdolImageData() { Hash = hash, Type = type, Data = data }); await db.SaveChangesAsync(); if (data == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(CreateFileResult(data, "image/jpeg")); } }