I am trying to catch the event of uploading images to Asset Management in EPiServer Commerce Manager 7.0 in order to create 3 folders in the original folder where the images were uploaded, in which I will upload resized images.
For instance, if the user uploads Image1.jpg to Folder1, I will need to do the following:
- create the "fullsize" folder and move the original file (Image1.jpg)
- resize Image1.jpg to some dimensions (47x41) and move the result to a new folder called "icon"
- resize Image1.jpg to other dimensions (306x265) and move the result to a new folder called "thumbnail"
So, at the end of the process, Folder1 will contain 3 folders: "fullsize" (containing Image1.jpg), "icon" (containing Image1-icon.jpg) and "thumbnail" (containing Image1-thumbnail.jpg).
How do you suggest I do this? Is there an event that I can attach to in order to obtain this?
BlobStorage has an event named CreatedEvent:
static public event BlobEventHandler CreatedEvent;
If you are using the default PublicDiskStorageProvider, whenever it creates a blob, it will fire this event. You can hook into this to check if the blob is a image and create thumbnails for it.
I am using the default PublicDiskStorageProvider, but where how do I use the CreatedEvent? Do I need to create a IInitializableModule class for this? Can you give me an example?
Given you're using Commerce R3 (7.0), no IInitializableModule is available. You can use Global.asax or IHttpModule instead.
Global.asax would be a simpler solution, just register this in your Application_Start method:
BlobStorage.CreateEvent += <your method here>
I have added the event in Global.asax and I have put a breakpoint in the event, but the breakpoint is not hit. Also, I have put this line in the event just to see if the event is fired or not:
but it doesn't put anything in the logs when I am uploading an image in Commerce Manager.
Here's what I did to verify that I'm able to attach to the event.Assuming you've got a Global.asax with no code-behind...1. Add an Import directive for the Mediachase.BusinessFoundation.Blob namespace at the top.
<%@ Import Namespace="Mediachase.BusinessFoundation.Blob" %>
2. In the server-side script block, add a method at the end to be used to handle the Created event of BlobStorage.
void BlobStorage_CreatedEvent(object sender, BlobEventArgs args)
3. In the server-side script block, find Application_Start to add some code at the end to do sanity check logging and attach to the CreatedEvent event of BlobStorage.
LogManager.GetLogger(GetType()).Debug("BlobStorage.CreatedEvent += new BlobEventHandler(BlobStorage_CreatedEvent);");
BlobStorage.CreatedEvent += new BlobEventHandler(BlobStorage_CreatedEvent);
4. Double-check log4net.config at the web root. Make sure that the root element has the right appenders enabled. Make sure the root/level/@value attribute is set to the right level (DEBUG in my case).
5. Restart the web app, try uploading a new image, and check the logs.You should see entries similar to the following.
2014-10-27 10:14:26,605  DEBUG ASP.global_asax - BlobStorage.CreatedEvent += new BlobEventHandler(BlobStorage_CreatedEvent);2014-10-27 10:14:44,390  DEBUG ASP.global_asax - Testing!!!
6. This test worked for me with both DiskBlobStorageProvider and PublicDiskStorageProvider (See \Configs\baf.config - blobConfiguration/storageProvder/@defaultProvider). If you reflect both classes in the Mediachase.BusinessFoundation.dll assembly (18.104.22.168) in the Mediachase.BusinessFoundation.Blob namespace, you'll see that both support the BlobStorage class's CreatingEvent and CreatedEvent. The DiskBlobStorageProvider supports a few more BlobStorage events, whereas PublicDiskStorageProvider does not.
I have made the changes you proposed, but I don't see anything in the logs. Do you have any idea why the event is not triggering?
Now I can see the message in the logs.
Can you give me an example of resizing the image in args.NewBlobInfo? I also want to see how you can create a folder on the same level as the one that the image is being uploaded and save the resized image in the newly created folder.
Can you please provide an example for this?