I recently upgraded to umbraco 6.2.1 and decided to use the what I understand now recommended way of attaching event handlers for documents and media in 6.2 and 7.
So I used the technique described in MediaService-Events to hook up my media event handler (which simply has to scale down any uploaded image).
In my previous implementations (using the 'old Media.BeforeSave concept) I always looked at the "umbracoFile" property to get the file location of the saved image and used that to create a reduced version. Now surprisingly,this doesn't immediately work with the new event handling.
It turns out that the 'Saved' event is called twice: - The first time, none of the media properties are available. - The second time all property values are correctly assigned.
Luckily, there is an IMedia method 'IsNewEntity()', which is set true the first time and false the second time, to get the job done, but I am wondering why 'Saved' is actually called twice.
No, I didn't investigate this more deeply. I'am now always calling 'IsNewEntity()' on an IMedia object and only deal with these objects when the method returns false.
I get a similar problem with Version 7.2.5
I hook into the MediaService.Saving event to scale down an image as well. The first time media.GetValue<string>("umbracoFile") returns a valid path to the image whereas the second time it returns a Json object. I have had to try to parse it into a JObject in order to return a valid path from .src. Otherwise the whole thing errors out with "Illegal Characters in path"
if (!String.IsNullOrEmpty(media.GetValue<string>("umbracoFile")))
{
path = media.GetValue<string>("umbracoFile");
}
try
{
dynamic jsonPath = JObject.Parse(path);
if (jsonPath.src != null)
{
path = jsonPath.src.ToString();
}
}
catch(JsonReaderException) {
//Do nothing as it will have been a normal IMedia object.
}
MediaService 'Saved' event invoked twice
I recently upgraded to umbraco 6.2.1 and decided to use the what I understand now recommended way of attaching event handlers for documents and media in 6.2 and 7.
So I used the technique described in MediaService-Events to hook up my media event handler (which simply has to scale down any uploaded image).
In my previous implementations (using the 'old Media.BeforeSave concept) I always looked at the "umbracoFile" property to get the file location of the saved image and used that to create a reduced version. Now surprisingly,this doesn't immediately work with the new event handling.
It turns out that the 'Saved' event is called twice:
- The first time, none of the media properties are available.
- The second time all property values are correctly assigned.
Luckily, there is an IMedia method 'IsNewEntity()', which is set true the first time and false the second time, to get the job done, but I am wondering why 'Saved' is actually called twice.
Hi Jos,
I was wondering if you ever figured out why the Saved event is called twice?
Gr Roald
Roald,
No, I didn't investigate this more deeply. I'am now always calling 'IsNewEntity()' on an IMedia object and only deal with these objects when the method returns false.
Greetings, Jos
I get a similar problem with Version 7.2.5 I hook into the MediaService.Saving event to scale down an image as well. The first time
media.GetValue<string>("umbracoFile")
returns a valid path to the image whereas the second time it returns a Json object. I have had to try to parse it into a JObject in order to return a valid path from .src. Otherwise the whole thing errors out with "Illegal Characters in path"is working on a reply...