Last active
December 5, 2017 16:05
-
-
Save leekelleher/705508006273850ff6e3e03c779d67b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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