Created
September 6, 2015 11:25
-
-
Save DavidVeksler/3771c54c29beb5b273d2 to your computer and use it in GitHub Desktop.
Umbraco: JPEGmini Umbraco background uploaded media JPEG image optimizer
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
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
MediaService.Saved += MediaService_Saved; | |
} | |
private void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e) | |
{ | |
try | |
{ | |
.. some other stuff... | |
Task.Run(() => ImageOptimizer.OptimizeMedia(media)); | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex); | |
LogHelper.Error<Global>("error processing image save",ex); | |
} | |
} |
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
#region | |
using System; | |
using System.Configuration; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net; | |
using System.Threading; | |
using System.Web; | |
using FEE.DataAccess; | |
using Newtonsoft.Json; | |
using Umbraco.Core.IO; | |
using Umbraco.Core.Logging; | |
using Umbraco.Core.Models; | |
using Umbraco.Web.Models; | |
using File = System.IO.File; | |
#endregion | |
namespace FEE.Domain | |
{ | |
// http://www.jpegmini.com/server/docs#JPEGmini_for_AWS_REST_API_aws | |
public class ImageOptimizer | |
{ | |
public static void OptimizeMedia(IMedia media) | |
{ | |
if (media.ContentTypeId == (int) CmsContentTypes.Image) | |
{ | |
OptimizePhotoFromImageCropString(media.GetValue<string>("umbracoFile")); | |
} | |
} | |
public static void OptimizePhotoFromImageCropString(string umbracoFileString) | |
{ | |
var imageCrops = JsonConvert.DeserializeObject<ImageCropDataSet>(umbracoFileString); | |
var src = imageCrops.Src; | |
if (!src.EndsWith(".jpg") && !src.EndsWith(".jpeg")) return; | |
var path = HttpRuntime.AppDomainAppPath + src.Replace("/", @"\"); | |
OptimizePhoto(path); | |
} | |
public static void OptimizePhoto(string inputFile) | |
{ | |
try | |
{ | |
Debug.WriteLine("optimize " + inputFile); | |
string temporaryFilePath = null; | |
temporaryFilePath = HttpRuntime.AppDomainAppPath + SystemDirectories.Data + @"\TEMP\FileUploads\" + | |
new FileInfo(inputFile).Name; | |
temporaryFilePath = temporaryFilePath.Replace(@"~/", ""); | |
if (File.Exists(temporaryFilePath)) | |
{ | |
Debug.WriteLine("file already optimized"); | |
return; | |
} | |
var host = ConfigurationManager.AppSettings["ImageSmushingService"]; | |
var requestUrl = string.Format("http://{0}/api/v1/optimize", host); | |
//Setup the web POST request | |
var httpWebRequest = | |
(HttpWebRequest) WebRequest.Create(requestUrl); | |
httpWebRequest.Method = "POST"; | |
//Upload the file | |
using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) | |
{ | |
fs.CopyTo(httpWebRequest.GetRequestStream()); | |
} | |
//Get the server response, and write it a file | |
var response = httpWebRequest.GetResponse(); | |
using (var of = new FileStream(temporaryFilePath, FileMode.Create, FileAccess.Write)) | |
{ | |
response.GetResponseStream().CopyTo(of); | |
} | |
File.Copy(temporaryFilePath, inputFile, true); | |
Debug.WriteLine("updated " + inputFile); | |
LogHelper.Info<ImageOptimizer>("optimize image: " + inputFile); | |
// wait 300 seconds before cleanup to prevent smush from being run again in case there is another Save event | |
Thread.Sleep(300*1000); | |
File.Delete(temporaryFilePath); | |
} | |
catch (Exception ex) | |
{ | |
LogHelper.Error<ImageOptimizer>("failed to optimize image", ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment