Fixes: U4-4799 Ensure image logic found in the ContentService is duplicated for the new cropper

This commit is contained in:
Shannon
2014-05-06 15:27:53 +10:00
parent d3e925add9
commit a846ba64eb
3 changed files with 121 additions and 43 deletions

View File

@@ -1159,47 +1159,6 @@ namespace Umbraco.Core.Services
repository.AddOrUpdatePreviewXml(copy, c => _entitySerializer.Serialize(this, _dataTypeService, c));
uow.Commit();
//Special case for the Upload DataType
//TODO: Should we handle this with events?
//TODO: This really shouldn't be here! What about the cropper in v7, we'll have the same issue.
if (content.Properties.Any(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias))
{
bool isUpdated = false;
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
//Loop through properties to check if the content contains media that should be deleted
foreach (var property in content.Properties.Where(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias
&& string.IsNullOrEmpty(x.Value.ToString()) == false))
{
if (fs.FileExists(IOHelper.MapPath(property.Value.ToString())))
{
var currentPath = fs.GetRelativePath(property.Value.ToString());
var propertyId = copy.Properties.First(x => x.Alias == property.Alias).Id;
var newPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(currentPath));
fs.CopyFile(currentPath, newPath);
copy.SetValue(property.Alias, fs.GetUrl(newPath));
//Copy thumbnails
foreach (var thumbPath in fs.GetThumbnails(currentPath))
{
var newThumbPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(thumbPath));
fs.CopyFile(thumbPath, newThumbPath);
}
isUpdated = true;
}
}
if (isUpdated)
{
repository.AddOrUpdate(copy);
//add or update a preview
repository.AddOrUpdatePreviewXml(copy, c => _entitySerializer.Serialize(this, _dataTypeService, c));
uow.Commit();
}
}
//TODO: Move this to the repository layer in a single transaction!
//Special case for the associated tags
var tags = uow.Database.Fetch<TagRelationshipDto>("WHERE nodeId = @Id", new { Id = content.Id });
@@ -1210,7 +1169,7 @@ namespace Umbraco.Core.Services
}
//NOTE This 'Relation' part should eventually be delegated to a RelationService
//TODO: This should be party of a single commit
//TODO: This should be part of a single commit
if (relateToOriginal)
{
IRelationType relationType = null;

View File

@@ -31,9 +31,13 @@ namespace Umbraco.Web.PropertyEditors
static FileUploadPropertyEditor()
{
MediaService.Saving += MediaServiceSaving;
MediaService.Creating += MediaServiceCreating;
MediaService.Created += MediaServiceCreating;
ContentService.Copied += ContentServiceCopied;
}
/// <summary>
/// Creates our custom value editor
/// </summary>
@@ -50,6 +54,50 @@ namespace Umbraco.Web.PropertyEditors
return new FileUploadPreValueEditor();
}
/// <summary>
/// After the content is copied we need to check if there are files that also need to be copied
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs<IContent> e)
{
if (e.Original.Properties.Any(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias))
{
bool isUpdated = false;
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
//Loop through properties to check if the content contains media that should be deleted
foreach (var property in e.Original.Properties.Where(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias
&& x.Value != null
&& string.IsNullOrEmpty(x.Value.ToString()) == false))
{
if (fs.FileExists(IOHelper.MapPath(property.Value.ToString())))
{
var currentPath = fs.GetRelativePath(property.Value.ToString());
var propertyId = e.Copy.Properties.First(x => x.Alias == property.Alias).Id;
var newPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(currentPath));
fs.CopyFile(currentPath, newPath);
e.Copy.SetValue(property.Alias, fs.GetUrl(newPath));
//Copy thumbnails
foreach (var thumbPath in fs.GetThumbnails(currentPath))
{
var newThumbPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(thumbPath));
fs.CopyFile(thumbPath, newThumbPath);
}
isUpdated = true;
}
}
if (isUpdated)
{
//need to re-save the copy with the updated path value
sender.Save(e.Copy);
}
}
}
static void MediaServiceCreating(IMediaService sender, Core.Events.NewEventArgs<IMedia> e)
{
AutoFillProperties(e.Entity);

View File

@@ -18,10 +18,20 @@ namespace Umbraco.Web.PropertyEditors
[PropertyEditor(Constants.PropertyEditors.ImageCropperAlias, "Image Cropper", "imagecropper", ValueType = "JSON", HideLabel = false)]
public class ImageCropperPropertyEditor : PropertyEditor
{
/// <summary>
/// We're going to bind to the MediaService Saving event so that we can populate the umbracoFile size, type, etc... label fields
/// if we find any attached to the current media item.
/// </summary>
/// <remarks>
/// I think this kind of logic belongs on this property editor, I guess it could exist elsewhere but it all has to do with the cropper.
/// </remarks>
static ImageCropperPropertyEditor()
{
MediaService.Saving += MediaServiceSaving;
MediaService.Created += MediaServiceCreated;
ContentService.Copied += ContentServiceCopied;
}
/// <summary>
@@ -49,6 +59,67 @@ namespace Umbraco.Web.PropertyEditors
};
}
/// <summary>
/// After the content is copied we need to check if there are files that also need to be copied
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs<IContent> e)
{
if (e.Original.Properties.Any(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.ImageCropperAlias))
{
bool isUpdated = false;
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
//Loop through properties to check if the content contains media that should be deleted
foreach (var property in e.Original.Properties.Where(x => x.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.ImageCropperAlias
&& x.Value != null
&& string.IsNullOrEmpty(x.Value.ToString()) == false))
{
JObject json;
try
{
json = JsonConvert.DeserializeObject<JObject>(property.Value.ToString());
}
catch (Exception ex)
{
LogHelper.Error<ImageCropperPropertyEditor>("An error occurred parsing the value stored in the image cropper value: " + property.Value.ToString(), ex);
continue;
}
if (json["src"] != null && json["src"].ToString().IsNullOrWhiteSpace() == false)
{
if (fs.FileExists(IOHelper.MapPath(json["src"].ToString())))
{
var currentPath = fs.GetRelativePath(json["src"].ToString());
var propertyId = e.Copy.Properties.First(x => x.Alias == property.Alias).Id;
var newPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(currentPath));
fs.CopyFile(currentPath, newPath);
json["src"] = fs.GetUrl(newPath);
e.Copy.SetValue(property.Alias, json.ToString());
//Copy thumbnails
foreach (var thumbPath in fs.GetThumbnails(currentPath))
{
var newThumbPath = fs.GetRelativePath(propertyId, System.IO.Path.GetFileName(thumbPath));
fs.CopyFile(thumbPath, newThumbPath);
}
isUpdated = true;
}
}
}
if (isUpdated)
{
//need to re-save the copy with the updated path value
sender.Save(e.Copy);
}
}
}
static void MediaServiceCreated(IMediaService sender, Core.Events.NewEventArgs<IMedia> e)
{
AutoFillProperties(e.Entity);