Created
October 10, 2012 09:39
-
-
Save leekelleher/3864395 to your computer and use it in GitHub Desktop.
ImageGenFetch - when taking a copy of an Umbraco site from a production server, the /media directory can be a huge download. So if using ImageGen you can auto-download the /media images on-demand.
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.Net.Mime; | |
using System.Text; | |
using System.Web; | |
namespace Umbrella.ImageGenFetch | |
{ | |
public class CheckImageGenModule : IHttpModule | |
{ | |
public void Dispose() { } | |
public void Init(HttpApplication context) | |
{ | |
context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute); | |
} | |
private void context_PostRequestHandlerExecute(object sender, EventArgs e) | |
{ | |
if (HttpContext.Current == null) | |
return; | |
var context = HttpContext.Current; | |
var request = context.Request; | |
var currentExecutionFilePath = request.CurrentExecutionFilePath; | |
if (!string.Equals(currentExecutionFilePath, "/ImageGen.ashx", StringComparison.OrdinalIgnoreCase)) | |
return; | |
var response = context.Response; | |
if (response.ContentType != MediaTypeNames.Text.Html) | |
return; | |
var image = request.QueryString["image"]; | |
if (string.IsNullOrWhiteSpace(image)) | |
return; | |
response.Filter = new FetchRemoteImage(response.Filter, image, context.Server.MapPath(image), "http://www.domain.com"); | |
} | |
} | |
} |
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.Net.Mime; | |
using System.Text; | |
using System.Web; | |
namespace Umbrella.ImageGenFetch | |
{ | |
public class FetchRemoteImage : MemoryStream | |
{ | |
private string Domain; | |
private Stream OutputStream; | |
private string LocalPath; | |
private string UrlPath; | |
public FetchRemoteImage(Stream output, string urlPath, string localPath, string domain) | |
{ | |
this.Domain = domain; | |
this.OutputStream = output; | |
this.LocalPath = localPath; | |
this.UrlPath = urlPath; | |
} | |
public override void Write(byte[] buffer, int offset, int count) | |
{ | |
var content = UTF8Encoding.UTF8.GetString(buffer); | |
if (content.StartsWith("File not found:")) | |
{ | |
var url = string.Concat(this.Domain, this.UrlPath); | |
var path = this.LocalPath; | |
var directory = Path.GetDirectoryName(path); | |
if (!Directory.Exists(directory)) | |
Directory.CreateDirectory(directory); | |
using (var client = new WebClient()) | |
{ | |
client.DownloadFile(url, path); | |
} | |
// TODO: Read the downloaded image, output to buffer | |
} | |
var outputBuffer = UTF8Encoding.UTF8.GetBytes(content); | |
this.OutputStream.Write(outputBuffer, 0, outputBuffer.Length); | |
} | |
} | |
} |
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
<add name="ImageGenFetch" type="Umbrella.ImageGenFetch.CheckImageGenModule,Umbrella.ImageGenFetch" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment