Permission denied # dir_s_mkdir Error - ruby-on-rails

I've been searching around for a while now but can't seem to find the answer.
I'm using paperclip and postgresql database to upload and store files.
The error I am getting is :
Errno::EACCES in DocumentsController#create
Permission denied # dir_s_mkdir - /documents
And the error code is specifically referring to this section in the documents controller:
def create
#document = current_user.documents.build(documents_params)
if #document.save
redirect_to #document
else
render 'new'
end
end
I recently switched my database from sqlite to postgresql and it is working perfectly fine online (I have uploaded it with heroku), just not in development.
Also, I am able to edit and update documents that have been uploaded already in development, just not able to upload any.
Are there any config files or something that I need to modify to grand permission for # dir_s_mkdir?

Finally I managed to fix this problem.
Because I had modified my database to use PostgreSQL with Heroku I needed to also modify my Document model, to accomodate for both production and development environments.
I also had to change the :url that the document object was assigning to in development. The updated :url became:
:url => "/system/documents/pdfs/:id/:basename.:extension"
Below is the updated document.rb model (for the paperclip section):
if Rails.env.development?
has_attached_file :pdf, :use_timestamp => false,
:url => "/system/documents/pdfs/:id/:basename.:extension",
:path => ":rails_root/public/system/documents/pdfs/:id/:basename.:extension"
validates_attachment_content_type :pdf, :content_type => ["application/pdf","application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"]
else
has_attached_file :pdf, :use_timestamp => false
validates_attachment_content_type :pdf, :content_type => ["application/pdf","application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"]
end
Many answers I referred to were saying to use either:
sudo chown -R username app_path
/* or */
chmod -R 777 PATH_TO_APP/uploads
/* or */
chmod -R 777 PATH_TO_APP/tmp
Although changing ownership of a file/folder is not a good option, as it sets every file as executable, readable, and writeable by anyone.

Related

Paperclip validation issue on production

I have a Problem when I deploy my application on google cloud I get this error
has contents that are not what they are reported to be
Locally it works fine! I already tried to using the command_path. So I really don't know what I have to do next...
This is my model
has_mongoid_attached_file :image,
:styles => { :large => "380x380!" , :medium => "240x240", :small => "120x120!" },
:storage => :fog,
:fog_public => true,
:fog_directory => 'XXXX',
:path => "images/:id/:style/:basename.:extension",
:fog_credentials => { :provider => 'Google',
:google_storage_access_key_id => 'XXXXX',
:google_storage_secret_access_key => 'XXXXX'}
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Thank you for your efforts. I hope you guys can help me
Okay I found a result. I just created a initializers/paperclip.rb file
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
Right now it work's perfectly for me.
If you have problems with ImageMagick on App Engine using Rails see this link
That issue occurs because the content-type discovered from file command returns empty string.
Actually system is not able to find the file executable so a exception is raised and empty string is returned back.
Check the code below
begin
Paperclip.run("file", "-b --mime :file", :file => '/tmp/RackMultipart20160826-15649-kwvnq2.png').split(/[:;]\s+/).first
rescue Cocaine::CommandLineError
""
end
Solution:-
Add below line in you initializer file.
Paperclip.options[:command_path] = '/usr/bin'
Looks like Google Cloud can't determine MIME type of uploaded files.
You can map file extensions to types in you initializer (application.rb, production.rb or create initializers/paperclip.rb)
Paperclip.options[:content_type_mappings] = {
:jpg => "image/jpeg",
:png => "image/png",
:gif => "image/gif"
}
But this way spoofing check won't be performed for image files.
I know I am late to the party, but in working with a legacy RoR system, I ran into this issue. The problem arose in setting the app up in Docker. Ultimately paperclip calling imagemagick was attempting to use file to identify mime-type and the minimal Docker did not have it installed. apt-get install file fixed it.

ckeditor in rails with mongodb

