Rails Dragonfly find files in use

2019-09-12 08:28发布

问题:

We are using Dragonfly for file and image upload in the app and in the Rails Admin.

Dragonfly in app part Users can apply for jobs and add their resume as attachment. When user uploads attachments and sucesfully apply for a job the files get deleted.

Dragonfly in Rails Admin part Admins can create pages with attachments and images in the Rails Admin part, these attachments are linked via the specific tables.

Problem When a user is applying for a job but doesn't finish the apply the files remain unused on the server. Now dragonfly stores the files from the Admin on the same place as the user uploaded files.

Question Is it possible to filter out the files that are not linked in the DB and bulk delete these files?

回答1:

I've fixed this simply by supplying a path when storing the data:

path = "ApplyAttachments/" + Time.now.strftime("%d%m%Y_%H%M") + "_" + rand(1000).to_s + "_" + data.original_filename.to_s
file["file"] = Dragonfly[:images].store(data, :path => path)