Files
Umbraco-CMS/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs

166 lines
7.1 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
2013-11-07 17:16:22 +01:00
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
2018-02-15 14:49:32 +01:00
[DataEditor(Constants.PropertyEditors.Aliases.UploadField, "File upload", "fileupload", Icon = "icon-download-alt", Group = "media")]
2018-02-25 10:43:16 +01:00
public class FileUploadPropertyEditor : DataEditor
2013-11-07 17:16:22 +01:00
{
private readonly MediaFileSystem _mediaFileSystem;
2018-01-26 17:55:20 +01:00
public FileUploadPropertyEditor(ILogger logger, MediaFileSystem mediaFileSystem)
: base(logger)
2013-11-07 17:16:22 +01:00
{
2017-05-12 14:49:44 +02:00
_mediaFileSystem = mediaFileSystem ?? throw new ArgumentNullException(nameof(mediaFileSystem));
2013-11-07 17:16:22 +01:00
}
/// <summary>
/// Creates the corresponding property value editor.
2013-11-07 17:16:22 +01:00
/// </summary>
/// <returns>The corresponding property value editor.</returns>
2018-02-15 14:49:32 +01:00
protected override IDataValueEditor CreateValueEditor()
2013-11-07 17:16:22 +01:00
{
2018-01-24 13:37:14 +01:00
var editor = new FileUploadPropertyValueEditor(Attribute, _mediaFileSystem);
editor.Validators.Add(new UploadFileTypeValidator());
return editor;
2013-11-07 17:16:22 +01:00
}
/// <summary>
/// Gets a value indicating whether a property is an upload field.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>A value indicating whether a property is an upload field, and (optionaly) has a non-empty value.</returns>
private static bool IsUploadField(Property property)
{
return property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.Aliases.UploadField;
}
/// <summary>
/// Ensures any files associated are removed
/// </summary>
/// <param name="deletedEntities"></param>
2016-09-08 18:43:58 +02:00
internal IEnumerable<string> ServiceDeleted(IEnumerable<ContentBase> deletedEntities)
{
return deletedEntities.SelectMany(x => x.Properties)
.Where(IsUploadField)
.SelectMany(GetFilePathsFromPropertyValues)
.Distinct();
}
/// <summary>
/// Look through all propery values stored against the property and resolve any file paths stored
/// </summary>
/// <param name="prop"></param>
/// <returns></returns>
private IEnumerable<string> GetFilePathsFromPropertyValues(Property prop)
{
var propVals = prop.Values;
foreach (var propertyValue in propVals)
{
//check if the published value contains data and return it
var propVal = propertyValue.PublishedValue;
if (propVal != null && propVal is string str1 && !str1.IsNullOrWhiteSpace())
yield return _mediaFileSystem.GetRelativePath(str1);
//check if the edited value contains data and return it
propVal = propertyValue.EditedValue;
if (propVal != null && propVal is string str2 && !str2.IsNullOrWhiteSpace())
yield return _mediaFileSystem.GetRelativePath(str2);
}
}
/// <summary>
/// After a content has been copied, also copy uploaded files.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="args">The event arguments.</param>
internal void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs<IContent> args)
{
// get the upload field properties with a value
var properties = args.Original.Properties.Where(IsUploadField);
// copy files
var isUpdated = false;
foreach (var property in properties)
{
//copy each of the property values (variants, segments) to the destination
foreach (var propertyValue in property.Values)
{
2018-04-21 09:57:28 +02:00
var propVal = property.GetValue(propertyValue.Culture, propertyValue.Segment);
if (propVal == null || !(propVal is string str) || str.IsNullOrWhiteSpace()) continue;
var sourcePath = _mediaFileSystem.GetRelativePath(str);
var copyPath = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath);
2018-04-21 09:57:28 +02:00
args.Copy.SetValue(property.Alias, _mediaFileSystem.GetUrl(copyPath), propertyValue.Culture, propertyValue.Segment);
isUpdated = true;
}
}
// if updated, re-save the copy with the updated value
if (isUpdated)
sender.Save(args.Copy);
}
/// <summary>
/// After a media has been created, auto-fill the properties.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="args">The event arguments.</param>
internal void MediaServiceCreated(IMediaService sender, Core.Events.NewEventArgs<IMedia> args)
{
AutoFillProperties(args.Entity);
}
/// <summary>
/// After a media has been saved, auto-fill the properties.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="args">The event arguments.</param>
internal void MediaServiceSaving(IMediaService sender, Core.Events.SaveEventArgs<IMedia> args)
2013-11-07 17:16:22 +01:00
{
foreach (var entity in args.SavedEntities)
AutoFillProperties(entity);
2013-11-07 17:16:22 +01:00
}
/// <summary>
/// After a content item has been saved, auto-fill the properties.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="args">The event arguments.</param>
internal void ContentServiceSaving(IContentService sender, Core.Events.SaveEventArgs<IContent> args)
2013-11-07 17:16:22 +01:00
{
foreach (var entity in args.SavedEntities)
AutoFillProperties(entity);
2013-11-07 17:16:22 +01:00
}
/// <summary>
/// Auto-fill properties (or clear).
/// </summary>
private void AutoFillProperties(IContentBase model)
{
var properties = model.Properties.Where(IsUploadField);
2013-11-07 17:16:22 +01:00
foreach (var property in properties)
2013-11-07 17:16:22 +01:00
{
var autoFillConfig = _mediaFileSystem.UploadAutoFillProperties.GetConfig(property.Alias);
if (autoFillConfig == null) continue;
2013-11-07 17:16:22 +01:00
foreach (var pvalue in property.Values)
{
2018-04-21 09:57:28 +02:00
var svalue = property.GetValue(pvalue.Culture, pvalue.Segment) as string;
if (string.IsNullOrWhiteSpace(svalue))
2018-04-21 09:57:28 +02:00
_mediaFileSystem.UploadAutoFillProperties.Reset(model, autoFillConfig, pvalue.Culture, pvalue.Segment);
else
2018-04-21 09:57:28 +02:00
_mediaFileSystem.UploadAutoFillProperties.Populate(model, autoFillConfig, _mediaFileSystem.GetRelativePath(svalue), pvalue.Culture, pvalue.Segment);
}
2017-07-20 11:21:28 +02:00
}
2013-11-07 17:16:22 +01:00
}
}
}