Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if Dragonfly file exists before fetching it #350

Closed
DelawareConsulting opened this issue Jun 25, 2014 · 4 comments
Closed

Check if Dragonfly file exists before fetching it #350

DelawareConsulting opened this issue Jun 25, 2014 · 4 comments

Comments

@DelawareConsulting
Copy link

I'm retrieving the Dragonfly file with Dragonfly[:images].fetch(uid) but if I already deleted the file I'm getting Dragonfly::DataStorage::DataNotFound which is logical because I'm trying to fetch a file that is deleted.

The file is automatically deleted when send to the Backend, but if an error occurs somewhere while processing the form I return to index and the system will try to upload it again.

So I'm currently fixing it like this:

  file = Dragonfly[:images].fetch(uid) rescue ""
  Backend.add_attachment(file.name, file.data, token: @user.token) if file
  Dragonfly[:images].destroy(uid) rescue "" # Delete the file after uploading to Backend

But it would be better if I could do something like this

  file = Dragonfly[:images].fetch(uid) if Dragonfly[:images].exists?(uid)
  if file
    Backend.add_attachment(file.name, file.data, token: @user.token) 
    Dragonfly[:images].destroy(uid)
  end
@markevans
Copy link
Owner

hi - the problem with adding #exists? is that this will need to be added to the spec for datastores, which is currently very simple (read, write and destroy).
I don't fully understand your use case, but how are you keeping tabs on uids? It seems as if it might be the wrong place to testing whether a piece of content exists - if you're keeping tabs on uids yourself you should probably already know whether it exists or not? Are you using the datastore just as temporary storage?

@DelawareConsulting
Copy link
Author

We are storing:

  # First things first, upload all files to the Rails server
  (params[:attachments] || {}).each do |i, hash|
    file = {}
    file["type"] = hash["type"]
    if data = hash["file"]
      file["file"] = Dragonfly[:images].store(data)
    elsif uid = hash["cache"]
      file["file"] = uid unless uid.blank?
    end
    @cached_files << file
  end
  @cached_files = @cached_files.reject {|f| f["file"].blank?}


 if @files.present?
    @files.compact.each do |uid|
      file = Dragonfly[:images].fetch(uid) rescue ""
      Backend.add_attachment(file.name, file.data, token: @user.token) if file
      Dragonfly[:images].destroy(uid) rescue "" # Delete the file after uploading to Backend
    end
  end


  if @cached_files.present?
    @cached_files.compact.each do |data|
      uid = data["file"]
      type = data["type"]
      next unless uid
      file = Dragonfly[:images].fetch(uid) rescue ""
      Backend.add_attachment_with_type(file.name, file.data, type, token: @user.token) if file
      Dragonfly[:images].destroy(uid) rescue "" # Delete the file after uploading to Backend
    end
  end

@DelawareConsulting
Copy link
Author

As seen above we first upload the file to the rails server if the form is validated we try to send it to the Backend. But after this upload an error can still occur with other data and this will perform return render :index. When the user tries to submit the page again the same code will be launched since @cached_files is still present as before only difference is that the file doesn't exist anymore on the server because we deleted it in the previous run.

@markevans to answer the question even more since the @cached_files exist the UID is still in them. So actually we try to retrieve file and send it to the backend but it doesn't exist. So it would be awesome if I was able to check if the file still exists before trying fetching a file that doesn't exist.

@markevans
Copy link
Owner

I don't want to add this to the datastore spec, however it could be added to an individual datastore, e.g.
Dragonfly.app.datastore.exists?(uid)
Feel free to do a pull request - I'll close this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants