From f5194efb6e47aabaf76a520e62d0562763e182dc Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 28 May 2014 12:29:41 +1000 Subject: [PATCH] Fixes: U4-4999 Image cropper - Image file duplicated each time the page (content) is saved/published --- .../fileupload/fileupload.controller.js | 6 +++--- .../imagecropper/imagecropper.controller.js | 7 +++++++ .../ImageCropperPropertyValueEditor.cs | 18 ++++++++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) 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 7aa65473a6..9106e2b334 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 @@ -25,6 +25,9 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag /** this method is used to initialize the data and to re-initialize it if the server value is changed */ function initialize(index) { + + clearFiles(); + if (!index) { index = 1; } @@ -116,9 +119,6 @@ function fileUploadController($scope, $element, $compile, imageHelper, fileManag initialize($scope.rebuildInput.index + 1); } - //if (newVal !== "{clearFiles: true}" && newVal !== $scope.originalValue && !newVal.startsWith("{selectedFiles:")) { - // initialize($scope.rebuildInput.index + 1); - //} } }); }; diff --git a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/imagecropper/imagecropper.controller.js b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/imagecropper/imagecropper.controller.js index 7189ca504a..42eee9b4a8 100644 --- a/src/Umbraco.Web.UI.Client/src/views/propertyeditors/imagecropper/imagecropper.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/propertyeditors/imagecropper/imagecropper.controller.js @@ -88,6 +88,13 @@ angular.module('umbraco') } }); + + //here we declare a special method which will be called whenever the value has changed from the server + $scope.model.onValueChanged = function (newVal, oldVal) { + //clear current uploaded files + fileManager.setFiles($scope.model.alias, []); + }; + var unsubscribe = $scope.$on("formSubmitting", function () { $scope.done(); }); diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs index 2f06aaebcd..72b5aa1e62 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyValueEditor.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.IO; +using Umbraco.Core.Logging; using Umbraco.Core.Media; using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; @@ -50,11 +51,22 @@ namespace Umbraco.Web.PropertyEditors JObject oldJson = null; //get the old src path - if (currentValue != null) + if (string.IsNullOrEmpty(currentValue.ToString()) == false) { - oldJson = currentValue as JObject; + try + { + oldJson = JObject.Parse(currentValue.ToString()); + } + catch (Exception ex) + { + //for some reason the value is invalid so continue as if there was no value there + LogHelper.WarnWithException("Could not parse current db value to a JObject", ex); + } + if (oldJson != null && oldJson["src"] != null) + { oldFile = oldJson["src"].Value(); + } } //get the new src path @@ -62,7 +74,9 @@ namespace Umbraco.Web.PropertyEditors { newJson = editorValue.Value as JObject; if (newJson != null && newJson["src"] != null) + { newFile = newJson["src"].Value(); + } } //compare old and new src path