Upload comfortable mexican sofa cms image's to heroku from s3 bucket - heroku

I had successfully integrated comfortable mexican sofa CMS into an existing rails 4.1.2 application.
Now I want to upload images to heroku from s3 bucket.
Can anyone please tell the steps for that?

Sofa is using paperclip for attachments. So first, take a look here: http://www.rubydoc.info/gems/paperclip/Paperclip/Storage/S3
Then in initializers/comfortable_mexican_sofa.rb you'll find config.upload_file_options. This is how you'll override defaults.
config.upload_file_options = {
:storage => :s3,
:s3_credentials => ...
}

config.upload_file_options = {
:whiny => false,
:storage => :s3,
:s3_credentials => {"access_key_id" => ENV["S3_ACCESS_KEY_ID"], "secret_access_key" => ENV["S3_SECRET_ACCESS_KEY"]},
:bucket => ENV["S3_BUCKET_NAME"],
:s3_host_name => 's3 HOST NAME',
:path => "uploaded_files/:basename.:extension",
:styles => {:thumb => "850x850>" }
}
Give styles inside the config.upload_file_options which will take exact size of an image.
Which make more sense in uploading image else image pixels may vary.
The available configuration parameters are defined in paperclip, not comfy, and more information about there meaning can be found at:
http://www.rubydoc.info/github/thoughtbot/paperclip/Paperclip/Storage/S3

Just a note: Comfy has switched from Paperclip to ActiveStorage in 2017, so the answers are somewhat outdated.

Related

Change path of images uploaded on Amazon S3 using PaperClip in Rails

How can I change the path of the images I upload using paperclip and rails. I want the path to be inside my bucket's gov_id folder and the image just stays there without any subfolders. And also how to make the url of the image to follow this format: "https://s3-ap-southeast-1.amazonaws.com/BUCKET_NAME/GOV_ID/IMAGE_NAME.EXTENSION"
Note: I have a gov_id folder inside my bucket
I have an attachment model that looks like this:
class Attachment < ApplicationRecord
belongs_to :attachable, polymorphic: true
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => :s3,
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }
validates_attachment :image, presence: true
before_save :rename_file
def rename_file
extension = File.extname(image_file_name).gsub(/^\.+/, '')
new_image_file_name = "gov_#{self.attachable.reference_code}.#{extension}"
image.instance_write(:file_name, new_image_file_name)
end
end
this stores the image uploaded to my bucket but not inside the gov_id folder. It goes to attachments/images/000/000/013/original
And the url becomes "s3-ap-southeast-1.amazonaws.com/BUCKET_NAME/attachments/images/000/000/013/original/gov_UG2S463C.png?1500620951
The problem is that you are trying to assign it a new name and succeeding, but you aren't telling it to do this in a format that s3 understands.
The thing you have to remember, is that s3 buckets work based on keys and objects. If you look at an s3 bucket, the folder structure is just for show, for the most part. The file path (including the file name) is essentially the key, and the object is the image stored there (in this case).
So, the key you are assigning the image is the default paperclip path (source:
paperclip docs), which ends in :file_name
in this block
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => :s3,
You have a comma at the end of your has_attached_file, which I assume means you deleted things like bucket_name: (which is fine, but next_time, replace any sensitive info with a placeholder name. it makes the problem easier to understand).
You should have a path: symbol associated with the key used to access the s3 object. Usually, paperclip auto-generates this for you, but here you want to manually assign it. So you should be able to add something like this:
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => s3,
:path => "/gov_id/:class/:attachment/:style/:file_name"
If you would like the '000/000/001' then put :path => "/gov_id/:class/:attachment/:id_partition/:style/:file_name"
I assume you want to have style in there so that it will deal with both the :original and :thumb style appropriately.
Also, instead of using a before_save, you might want to look into Paperclip.interpolates
something like:
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => s3,
:path => "/gov_id/:class/:attachment/:style/:replaced_file_name"
Paperclip.interpolates :replaced_file_name do
extension = File.extname(image_file_name).gsub(/^\.+/, '')
new_image_file_name = "gov_#{self.attachable.reference_code}.#{extension}"
new_image_file_name
end

Documentation for chef's attribute construct in metadata.rb file

I am new to chef and seeing the following code in metadata.rb file for a project handed over to me from someone.
attribute 'setup',
:required => 'required',
:default => 'internal',
:format => {
:category => '1.Global',
:order => 1,
:filter => {'all' => {'visible' => 'true'}},
:form => {'field' => 'select', 'options_for_select' => [
['External', 'external'], ['Internal','internal']
]
}
}
I have gone through both https://docs.chef.io/config_rb_metadata.html and
https://docs.chef.io/attributes.html but unable to understand most of the non-obvious constructs like category, order, filter, form etc.
Are these not part of the standard chef packages?
The metadata fields still exist but nothing in the Chef ecosystem ever used them (except maybe old versions of Rightscale but that's not really "Chef ecosystem" anymore). Because of this we remove most of the docs about the feature. You can delete at will most likely, unless you have custom tools that use the data.

Paperclip - allow only PDF

How can allow to use upload only some kind of files, for example only PDF, Word or Excel files?
I use Paperclip gem.
Use content_type validation, like here:
validates_attachment :avatar, :content_type => { :content_type => "application/pdf" }

Rails paperclip plugin

I am a newbie. I am trying to upload an image through paperclip. The url and path code is working but the style option is not working. This is my code:
class User < ActiveRecord::Base
has_attached_file :image, :styles => { :small => "150x150>" },
:url => "/assets/users/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/users/:id/:style/:basename.:extension"
end
When I use the style it doesn't work. If I remove the style option it does work. Please help me out!
Not sure if this will work but try :style_:basename.:extension

Rails3 mailer with image_tag ignoring host

I've got a Rails3 mailer layout that include images.
This ones are used like :
image_tag("emails/top.gif", :width => "700", :height => "10", :alt => "")
As of Rails 2, this images included the host and produced the expected result. However, since Rails3 the config.action_mailer.default_url_options seems to be ignored.
Is there anything I'm missing?
Update
my config/environment/development.rb include:
config.action_mailer.default_url_options = { :host => 'mydomain.tld' }
Needs to use config.action_mailer.asset_host = 'http://mysite.com' in your environment config file
Credits: wmoxam in #rubyonrails

Resources