From c3471ffbc3fbc9b050f3e689f928f29b7ebe03bd Mon Sep 17 00:00:00 2001 From: perploug Date: Thu, 20 Feb 2014 12:58:38 +0100 Subject: [PATCH] Cropsizes config and upload compatible Refactors imagecropper and fileupload to use the samen metadata setting extensions --- .../prevalueeditors/cropsizes.controller.js | 13 ++- .../src/views/prevalueeditors/cropsizes.html | 84 +++++++--------- src/Umbraco.Web/MediaPropertyExtensions.cs | 97 +++++++++++++++++++ src/Umbraco.Web/Models/ImageCropData.cs | 4 +- .../FileUploadPropertyEditor.cs | 59 +---------- .../ImageCropperPropertyEditor.cs | 53 +++++++++- src/Umbraco.Web/Umbraco.Web.csproj | 1 + 7 files changed, 198 insertions(+), 113 deletions(-) create mode 100644 src/Umbraco.Web/MediaPropertyExtensions.cs diff --git a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.controller.js b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.controller.js index 78b333175d..b79f1f6866 100644 --- a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.controller.js @@ -1,8 +1,6 @@ angular.module("umbraco").controller("Umbraco.PrevalueEditors.CropSizesController", function ($scope, $timeout) { - $scope.newItem = {}; - if(!$scope.model.value){ $scope.model.value = []; } @@ -14,8 +12,12 @@ angular.module("umbraco").controller("Umbraco.PrevalueEditors.CropSizesControlle }); }; - $scope.add = function (evt) { + $scope.edit = function(item, evt) { + evt.preventDefault(); + $scope.newItem = item; + }; + $scope.add = function (evt) { evt.preventDefault(); if ($scope.newItem) { @@ -26,9 +28,10 @@ angular.module("umbraco").controller("Umbraco.PrevalueEditors.CropSizesControlle $scope.hasError = false; return; } - } - + + $scope.newItem = undefined; //there was an error, do the highlight (will be set back by the directive) $scope.hasError = true; + } }; }); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.html b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.html index b1a62dda20..68949b42ec 100644 --- a/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.html +++ b/src/Umbraco.Web.UI.Client/src/views/prevalueeditors/cropsizes.html @@ -1,58 +1,48 @@
-
-
-
+
+
    + +
  • + + + + {{node.alias}} +
    {{node.width}}px × {{node.height}}px +
  • +
+
+ +
+

Define crop

+

+ Give the crop an alias and it's default width and height. +

+ +
-
+
-
- - -
- -
- - -
- -
- -
-
+
+ + + × + +
+ +
+ +
-
-
-
- - -
- -
- - -
- -
- - -
- -
- -
-
+
+
\ No newline at end of file diff --git a/src/Umbraco.Web/MediaPropertyExtensions.cs b/src/Umbraco.Web/MediaPropertyExtensions.cs new file mode 100644 index 0000000000..ebe6756e6a --- /dev/null +++ b/src/Umbraco.Web/MediaPropertyExtensions.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Core; +using Umbraco.Core.Configuration; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.IO; +using Umbraco.Core.Models; + +namespace Umbraco.Web +{ + internal static class MediaPropertyExtensions + { + + internal static void AutoPopulateFileMetaDataProperties(this IContentBase model, string propertyAlias, string relativefilePath = null) + { + var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + var uploadFieldConfigNode = + UmbracoConfig.For.UmbracoSettings().Content.ImageAutoFillProperties + .FirstOrDefault(x => x.Alias == propertyAlias); + + if (uploadFieldConfigNode != null && model.Properties.Contains(propertyAlias)) + { + if(relativefilePath== null) + relativefilePath = model.GetValue(propertyAlias); + + //now we need to check if there is a path + if (!string.IsNullOrEmpty(relativefilePath)) + { + var fullPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(relativefilePath)); + var umbracoFile = new UmbracoMediaFile(fullPath); + FillProperties(uploadFieldConfigNode, model, umbracoFile); + }else{ + //for now I'm just resetting this + ResetProperties(uploadFieldConfigNode, model); + } + } + } + + internal static void ResetFileMetaDataProperties(this IContentBase content, IImagingAutoFillUploadField uploadFieldConfigNode) { + ResetProperties(uploadFieldConfigNode, content); + } + + private static void ResetProperties(IImagingAutoFillUploadField uploadFieldConfigNode, IContentBase content) + { + if (content.Properties.Contains(uploadFieldConfigNode.WidthFieldAlias)) + content.Properties[uploadFieldConfigNode.WidthFieldAlias].Value = string.Empty; + + if (content.Properties.Contains(uploadFieldConfigNode.HeightFieldAlias)) + content.Properties[uploadFieldConfigNode.HeightFieldAlias].Value = string.Empty; + + if (content.Properties.Contains(uploadFieldConfigNode.LengthFieldAlias)) + content.Properties[uploadFieldConfigNode.LengthFieldAlias].Value = string.Empty; + + if (content.Properties.Contains(uploadFieldConfigNode.ExtensionFieldAlias)) + content.Properties[uploadFieldConfigNode.ExtensionFieldAlias].Value = string.Empty; + } + + + internal static void PopulateFileMetaDataProperties(this IContentBase content, IImagingAutoFillUploadField uploadFieldConfigNode, object relativeFilePath){ + if (relativeFilePath is string && ((string)relativeFilePath).IsNullOrWhiteSpace() == false && !relativeFilePath.ToString().DetectIsJson()) + { + var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + var fullPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(relativeFilePath.ToString())); + var umbracoFile = new UmbracoMediaFile(fullPath); + FillProperties(uploadFieldConfigNode, content, umbracoFile); + } + else + { + //for now I'm just resetting this since we cant detect a file + ResetProperties(uploadFieldConfigNode, content); + } + } + + private static void FillProperties(IImagingAutoFillUploadField uploadFieldConfigNode, IContentBase content, UmbracoMediaFile um) + { + var size = um.SupportsResizing ? (Size?)um.GetDimensions() : null; + + if (content.Properties.Contains(uploadFieldConfigNode.WidthFieldAlias)) + content.Properties[uploadFieldConfigNode.WidthFieldAlias].Value = size.HasValue ? size.Value.Width.ToInvariantString() : string.Empty; + + if (content.Properties.Contains(uploadFieldConfigNode.HeightFieldAlias)) + content.Properties[uploadFieldConfigNode.HeightFieldAlias].Value = size.HasValue ? size.Value.Height.ToInvariantString() : string.Empty; + + if (content.Properties.Contains(uploadFieldConfigNode.LengthFieldAlias)) + content.Properties[uploadFieldConfigNode.LengthFieldAlias].Value = um.Length; + + if (content.Properties.Contains(uploadFieldConfigNode.ExtensionFieldAlias)) + content.Properties[uploadFieldConfigNode.ExtensionFieldAlias].Value = um.Extension; + } + + + } +} diff --git a/src/Umbraco.Web/Models/ImageCropData.cs b/src/Umbraco.Web/Models/ImageCropData.cs index 392529de4b..3aff152429 100644 --- a/src/Umbraco.Web/Models/ImageCropData.cs +++ b/src/Umbraco.Web/Models/ImageCropData.cs @@ -43,7 +43,8 @@ namespace Umbraco.Web.Models { if (HasFocalPoint()) { - + sb.Append("?center=" + FocalPoint.Top + "," + FocalPoint.Left); + sb.Append("&mode=crop"); } else { @@ -51,7 +52,6 @@ namespace Umbraco.Web.Models sb.Append("&mode=crop"); } - } sb.Append("&width=").Append(crop.Width); diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 1ff9637954..fcfa093b01 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -74,68 +74,11 @@ namespace Umbraco.Web.PropertyEditors if (uploadFieldConfigNode != null) { - - //now we need to check if there is a value - if (p.Value is string && ((string) p.Value).IsNullOrWhiteSpace() == false) - { - if (!p.Value.ToString().DetectIsJson()) - { - //there might be multiple, we can only process the first one! - var split = ((string)p.Value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - if (split.Any()) - { - var fullPath = mediaFileSystem.GetFullPath(mediaFileSystem.GetRelativePath(split[0])); - var umbracoFile = new UmbracoMediaFile(fullPath); - FillProperties(uploadFieldConfigNode, model, umbracoFile); - } - } - else - { - //for now I'm just resetting this - ResetProperties(uploadFieldConfigNode, model); - } - } - else - { - //there's no value so need to reset to zero - ResetProperties(uploadFieldConfigNode, model); - } + model.PopulateFileMetaDataProperties(uploadFieldConfigNode, p.Value); } } } - private static void ResetProperties(IImagingAutoFillUploadField uploadFieldConfigNode, IContentBase content) - { - if (content.Properties.Contains(uploadFieldConfigNode.WidthFieldAlias)) - content.Properties[uploadFieldConfigNode.WidthFieldAlias].Value = string.Empty; - - if (content.Properties.Contains(uploadFieldConfigNode.HeightFieldAlias)) - content.Properties[uploadFieldConfigNode.HeightFieldAlias].Value = string.Empty; - - if (content.Properties.Contains(uploadFieldConfigNode.LengthFieldAlias)) - content.Properties[uploadFieldConfigNode.LengthFieldAlias].Value = string.Empty; - - if (content.Properties.Contains(uploadFieldConfigNode.ExtensionFieldAlias)) - content.Properties[uploadFieldConfigNode.ExtensionFieldAlias].Value = string.Empty; - } - - private static void FillProperties(IImagingAutoFillUploadField uploadFieldConfigNode, IContentBase content, UmbracoMediaFile um) - { - var size = um.SupportsResizing ? (Size?)um.GetDimensions() : null; - - if (content.Properties.Contains(uploadFieldConfigNode.WidthFieldAlias)) - content.Properties[uploadFieldConfigNode.WidthFieldAlias].Value = size.HasValue ? size.Value.Width.ToInvariantString() : string.Empty; - - if (content.Properties.Contains(uploadFieldConfigNode.HeightFieldAlias)) - content.Properties[uploadFieldConfigNode.HeightFieldAlias].Value = size.HasValue ? size.Value.Height.ToInvariantString() : string.Empty; - - if (content.Properties.Contains(uploadFieldConfigNode.LengthFieldAlias)) - content.Properties[uploadFieldConfigNode.LengthFieldAlias].Value = um.Length; - - if (content.Properties.Contains(uploadFieldConfigNode.ExtensionFieldAlias)) - content.Properties[uploadFieldConfigNode.ExtensionFieldAlias].Value = um.Extension; - } - /// /// A custom pre-val editor to ensure that the data is stored how the legacy data was stored in /// diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs index 268a0558ba..ca7c9a2ec3 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs @@ -1,16 +1,26 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Umbraco.Core; +using Umbraco.Core.Configuration; +using Umbraco.Core.IO; +using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; namespace Umbraco.Web.PropertyEditors { [PropertyEditor(Constants.PropertyEditors.ImageCropperAlias, "Image Cropper", "imagecropper", ValueType = "JSON", HideLabel = false)] public class ImageCropperPropertyEditor : PropertyEditor { + static ImageCropperPropertyEditor() + { + MediaService.Saving += MediaServiceSaving; + MediaService.Creating += MediaServiceCreating; + } /// /// Creates our custom value editor @@ -38,6 +48,47 @@ namespace Umbraco.Web.PropertyEditors }; } + static void MediaServiceCreating(IMediaService sender, Core.Events.NewEventArgs e) + { + AutoFillProperties(e.Entity); + } + + static void MediaServiceSaving(IMediaService sender, Core.Events.SaveEventArgs e) + { + foreach (var m in e.SavedEntities) + { + AutoFillProperties(m); + } + } + + static void AutoFillProperties(IContentBase model) + { + var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + foreach (var p in model.Properties.Where(x => x.PropertyType.Alias == Constants.PropertyEditors.ImageCropperAlias)) + { + var uploadFieldConfigNode = + UmbracoConfig.For.UmbracoSettings().Content.ImageAutoFillProperties + .FirstOrDefault(x => x.Alias == p.Alias); + + if (uploadFieldConfigNode != null) + { + if (p.Value != null){ + var json = p.Value as JObject; + if (json != null && json["src"] != null) + model.PopulateFileMetaDataProperties(uploadFieldConfigNode, json["src"].Value()); + else if (p.Value is string) + { + var config = ApplicationContext.Current.Services.DataTypeService.GetPreValuesByDataTypeId(p.PropertyType.DataTypeDefinitionId).FirstOrDefault(); + var crops = !string.IsNullOrEmpty(config) ? config : "[]"; + p.Value = "{src: '" + p.Value + "', crops: " + crops + "}"; + model.PopulateFileMetaDataProperties(uploadFieldConfigNode, p.Value); + } + }else + model.ResetFileMetaDataProperties(uploadFieldConfigNode); + } + } + } + private IDictionary _internalPreValues; public override IDictionary DefaultPreValues { diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 0b89f414ef..47fd9fbffd 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -302,6 +302,7 @@ +