Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active December 5, 2017 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leekelleher/705508006273850ff6e3e03c779d67b1 to your computer and use it in GitHub Desktop.
Save leekelleher/705508006273850ff6e3e03c779d67b1 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using ImageProcessor.Web.Services;
namespace UmbrellaInc.Web.Services
{
public class LocalFileRemoteFallbackImageService : LocalFileImageService
{
public override async Task<byte[]> GetImage(object id)
{
try
{
return await base.GetImage(id);
}
catch (HttpException ex)
{
if (ex.GetHttpCode() != 404)
{
throw ex;
}
}
var path = (string)id;
var host = this.Settings["Host"];
var relativePath = path.Replace(HostingEnvironment.ApplicationPhysicalPath, string.Empty);
var uri = new Uri(new Uri(host), relativePath);
var dir = Path.GetDirectoryName(path);
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
using (var client = new WebClient())
{
if (uri.Scheme == Uri.UriSchemeHttps && ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}
client.DownloadFile(uri, path);
}
return await base.GetImage(id);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<security>
<services>
<service name="LocalFileRemoteFallbackImageService" type="UmbrellaInc.Web.Services.LocalFileRemoteFallbackImageService, UmbrellaInc.Web">
<settings>
<setting key="Host" value="https://yourhost.com/" />
</settings>
</service>
</services>
</security>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment