Cropsizes config and upload compatible

Refactors imagecropper and fileupload to use the samen metadata setting
extensions
This commit is contained in:
perploug
2014-02-20 12:58:38 +01:00
parent f8baf343bf
commit c3471ffbc3
7 changed files with 198 additions and 113 deletions

View File

@@ -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;
}
};
});

View File

@@ -1,58 +1,48 @@
<div ng-controller="Umbraco.PrevalueEditors.CropSizesController" class="umb-editor umb-cropsizes">
<div class="control-group">
<div class="row">
<div class="span3">
<div class="control-group umb-contentpicker">
<ul class="unstyled list-icons" style="margin-top: 30px"
ui-sortable
ng-model="model.value">
<li ng-repeat="node in model.value">
<i class="icon icon-delete red hover-show pull-right" ng-click="remove(node, $event)"></i>
<i class="icon icon-picture handle hover-hide"></i>
<a href prevent-default ng-click="edit(node, $event)" >{{node.alias}}</a>
<br/><small>{{node.width}}px &times; {{node.height}}px</small>
</li>
</ul>
</div>
<div class="form" ng-if="newItem">
<h4>Define crop</h4>
<p>
Give the crop an alias and it's default width and height.
</p>
<div class="control-group">
<label>Alias</label>
<input name="newItem.alias" type="text"
ng-model="newItem.alias" val-highlight="hasError" />
</div>
</div>
<div class="span3">
<label>Height</label>
<input name="newItem.height" type="number"
ng-model="newItem.height" val-highlight="hasError" />
</div>
<div class="span3">
<label>Width</label>
<input name="newItem.width" type="number"
ng-model="newItem.width" val-highlight="hasError" />
</div>
<div class="span3">
<button class="btn btn-small" ng-click="add($event)">Add</button>
</div>
</div>
<div class="control-group">
<label>Size</label>
<input name="newItem.width" type="number" placeholder="Width"
ng-model="newItem.width" class="umb-editor-tiny" val-highlight="hasError" />
&times;
<input name="newItem.height" type="number" placeholder="Height"
ng-model="newItem.height" class="umb-editor-tiny" val-highlight="hasError" />
</div>
<div class="control-group">
<button class="btn btn-success" ng-click="add($event)">Save</button>
</div>
</div>
<div class="control-group">
<div class="row" ng-repeat="item in model.value">
<div class="span3">
<label>Alias</label>
<input name="newItem.alias" type="text"
ng-model="item.alias"
required val-highlight="hasError" />
</div>
<div class="span3">
<label>Height</label>
<input name="newItem.height" type="number"
ng-model="item.height"
required val-highlight="hasError" />
</div>
<div class="span3">
<label>Width</label>
<input name="newItem.width" type="number"
ng-model="item.width"
required val-highlight="hasError" />
</div>
<div class="span3">
<button class="btn btn-small btn-danger" ng-click="remove(item, $event)">Remove</button>
</div>
</div>
<div class="control-group" ng-hide="newItem">
<button class="btn btn-small" ng-click="newItem = {}" prevent-default>Add new crop</button>
</div>
</div>

View File

@@ -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<MediaFileSystem>();
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<string>(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<MediaFileSystem>();
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;
}
}
}

View File

@@ -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);

View File

@@ -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;
}
/// <summary>
/// A custom pre-val editor to ensure that the data is stored how the legacy data was stored in
/// </summary>

View File

@@ -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;
}
/// <summary>
/// Creates our custom value editor
@@ -38,6 +48,47 @@ namespace Umbraco.Web.PropertyEditors
};
}
static void MediaServiceCreating(IMediaService sender, Core.Events.NewEventArgs<IMedia> e)
{
AutoFillProperties(e.Entity);
}
static void MediaServiceSaving(IMediaService sender, Core.Events.SaveEventArgs<IMedia> e)
{
foreach (var m in e.SavedEntities)
{
AutoFillProperties(m);
}
}
static void AutoFillProperties(IContentBase model)
{
var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
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<string>());
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<string, object> _internalPreValues;
public override IDictionary<string, object> DefaultPreValues
{

View File

@@ -302,6 +302,7 @@
<Compile Include="Editors\DataTypeValidateAttribute.cs" />
<Compile Include="Editors\EntityControllerActionSelector.cs" />
<Compile Include="Editors\ImagesController.cs" />
<Compile Include="MediaPropertyExtensions.cs" />
<Compile Include="Models\ContentEditing\EntityTypeSearchResult.cs" />
<Compile Include="Models\ContentEditing\ListViewAwareContentItemDisplayBase.cs" />
<Compile Include="Models\ImageCropData.cs" />