I am using ck editor in rails having database mongo db. I followed the link https://github.com/galetahub/ckeditor . I am succes in doing work with the help of ckeditor.
since my view.html.erb code is like this
<%= f.cktext_area :description, :toolbar => 'Easy', :width => 800, :height => 200 %><br>
and my show page is
<%= raw#department.description %>
it does not works for file cases.
I have my model attachment_file.rb is
class Ckeditor::AttachmentFile < Ckeditor::Asset
has_mongoid_attached_file :data,
:url => "/ckeditor_assets/attachments/:id/:filename",
:path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"
validates_attachment_size :data, :less_than => 100.megabytes
validates_attachment_presence :data
def url_thumb
#url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
It is working for image cases but not working for zip file or any attachement. when it comes to the file cases it can upload file successfully with its path. but to download that file by user it doesnot work. I mean backend works properly for all features. But lacks to download that uploaded file stops by
`javascript:void(0)/*130*/
i have found the answer of this problem . First run this in terminal.
$ sudo chmod -R 777 /usr/share/ruby-rvm/gems/ruby-1.9.3-p194/gems/ckeditor-3.7.1
follow this path in your computer since i am using linux and my gem file locates here.
/usr/share/ruby-rvm/gems/ruby-1.9.3-p194/gems/ckeditor-3.7.1/vendor/assets/javascripts/ckeditor/plugins/attachment/dialogs
and open attachement.js file and edit it with the code that u find from tha above link.
click
Now ck editor will works for file attachment also.
Seem like you encounter this bug in CKeditor:
It sugests adding before filter as fix eg:
# app/model/department.rb
before_save :fix_ckeditor_attachment_paths
def fix_ckeditor_attachment_paths
if self.description.index(/_cke_saved_href/)
self.description = self.body.gsub(/_cke_saved_href/, 'href')
end
end

PaperClip -- save a new attachment without deleting the old one

I am using Paperclip (w/ Amazon s3) on Rails 3. I want to attach a new file to my model without replacing the old file. I don't want the old file to be accessible, I only want to have it there on s3 as a back up.
Do you know if there is a way of telling paperclip to take care of it, itself?
in post.rb I have:
has_attached_file :sound,
:storage => :s3,
:s3_credentials => "....",
:styles => {:mp3 => {:format => :mp3}},
:processors => [:sound_processor],
:s3_host_alias => '....',
:bucket => '....',
:path => ":attachment/:id/:style/out.:extension",
:url => ":s3_alias_url"
and the processor is as follows:
class Paperclip::SoundProcessor < Paperclip::Processor
def initialize file, options = {}, attachment = nil
super
#format = options[:format] || "mp3"
#current_format = File.extname(#file.path)
#basename = File.basename(#file.path, #current_format)
end
def make
src = #file
dst = Tempfile.new([#basename,".#{#format}"])
dst.binmode
cmd = "ffmpeg -y -ab 128k -t 600 -i #{File.expand_path(src.path)} #{File.expand_path(dst.path)}"
Paperclip.log(cmd)
out = `#{cmd}`
raise Paperclip::PaperclipError, "processor does not accept the given audio file" unless $?.exitstatus == 0
dst
end
end
Here's what I do. I timestamp the filename before saving it (to prevent another file with the same name overwriting the original), and force paperclip to never delete anything.
before_create :timestamp_filename
def timestamp_filename
fname = Time.now.to_s(:db).gsub(/[^0-9]/,'') + '_' + sound_file_name
sound.instance_write(:file_name, fname)
end
# override paperclip's destroy files
# method to always keep them around
def destroy_attached_files
true
end
It's quite straightforward to add versioning to your models, recently I user CarrierWave and paper_trail in combo to achieve this. We were deploying to Heroku so S3 was in the mix too.
The reason I'm posting this as an answer is because, although controversial, I don't think libraries like PaperClip should support backing up files, a library specific to solve that problem feels better to me personally.
Here is a good resource that you can take a look at: http://eggsonbread.com/2009/07/23/file-versioning-in-ruby-on-rails-with-paperclip-acts_as_versioned/
It creates new versions of a file. Hope this helps.

Rails 3 & Paperclip: "not recognized by the 'identify' command"

