I'm intercepting image uploads to resize images to a maximum size. Easy-peasy except...
I can't seem to get the IMedia properties to update.
// Tap into the Saving event
MediaService.Saving += (sender, args) =>
{
// Resize code removed for brevity. It works.
// Set the correct meta values for the image.
media.SetValue(Constants.Conventions.Media.Width, newWidth);
media.SetValue(Constants.Conventions.Media.Height, newHeight);
media.SetValue(Constants.Conventions.Media.Bytes, bytes);
sender.Save(media, 0, false);
}
Updating media properties on upload.
I'm intercepting image uploads to resize images to a maximum size. Easy-peasy except...
I can't seem to get the
IMedia
properties to update.Am I saving those values at the wrong time?
I hope your not doing GetById to get the media item else your writing to it before it's saving so it will then not save anything.
In other words: show your
var media = ... something
? :)Nah... I'm looping through the args.SavedEntities enumerable. Basically an enhanced version of the preprocessing example here. http://24days.in/umbraco/2014/all-your-images-are-belong-to-umbraco/
Could it be that your "saving" event happens before the core code sets the same properties?
EDIT: My sample was a false positive! Refreshing the media revealed that it was in fact not saved, just modified for display.
Fixed sample:
The above seems to work.
Yeah that get's it Morten thanks. I thought it was probably a timing issue.
Morten Christensen reckons this is the probable cause of my issue.
https://github.com/umbraco/Umbraco-CMS/blob/07e9a3a4ea27e167816e2cce8cea1b89258b01a8/src/Umbraco.Web/Strategies/DataTypes/LegacyUploadFieldWorkaround.cs
is working on a reply...