jeudi 17 septembre 2015

Rails Errno::ENOENT: No such file or directory when saving an upload

I'm having the following issue with my Rails API.

I am trying to save a file into a temp directory. My initial assumption was that I should be able to save it into the system /tmp directory, because that's what that directory is for. So I have the following code in my gallery_images_controller.rb:

    def upload
        # set the upload
        upload = params[:qqfile].original_filename
        # we want to save files in tmp for now
        directory = "/tmp"
        ap(directory)
        # create the full path for where to save the file
        path = File.join(directory, upload)
        ap(path)
        # get the md5 of the file so we can use it as the key in the json response
        md5 = Digest::MD5.file(path).hexdigest
        # create an empty file because Ruby is being dumb
        # FileUtils.touch(path)
        # save the file
        File.open(path, "w+b") { |f| f.write(params[:qqfile].read) }
        # render the result with the md5 as the key and the filename and full path
        render json: {success: true, upload: {"#{md5}": {filename: upload, full_path: path}}}, status: 200
    end

When I send the post request with the file I get the following error:

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /tmp/3Form-Museum-of-Science-25.jpg"}

I also tried to save the file in the Rails.root tmp folder and get the same error:

directory = "#{Rails.root}/tmp/"

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /vagrant/tmp/3Form-Museum-of-Science-25.jpg"}

I've also tried the mode as w+band wb to no avail.

The documentation for Ruby (http://ift.tt/1KUdEyV) says that the the w and w+ modes should create the file if it doesn't exist. Which is exactly what I want it to do, and it's not.

Also, I have checked the permissions on the folders. The /tmp folder, as you would expect, has 777 and the rails root /vagrant/tmp has 755 just like all the other folders in the rails root.

Please help!


System Information:

  • Dev: Vagrant box running ubuntu 14.04, unicorn and nginx
  • Prod: Amazon EC2 running ubuntu 14.04, unicorn and nginx

Aucun commentaire:

Enregistrer un commentaire