diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index 9aa5f455af..9ec1345a74 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -101,18 +101,34 @@ namespace Umbraco.Web.Editors public HttpResponseMessage GetBigThumbnail(string originalImagePath) { var imagePath = originalImagePath; - var bigThumbPath = imagePath.Substring(0, imagePath.LastIndexOf('.')) + "_big-thumb" + ".jpg"; - var thumbFilePath = IOHelper.MapPath(bigThumbPath); - if (System.IO.File.Exists(thumbFilePath) == false) + var thumbIsNew = false; + string thumbFilePath; + var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + if (imagePath.StartsWith("http")) + { + thumbFilePath = imagePath.TrimEnd(".jpg") + "_big-thumb.jpg"; + thumbIsNew = mediaFileSystem.FileExists(thumbFilePath) == false; + } + else + { + var bigThumbPath = imagePath.Substring(0, imagePath.LastIndexOf('.')) + "_big-thumb" + ".jpg"; + thumbFilePath = IOHelper.MapPath(bigThumbPath); + thumbIsNew = System.IO.File.Exists(thumbFilePath) == false; + } + if (thumbIsNew) { //we need to generate it - var origFilePath = IOHelper.MapPath(imagePath); - if (System.IO.File.Exists(origFilePath) == false) + string origFilePath = imagePath; + if (imagePath.StartsWith("http") == false) { - return new HttpResponseMessage(HttpStatusCode.NotFound); + origFilePath = IOHelper.MapPath(imagePath); + if (System.IO.File.Exists(origFilePath) == false) + { + return new HttpResponseMessage(HttpStatusCode.NotFound); + } } - var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); - using (var fileStream = new FileStream(origFilePath, FileMode.Open, FileAccess.Read)) + + using (var fileStream = mediaFileSystem.OpenFile(origFilePath)) { if (fileStream.CanSeek) fileStream.Seek(0, 0); using (var originalImage = Image.FromStream(fileStream)) @@ -130,7 +146,89 @@ namespace Umbraco.Web.Editors var result = new HttpResponseMessage(HttpStatusCode.OK); //NOTE: That we are not closing this stream as the framework will do that for us, if we try it will // fail. See http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api - var stream = new FileStream(thumbFilePath, FileMode.Open, FileAccess.Read); + var stream = mediaFileSystem.OpenFile(thumbFilePath); + if (stream.CanSeek) stream.Seek(0, 0); + result.Content = new StreamContent(stream); + result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); + return result; + } + + /// + /// Gets a rezised image for the media id + /// + /// + /// + /// + /// + /// If there is no media, image property or image file is found then this will return not found. + /// + public HttpResponseMessage GetResized(int mediaId, int width) + { + var media = Services.MediaService.GetById(mediaId); + if (media == null) + { + return new HttpResponseMessage(HttpStatusCode.NotFound); + } + var imageProp = media.Properties[Constants.Conventions.Media.File]; + if (imageProp == null) + { + return new HttpResponseMessage(HttpStatusCode.NotFound); + } + + var imagePath = imageProp.Value.ToString(); + + return GetResized(imagePath, width); + } + + public HttpResponseMessage GetResized(string imagePath, int width) + { + var thumbIsNew = false; + string thumbFilePath; + var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + if (imagePath.StartsWith("http")) + { + thumbFilePath = imagePath.TrimEnd(".jpg") + "_" + width + ".jpg"; + thumbIsNew = mediaFileSystem.FileExists(thumbFilePath) == false; + } + else + { + var bigThumbPath = imagePath.Substring(0, imagePath.LastIndexOf('.')) + width + ".jpg"; + thumbFilePath = IOHelper.MapPath(bigThumbPath); + thumbIsNew = System.IO.File.Exists(thumbFilePath) == false; + } + if (thumbIsNew) + { + //we need to generate it + string origFilePath = imagePath; + if (imagePath.StartsWith("http") == false) + { + origFilePath = IOHelper.MapPath(imagePath); + if (System.IO.File.Exists(origFilePath) == false) + { + return new HttpResponseMessage(HttpStatusCode.NotFound); + } + } + + using (var fileStream = mediaFileSystem.OpenFile(origFilePath)) + { + if (fileStream.CanSeek) fileStream.Seek(0, 0); + using (var originalImage = Image.FromStream(fileStream)) + { + ImageHelper.GenerateThumbnail( + originalImage, + width, + string.Format("{0}_{1}.jpg", origFilePath.Substring(0, origFilePath.LastIndexOf(".")), width), + Path.GetExtension(origFilePath).Substring(1).ToLowerInvariant(), + mediaFileSystem); + } + } + } + + var result = new HttpResponseMessage(HttpStatusCode.OK); + //NOTE: That we are not closing this stream as the framework will do that for us, if we try it will + // fail. See http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api + var stream = mediaFileSystem.OpenFile(thumbFilePath); + if (stream.CanSeek) stream.Seek(0, 0); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return result; diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 45e74ac38c..fb357f4e05 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text.RegularExpressions; using System.Xml; using Newtonsoft.Json.Linq; +using umbraco.cms.businesslogic.Files; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; @@ -79,7 +80,11 @@ namespace Umbraco.Web.PropertyEditors var split = ((string) p.Value).Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); if (split.Any()) { - var umbracoFile = new UmbracoMediaFile(IOHelper.MapPath(split[0])); + UmbracoMediaFile umbracoFile; + if (split[0].StartsWith("http")) + umbracoFile = new UmbracoMediaFile(split[0]); + else + umbracoFile = new UmbracoMediaFile(IOHelper.MapPath(split[0])); FillProperties(uploadFieldConfigNode, model, umbracoFile); } }