Reputation: 123
I have a one User - one Channel relationship
(@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
The channel has an avatar, which is stored on the local drive, and the database stores the path to the photo.
Where and how would be the best place to remove the file from the local drive when removing the Channel? In Facade, Service or check once a day if the database contains an entity and the folder corresponds to it and if the folder is there but the entity is not, then delete it?
At the moment I delete simply with the entityManager, but it does not delete the photo from the local drive
repository.deleteById(id);
I'm sorry if I didn't phrase the question correctly, I'm just not very experienced at it and I tried to describe the problems in as much detail as possible.
Upvotes: 4
Views: 70
Reputation: 19108
You can do it on the service layer but please consider the following:
@Transactional
in your service method. This way if something goes side ways the database action is rolled back and you can try again later.the other solution mentioned, having a job scheduled in the application which regularly inspects the folder with photos and deletes any non used photo, sounds also good but in this case consider carefully the following
Upvotes: 1