From 36a1c0a9e0985fa723a8772d33bf619ce6fbc636 Mon Sep 17 00:00:00 2001 From: Wincent Date: Sat, 31 Oct 2015 16:29:02 +0100 Subject: [PATCH 1/2] Changed web api GetBigThumbnail to image processor in mediahelper.service.js and fileupload.controller.js --- .../src/common/services/mediahelper.service.js | 10 ++-------- .../fileupload/fileupload.controller.js | 12 ++++-------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js index 68ca32efbc..c245b99963 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js @@ -303,14 +303,8 @@ function mediaHelper(umbRequestHelper) { return null; } - //get the proxy url for big thumbnails (this ensures one is always generated) - var thumbnailUrl = umbRequestHelper.getApiUrl( - "imagesApiBaseUrl", - "GetBigThumbnail", - [{ originalImagePath: imagePath }]); - - //var ext = imagePath.substr(imagePath.lastIndexOf('.')); - //return imagePath.substr(0, imagePath.lastIndexOf('.')) + "_big-thumb" + ".jpg"; + //get default big thumbnail from image processor + var thumbnailUrl = imagePath + "?width=500"; return thumbnailUrl; }, diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js index 37dea64c21..6571acd866 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js @@ -63,10 +63,8 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag _.each($scope.persistedFiles, function (file) { - var thumbnailUrl = umbRequestHelper.getApiUrl( - "imagesApiBaseUrl", - "GetBigThumbnail", - [{ originalImagePath: file.file }]); + //get default big thumbnail from image processor + var thumbnailUrl = file.file + "?width=500"; file.thumbnail = thumbnailUrl; }); @@ -154,10 +152,8 @@ angular.module("umbraco") if (mediaHelper.detectIfImageByExtension(property.value)) { - var thumbnailUrl = umbRequestHelper.getApiUrl( - "imagesApiBaseUrl", - "GetBigThumbnail", - [{ originalImagePath: property.value }]); + //get default big thumbnail from image processor + var thumbnailUrl = property.value + "?width=500"; return thumbnailUrl; } From 0409cbe6134ba2382b9a0c9848fcbcb625c62f3c Mon Sep 17 00:00:00 2001 From: Wincent Date: Mon, 9 Nov 2015 21:41:25 +0100 Subject: [PATCH 2/2] Changed ImageController.GetBigThumbnail to redirect Changed thumbnail paths to include rnd equal to UpdateDate --- .../common/services/mediahelper.service.js | 10 +- .../fileupload/fileupload.controller.js | 51 +++++----- src/Umbraco.Web/Editors/ImagesController.cs | 99 +++++-------------- 3 files changed, 56 insertions(+), 104 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js index c245b99963..68ca32efbc 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/mediahelper.service.js @@ -303,8 +303,14 @@ function mediaHelper(umbRequestHelper) { return null; } - //get default big thumbnail from image processor - var thumbnailUrl = imagePath + "?width=500"; + //get the proxy url for big thumbnails (this ensures one is always generated) + var thumbnailUrl = umbRequestHelper.getApiUrl( + "imagesApiBaseUrl", + "GetBigThumbnail", + [{ originalImagePath: imagePath }]); + + //var ext = imagePath.substr(imagePath.lastIndexOf('.')); + //return imagePath.substr(0, imagePath.lastIndexOf('.')) + "_big-thumb" + ".jpg"; return thumbnailUrl; }, diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js index 6571acd866..d62d76c982 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js @@ -63,8 +63,10 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag _.each($scope.persistedFiles, function (file) { - //get default big thumbnail from image processor - var thumbnailUrl = file.file + "?width=500"; + var thumbnailUrl = umbRequestHelper.getApiUrl( + "imagesApiBaseUrl", + "GetBigThumbnail", + [{ originalImagePath: file.file }]); file.thumbnail = thumbnailUrl; }); @@ -142,29 +144,28 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag }; angular.module("umbraco") .controller('Umbraco.PropertyEditors.FileUploadController', fileUploadController) - .run(function(mediaHelper, umbRequestHelper){ + .run(function(mediaHelper, umbRequestHelper, assetsService){ if (mediaHelper && mediaHelper.registerFileResolver) { - - //NOTE: The 'entity' can be either a normal media entity or an "entity" returned from the entityResource - // they contain different data structures so if we need to query against it we need to be aware of this. - mediaHelper.registerFileResolver("Umbraco.UploadField", function(property, entity, thumbnail){ - if (thumbnail) { - - if (mediaHelper.detectIfImageByExtension(property.value)) { - - //get default big thumbnail from image processor - var thumbnailUrl = property.value + "?width=500"; - - return thumbnailUrl; - } - else { - return null; - } - + assetsService.load(["lib/moment/moment-with-locales.js"]).then( + function () { + //NOTE: The 'entity' can be either a normal media entity or an "entity" returned from the entityResource + // they contain different data structures so if we need to query against it we need to be aware of this. + mediaHelper.registerFileResolver("Umbraco.UploadField", function(property, entity, thumbnail){ + if (thumbnail) { + if (mediaHelper.detectIfImageByExtension(property.value)) { + //get default big thumbnail from image processor + var thumbnailUrl = property.value + "?rnd=" + moment(entity.updateDate).format("YYYYMMDDHHmmss") + "&width=500"; + return thumbnailUrl; + } + else { + return null; + } + } + else { + return property.value; + } + }); } - else { - return property.value; - } - }); + ); } - }); \ No newline at end of file + }); diff --git a/src/Umbraco.Web/Editors/ImagesController.cs b/src/Umbraco.Web/Editors/ImagesController.cs index 54938be76d..14444f53ee 100644 --- a/src/Umbraco.Web/Editors/ImagesController.cs +++ b/src/Umbraco.Web/Editors/ImagesController.cs @@ -8,6 +8,7 @@ using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.Media; +using Umbraco.Core.Models; using Umbraco.Web.Mvc; using Umbraco.Web.WebApi; using Umbraco.Web.WebApi.Filters; @@ -36,15 +37,8 @@ namespace Umbraco.Web.Editors { return Request.CreateResponse(HttpStatusCode.NotFound); } - var imageProp = media.Properties[Constants.Conventions.Media.File]; - if (imageProp == null) - { - return Request.CreateResponse(HttpStatusCode.NotFound); - } - var imagePath = imageProp.Value.ToString(); - - return GetBigThumbnail(imagePath); + return GetResized(media, 500); } /// @@ -60,7 +54,7 @@ namespace Umbraco.Web.Editors if (string.IsNullOrWhiteSpace(originalImagePath)) return Request.CreateResponse(HttpStatusCode.OK); - return GetResized(originalImagePath, 500, "big-thumb"); + return GetResized(originalImagePath, 500); } /// @@ -79,15 +73,8 @@ namespace Umbraco.Web.Editors { 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); + return GetResized( media, 500 ); } /// @@ -101,75 +88,33 @@ namespace Umbraco.Web.Editors /// public HttpResponseMessage GetResized(string imagePath, int width) { - return GetResized(imagePath, width, Convert.ToString(width)); + var media = Services.MediaService.GetMediaByPath( imagePath ); + if (media == null) + { + return new HttpResponseMessage( HttpStatusCode.NotFound ); + } + + return GetResized( media, 500 ); } - //TODO: We should delegate this to ImageProcessing - /// - /// Gets a resized image - if the requested max width is greater than the original image, only the original image will be returned. + /// Gets a resized image by redirecting to ImageProcessor /// - /// + /// /// - /// /// - private HttpResponseMessage GetResized(string imagePath, int width, string suffix) - { - var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); - var ext = Path.GetExtension(imagePath); - - //we need to check if it is an image by extension - if (UmbracoConfig.For.UmbracoSettings().Content.ImageFileTypes.InvariantContains(ext.TrimStart('.')) == false) + private HttpResponseMessage GetResized(IMedia media, int width) + { + var imageProp = media.Properties[Constants.Conventions.Media.File]; + if (imageProp == null) { - return Request.CreateResponse(HttpStatusCode.NotFound); + return Request.CreateResponse( HttpStatusCode.NotFound ); } - var thumbFilePath = imagePath.TrimEnd(ext) + "_" + suffix + ".jpg"; - var fullOrgPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(imagePath)); - var fullNewPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(thumbFilePath)); - var thumbIsNew = mediaFileSystem.FileExists(fullNewPath) == false; - if (thumbIsNew) - { - //we need to generate it - if (mediaFileSystem.FileExists(fullOrgPath) == false) - { - return Request.CreateResponse(HttpStatusCode.NotFound); - } - - using (var fileStream = mediaFileSystem.OpenFile(fullOrgPath)) - { - if (fileStream.CanSeek) fileStream.Seek(0, 0); - using (var originalImage = Image.FromStream(fileStream)) - { - //If it is bigger, then do the resize - if (originalImage.Width >= width && originalImage.Height >= width) - { - ImageHelper.GenerateThumbnail( - originalImage, - width, - fullNewPath, - "jpg", - mediaFileSystem); - } - else - { - //just return the original image - fullNewPath = fullOrgPath; - } - - } - } - } - - var result = Request.CreateResponse(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(fullNewPath); - if (stream.CanSeek) stream.Seek(0, 0); - result.Content = new StreamContent(stream); - result.Headers.Date = mediaFileSystem.GetLastModified(imagePath); - result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); - return result; + var imagePath = imageProp.Value.ToString(); + var response = Request.CreateResponse( HttpStatusCode.Found ); + response.Headers.Location = new Uri( string.Format( "{0}?rnd={1}&width={2}", imagePath, string.Format( "{0:yyyyMMddHHmmss}", media.UpdateDate ), width ), UriKind.Relative ); + return response; } } } \ No newline at end of file