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
|
|
|
|
{
|
2015-01-23 20:00:44 +11:00
|
|
|
|
private readonly MediaFileSystem _mediaFileSystem;
|
|
|
|
|
|
|
2018-01-26 17:55:20 +01:00
|
|
|
|
public FileUploadPropertyEditor(ILogger logger, MediaFileSystem mediaFileSystem)
|
2015-01-23 20:00:44 +11:00
|
|
|
|
: 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>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// Creates the corresponding property value editor.
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// </summary>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <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
|
|
|
|
}
|
|
|
|
|
|
|
2014-09-10 15:07:20 +10:00
|
|
|
|
/// <summary>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// Gets a value indicating whether a property is an upload field.
|
2014-09-10 15:07:20 +10:00
|
|
|
|
/// </summary>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <param name="property">The property.</param>
|
|
|
|
|
|
/// <param name="ensureValue">A value indicating whether to check that the property has a non-empty value.</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, bool ensureValue)
|
2014-09-10 15:07:20 +10:00
|
|
|
|
{
|
2018-01-20 12:09:15 +01:00
|
|
|
|
if (property.PropertyType.PropertyEditorAlias != Constants.PropertyEditors.Aliases.UploadField)
|
2015-12-08 12:53:11 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
if (ensureValue == false)
|
|
|
|
|
|
return true;
|
2017-11-07 19:49:14 +01:00
|
|
|
|
var stringValue = property.GetValue() as string;
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return string.IsNullOrWhiteSpace(stringValue) == false;
|
2014-09-10 15:07:20 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures any files associated are removed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="allPropertyData"></param>
|
2016-09-08 18:43:58 +02:00
|
|
|
|
internal IEnumerable<string> ServiceEmptiedRecycleBin(Dictionary<int, IEnumerable<Property>> allPropertyData)
|
2014-09-10 15:07:20 +10:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return allPropertyData.SelectMany(x => x.Value)
|
|
|
|
|
|
.Where (x => IsUploadField(x, true))
|
2017-11-07 19:49:14 +01:00
|
|
|
|
.Select(x => _mediaFileSystem.GetRelativePath((string)x.GetValue()))
|
2016-11-03 10:31:44 +01:00
|
|
|
|
.ToList();
|
2014-09-10 15:07:20 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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)
|
2014-09-10 15:07:20 +10:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return deletedEntities.SelectMany(x => x.Properties)
|
2015-12-08 12:53:11 +01:00
|
|
|
|
.Where(x => IsUploadField(x, true))
|
2017-11-07 19:49:14 +01:00
|
|
|
|
.Select(x => _mediaFileSystem.GetRelativePath((string) x.GetValue()))
|
2015-12-08 12:53:11 +01:00
|
|
|
|
.ToList();
|
2014-09-10 15:07:20 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-06 15:27:53 +10:00
|
|
|
|
/// <summary>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// After a content has been copied, also copy uploaded files.
|
2014-05-06 15:27:53 +10:00
|
|
|
|
/// </summary>
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
|
/// <param name="args">The event arguments.</param>
|
2016-11-03 10:31:44 +01:00
|
|
|
|
internal void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs<IContent> args)
|
2014-05-06 15:27:53 +10:00
|
|
|
|
{
|
2015-12-08 12:53:11 +01:00
|
|
|
|
// get the upload field properties with a value
|
|
|
|
|
|
var properties = args.Original.Properties.Where(x => IsUploadField(x, true));
|
2014-05-06 15:27:53 +10:00
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
// copy files
|
|
|
|
|
|
var isUpdated = false;
|
|
|
|
|
|
foreach (var property in properties)
|
2014-05-06 15:27:53 +10:00
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
var sourcePath = _mediaFileSystem.GetRelativePath((string) property.GetValue());
|
2016-11-03 10:31:44 +01:00
|
|
|
|
var copyPath = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath);
|
|
|
|
|
|
args.Copy.SetValue(property.Alias, _mediaFileSystem.GetUrl(copyPath));
|
2015-12-08 12:53:11 +01:00
|
|
|
|
isUpdated = true;
|
|
|
|
|
|
}
|
2014-05-06 15:27:53 +10:00
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
// if updated, re-save the copy with the updated value
|
|
|
|
|
|
if (isUpdated)
|
|
|
|
|
|
sender.Save(args.Copy);
|
|
|
|
|
|
}
|
2014-05-06 15:27:53 +10:00
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <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>
|
2016-11-03 10:31:44 +01:00
|
|
|
|
internal void MediaServiceCreated(IMediaService sender, Core.Events.NewEventArgs<IMedia> args)
|
2015-12-08 12:53:11 +01:00
|
|
|
|
{
|
|
|
|
|
|
AutoFillProperties(args.Entity);
|
2014-05-06 15:27:53 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <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>
|
2016-11-03 10:31:44 +01:00
|
|
|
|
internal void MediaServiceSaving(IMediaService sender, Core.Events.SaveEventArgs<IMedia> args)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2015-12-08 12:53:11 +01:00
|
|
|
|
foreach (var entity in args.SavedEntities)
|
|
|
|
|
|
AutoFillProperties(entity);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +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>
|
2016-11-03 10:31:44 +01:00
|
|
|
|
internal void ContentServiceSaving(IContentService sender, Core.Events.SaveEventArgs<IContent> args)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2015-12-08 12:53:11 +01:00
|
|
|
|
foreach (var entity in args.SavedEntities)
|
|
|
|
|
|
AutoFillProperties(entity);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Auto-fill properties (or clear).
|
|
|
|
|
|
/// </summary>
|
2016-11-03 10:31:44 +01:00
|
|
|
|
private void AutoFillProperties(IContentBase model)
|
2013-12-04 19:47:54 +01:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
var properties = model.Properties.Where(x => IsUploadField(x, false));
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2015-12-08 12:53:11 +01:00
|
|
|
|
foreach (var property in properties)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-11-03 10:39:21 +01:00
|
|
|
|
var autoFillConfig = _mediaFileSystem.UploadAutoFillProperties.GetConfig(property.Alias);
|
2015-12-08 12:53:11 +01:00
|
|
|
|
if (autoFillConfig == null) continue;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2017-12-06 10:20:15 +01:00
|
|
|
|
foreach (var pvalue in property.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
var svalue = property.GetValue(pvalue.LanguageId, pvalue.Segment) as string;
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(svalue))
|
|
|
|
|
|
_mediaFileSystem.UploadAutoFillProperties.Reset(model, autoFillConfig, pvalue.LanguageId, pvalue.Segment);
|
|
|
|
|
|
else
|
|
|
|
|
|
_mediaFileSystem.UploadAutoFillProperties.Populate(model, autoFillConfig, _mediaFileSystem.GetRelativePath(svalue), pvalue.LanguageId, pvalue.Segment);
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|