Cropsizes config and upload compatible
Refactors imagecropper and fileupload to use the samen metadata setting extensions
This commit is contained in:
97
src/Umbraco.Web/MediaPropertyExtensions.cs
Normal file
97
src/Umbraco.Web/MediaPropertyExtensions.cs
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user