Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active May 19, 2016 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakopappa/661348fd9d7897a86913624bba75c2b1 to your computer and use it in GitHub Desktop.
Save kakopappa/661348fd9d7897a86913624bba75c2b1 to your computer and use it in GitHub Desktop.
File based image caching for .NET WPF
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace CachedImage
{
public static class FileCache
{
private static readonly Dictionary<string, bool> IsWritingFile = new Dictionary<string, bool>();
static FileCache() {
CacheDirectory = string.Format("{0}\\{1}\\Cache\\",
System.AppDomain.CurrentDomain.BaseDirectory,
Process.GetCurrentProcess().ProcessName);
}
public static string CacheDirectory { get; set; }
/// <summary>
/// Get the image file from cache or download it if it does not exists
/// </summary>
/// <param name="url"></param>
/// <param name="sessionId"></param>
/// <returns></returns>
public static async Task<MemoryStream> GetAsync(string url, string sessionId) {
if (!Directory.Exists(CacheDirectory)) {
Directory.CreateDirectory(CacheDirectory);
}
var uri = new Uri(url);
var fileNameBuilder = new StringBuilder();
using (var sha1 = new SHA1Managed()) {
var canonicalUrl = uri.ToString();
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(canonicalUrl));
fileNameBuilder.Append(BitConverter.ToString(hash).Replace("-", "").ToLower());
if (Path.HasExtension(canonicalUrl))
fileNameBuilder.Append(Path.GetExtension(canonicalUrl));
}
var fileName = fileNameBuilder.ToString();
var localFile = string.Format("{0}\\{1}", CacheDirectory, fileName);
var memoryStream = new MemoryStream();
FileStream fileStream = null;
if (!IsWritingFile.ContainsKey(fileName) && File.Exists(localFile)) {
using (fileStream = new FileStream(localFile, FileMode.Open, FileAccess.Read)) {
await fileStream.CopyToAsync(memoryStream);
}
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
//request.Headers.Add(HttpRequestHeader.Cookie, "", ""));
request.Timeout = 30;
try {
var response = await request.GetResponseAsync();
var responseStream = response.GetResponseStream();
if (responseStream == null)
return null;
if (!IsWritingFile.ContainsKey(fileName)) {
IsWritingFile[fileName] = true;
fileStream = new FileStream(localFile, FileMode.Create, FileAccess.Write);
}
using (responseStream) {
var bytebuffer = new byte[100];
int bytesRead;
do {
bytesRead = await responseStream.ReadAsync(bytebuffer, 0, 100);
if (fileStream != null)
await fileStream.WriteAsync(bytebuffer, 0, bytesRead);
await memoryStream.WriteAsync(bytebuffer, 0, bytesRead);
} while (bytesRead > 0);
if (fileStream != null) {
await fileStream.FlushAsync();
fileStream.Dispose();
IsWritingFile.Remove(fileName);
RemoveIfEmpty(localFile);
}
}
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
catch (WebException ex) {
RemoveIfEmpty(localFile);
return null;
}
}
private static void RemoveIfEmpty(string localFile) {
var f = new FileInfo(localFile);
if (f.Exists && f.Length <= 0) {
File.Delete(localFile);
}
}
}
}
@kakopappa
Copy link
Author

Borrowed this code from another repository and improved the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment