Multiple MediaFactory for same extension - Chain of resposibility
Hello.
I need to upload files in a specific directory as a specific Media Type. I can implement this using a custom IMediaFactory and configure DMU to use this factory for "*" extensions. However I do not want to use this MediaFactory for each and every unmapped extension.
What I suggest is to implement a Chain of resposibility pattern [1] in the Config.GetMediaFactory() method. Instead of quering the XML for only one mediaFactory, you could select all that matches the file extension. Then loop through the factories and call a IMediaFactory.CanCreate(int parentNodeId, HttpPostedFile uploadFile) method. If this call returns true the factory can be used to create the Media item. This allows IMediaFactory implementations to do complex logic for validating whether or not they should be used to create the media item for the upload file.
How does this sound? I can implement the functionality and post a patch file if someone is interested..
Definatley sounds like a nice idea, so by all means create a patch and I'll try it out.
Re the seperation of assembley and type isn't for any particular reason, other than it just make it easier to reference each part, plus it tends to be how other configs in umbraco work.
I have posted a patch on your codefkex page [1]. I was very tempted to correct the typo of "mediaFacory" in the config XML file, but I choose not to :-)
I guess the typo and must of the config schema is borrowed from the Multiple File Upload plugin..
Great work, I'll get this incorporated into the next release (I have a few other updates to do, so will get them all sorted at the same time). I'll also fix the spelling issue to (I did use a lot of the Multiple File Upload plugin as a base for DMU)
PS I'd be interested to know what other refactorings you have made? Is it specific to your needs? Or something you think the whole project would benefit from?
Sounds good Matt. I will be happy to help if needed.
Well, the refactoring started as an interest to extract the GetMediaFactory() method in the Config class. It seems misplaced to me. During the refactoring I also wanted to change the logic in factory method to fix with the bug (in my patch) I described on your codeflex page. I then started to implement a MediaFactoryCreator (a MediaFactory factory) which caches the created media factories and reuses them. I will also just parse the config XML once. The MediaFactoryCreator could either be a static class or a singleton.
Its basically just an internal refactoring that no end-users would benefit from, but I would be happy to share the code. Though its not completed yet.
Btw, all that thumbnail generation code, is that really necessary? There's no way for Umbraco to create the thumbnails? It just feels wrong.
I'd definatley be intersted to see what you come up with. I'm all for making improvments =)
RE the thumbnails, I'm not sure, again, most of this is borrowed from eslewhere (I don't confess to being the best coder in the world, I just know how to reuse code, and get things done =) I'll have a look in the core source though to see if there is anything we could reuse.
Multiple MediaFactory for same extension - Chain of resposibility
Hello.
I need to upload files in a specific directory as a specific Media Type. I can implement this using a custom IMediaFactory and configure DMU to use this factory for "*" extensions. However I do not want to use this MediaFactory for each and every unmapped extension.
What I suggest is to implement a Chain of resposibility pattern [1] in the Config.GetMediaFactory() method. Instead of quering the XML for only one mediaFactory, you could select all that matches the file extension. Then loop through the factories and call a IMediaFactory.CanCreate(int parentNodeId, HttpPostedFile uploadFile) method. If this call returns true the factory can be used to create the Media item. This allows IMediaFactory implementations to do complex logic for validating whether or not they should be used to create the media item for the upload file.
How does this sound? I can implement the functionality and post a patch file if someone is interested..
[1] http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
Btw, what is the reason behind separating the assembly and type attribute the XML config? Most standard .NET config settings use one type attribute:
Anyway, thanks for a great product!
/Andreas
Hi Andreas,
My appologies, I totally forgot to reply to you.
Definatley sounds like a nice idea, so by all means create a patch and I'll try it out.
Re the seperation of assembley and type isn't for any particular reason, other than it just make it easier to reference each part, plus it tends to be how other configs in umbraco work.
All the best
Matt
Hi Matt,
I have posted a patch on your codefkex page [1]. I was very tempted to correct the typo of "mediaFacory" in the config XML file, but I choose not to :-)
I guess the typo and must of the config schema is borrowed from the Multiple File Upload plugin..
[1] http://dmu4umb.codeplex.com/workitem/2
/Andreas
Hi Andreas,
Great work, I'll get this incorporated into the next release (I have a few other updates to do, so will get them all sorted at the same time). I'll also fix the spelling issue to (I did use a lot of the Multiple File Upload plugin as a base for DMU)
Thanks again.
Matt
PS I'd be interested to know what other refactorings you have made? Is it specific to your needs? Or something you think the whole project would benefit from?
Matt
Sounds good Matt. I will be happy to help if needed.
Well, the refactoring started as an interest to extract the GetMediaFactory() method in the Config class. It seems misplaced to me. During the refactoring I also wanted to change the logic in factory method to fix with the bug (in my patch) I described on your codeflex page. I then started to implement a MediaFactoryCreator (a MediaFactory factory) which caches the created media factories and reuses them. I will also just parse the config XML once. The MediaFactoryCreator could either be a static class or a singleton.
Its basically just an internal refactoring that no end-users would benefit from, but I would be happy to share the code. Though its not completed yet.
Btw, all that thumbnail generation code, is that really necessary? There's no way for Umbraco to create the thumbnails? It just feels wrong.
Hi Andreas,
I'd definatley be intersted to see what you come up with. I'm all for making improvments =)
RE the thumbnails, I'm not sure, again, most of this is borrowed from eslewhere (I don't confess to being the best coder in the world, I just know how to reuse code, and get things done =) I'll have a look in the core source though to see if there is anything we could reuse.
All the best
Matt
Hey Andreas,
Just to let you know, the chain of responsibility pattern has been applied to the latest version (2.0.2) which was released yesterday.
If you have any other ideas for improvements, don't hesitate to let me know.
Many thanks
Matt
Sounds great Matt :-) I will post my other refactorings when I have properly tested them. I will post a msg on the codeplex page.
/Andreas
is working on a reply...