A few system details:
Mac OS X Lion 10.7.2, Rails 3, Paperclip gem.
ImageMagick 6.7.3-0, binaries installed using MacPorts in /opt/local/bin.
Using WEBrick in development environment.
I'm trying to upload photos that should get resized down into thumbnails. When I do so, the original uploaded file is saved and can be accessed via HTTP correctly. When I try to accessed the thumbnail version, I get this error:
Routing Error
No route matches "[file_URL]"
Before that, when uploading a valid PNG file, I see this error in my WEBrick log:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/n4/62q22gb52rjd0h13cx_j8vv40000gq/T/stream20111020-24984-17560xt-0.png is not recognized by the 'identify' command.>
Doing which identify outputs:
/opt/local/bin/identify
Calling identify with the path of the uploaded file correctly identifies the file as a PNG file.
I confirmed rails server starts the development environment. I added the following in config/environments/development.rb:
Paperclip.options[:command_path] = "/opt/local/bin"
My Photo model has the following:
class Photo < ActiveRecord::Base
has_attached_file :file, :default_style => :view, :styles => {
:view => { :geometry => '520x390>', :format => 'jpg' },
:preview => { :geometry => '160x120>', :format => 'jpg' } }
validates_attachment_content_type :file,
:content_type => [ 'image/jpeg', 'image/pjpeg', 'image/png' ]
end
My Photos controller:
class PhotosController < ApplicationController
# ...
def create
#photo = Photo.create params[:photo]
end
end
Like I said, I can access the original images in the URL path system/files/:id/original, but the resized versions I want aren't accessible and are not found in the filesystem. WEBrick's log suggest Paperclip can't even have ImageMagick identify the images before resizing them.
Any ideas? Thanks in advance!
This may be a duplicate question, as stated above, but this problem was caused by a recent bug in ImageMagick 6.7.3-0 fixed soon after in 6.7.3-1. MacPorts has also been updated with the new release.
In 6.7.3-0, stating the frame number in the file path (e.g. identify /path/to/file.png[0]) caused ImageMagick to segfault ("Segmentation fault 11"). Paperclip specified the frame number to ensure its processing the first frame or page of an image file.
The new release of ImageMagick solved this problem. The bug was mentioned on MacPorts:
https://trac.macports.org/ticket/31643

Paperclip::NotIdentifiedByImageMagickError image is not recognized by the 'identify' command

i´m getting this error when editing a model specifically when i delete an image associated to it and I select another:
Paperclip::NotIdentifiedByImageMagickError in Admin/packsController#update
Chrysanthemumprueba4.jpg is not recognized by the 'identify' command.
C:/Users/.../vendor/plugins/thoughtbot-paperclip-fc792c8/lib/paperclip/geometry.rb:24:in `from_file'
But when i create a new pack and I select images for it, it works ok.
I have two tables: packs and pack_images,and pack_images has the photos for the pack associated, here are the relations:
class Pack < ActiveRecord::Base
has_many :pack_images, :dependent => :destroy
end
class PackImage < ActiveRecord::Base
belongs_to :pack
attr_accessor :height, :width
has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:principal => "240x240>", :original => "400x400>", ...}
end
This is the controller's action that throws me the error:
def update
#pack = Pack.find(params[:id])
#pack.pack_products
unless params[:pack][:pack_images_attributes].nil?
params[:pack][:pack_images_attributes].count.times do |i|
unless params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo].blank?
file = params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo]
dimensions = Paperclip::Geometry.from_file(file)
#pack.pack_images[i].width = dimensions.width
#pack.pack_images[i].height = dimensions.height
end
end
end
respond_to do |format|
#pack.update_attributes(params[:pack])
format.html { redirect_to(admin_pack_path(#pack.id), :notice => 'Pack updated') }
end
I noticed that, when updating I get less parameters (only the photo's name) than when creating (photo's name,file type,width,height,etc).
I hope you can help me
Thank you very much
it used to work fine for pdf and images, tried out for an hour or so, followed everything I googled later the problem was found in my model has_attached_file :attachment,
:styles => {:original=> "125x125#"}
had to comment this line, and it worked for other attachments like docx or odt etc..
so in your case :styles => {:principal => "240x240>", :original => "400x400>"}
check out and comment.
Locate the path of the identify command like this:
$ which identify
For me the above command prints this: /usr/local/bin/identify
Add this in some initializer file:
Paperclip::Attachment.default_options[:command_path] = "/usr/local/bin"
One reason this error occurs is when you try to determine the dimensions of an image that does not exist:
Paperclip::Geometry.from_file(nil)
This command will hang and cause this error.
Try to run the "identify" command from ImageMagick on this image. It seems something with your ImageMagick install.
I had the exact same issue. Windows 8 64bit, Rails 4, ImageMagick-6.8.7-1-Q16-x64-static.exe. Do this :
In the root of your rails app (from Git Bash)
$ which identify
/c/Program Files/ImageMagick-6.8.7-Q16/./identify
Then
$ cd "/c/Program Files/ImageMagick-6.8.7-Q16"
Don't forget the quotes. Copy all executables to you /bin directory. I actually copied all these files to be certain.
$ cp * /bin
And voila paperclip works!

Resources