At the moment I’m working on the replacement of a resource on an asset. While doing so a lot of things come to mind. Questions like:
- Blocking deletion of used assets is merged into master this week. But this of course only takes usage of assets referenced to a node into account. What if someone built a domain model with a reference to an asset or has a usage we can’t think of at the moment? Those assets can still be deleted right now
- What if constraints in the media model defer? This will happen, and we can not foresee all possibilities. So there will be different validation rules for media models. Like for example not being able to upload videos which are violating copyright or something fancy / exotic like that. But for now I just have the usecase of blocking duplicate filenames
- What if someone needs custom post-processing after replacing an asset like automatically uploading the new version of the asset to some platform? This is not that far away, as we already have (from the point of view of the media package) custom handling in Neos: removal / updating of the references in nodes.
I’ve some solutions in mind, but would like to refine them and see if you guys have other suggestions. If we would be CQRS ready we would have mechanisms like commands, validation of commands, command handlers and stuff. But implementing all those right now just for this usecase is a bridge too far atm.
Asking if an asset is still in use before removal is a responsibility of the Media API in my opinion. The current implementation (merged this week) in Neos is purely Neos based, and handled in Neos too. I would like to refactor this a bit so the mechanism can become more extendable.
I’m currently thinking about something like AssetUsageDataProviders or something like that, which could say if a resource is in use, and if so: how and where. This should then also return a defined set of data which can be used to render a visual representation of the usage (in Neos you now see a table with de document, content node, workspace, last modification and stuff like that) including a possible link where the usage can be seen / edited.
I now did start with creating a ReplaceResourceCommand which I use as a data transfer object. This easily allows using a validator object that is resolved by the ValidatorResolver. To extend validation it could behave like the ConjunctionValidator, and in some way retrieve other validators and merge the results.
On removal / updating of an asset some implementations will require additional steps. Like for example Neos which will update references and stuff. In my opinion those steps can depend on options like ‘Keep original filename on replace’ or ‘Do not replace those references’, or ‘Add a 301 redirect after updating the resource’ and so on.
The approaches which would currently be the easiest to implement would be a domain event listener or signal slot I think.
I can now of course hack the world away and start working with providers and validators that are configurable by settings (most flexible imho) . Besides that I’d probably add resolving of them based on interfaces. Also using a domain event or signal slot is pretty much a decision that can be made. But I’d really like to have some more views on the topic before I write too much code.
Any pro’s, con’s or additional ideas on the topic?