I'm using paperclip, imagemagick, rmagick to upload the images to s3. Previously the image upload functionality is working well on ruby 1.9.3,'rails', '3.2.11'. After upgrading ruby 2.0.0 and 'rails', '4.0.3' it throughing error.
In store.rb
#paperclip
has_attached_file :logo,
styles: {
thumb: ["40x40#", :png],
small: ["400x400>", :png],
masked_with_overlay: ["288x64", :png],
masked_with_nearby_overlay: ["238x47#", :png],
masked_with_rewards_overlay: ["288x64", :png],
masked_with_overlay_for_old: ["288x64", :png],
masked_with_nearby_overlay_for_old: ["238x47#", :png],
masked_with_rewards_overlay_for_old: ["288x64", :png],
masked_full_card_image: ["576x128", :png],
masked_logo_only: ["576x128", :png]
},
convert_options: { masked_with_overlay: Proc.new{self.apply_mask_and_overlay},
masked_with_nearby_overlay: Proc.new{self.apply_mask_and_nearby_overlay},
masked_with_rewards_overlay: Proc.new{self.apply_mask_and_rewards_overlay},
masked_with_overlay_for_old: Proc.new{self.apply_mask_and_overlay_for_old},
masked_with_rewards_overlay_for_old: Proc.new{self.apply_mask_and_rewards_overlay_for_old},
masked_full_card_image: Proc.new{self.apply_mask_full_card_image},
masked_logo_only: Proc.new{self.apply_mask_logo_only}
},
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
path: "/store_logos/:style/:id/:filename"
validates_attachment :logo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/JPG", "image/JPEG", "image/PNG"] }
Here the Gemfile
ruby '2.0.0'
gem 'rails', '4.0.3'
gem 'aws-s3'
gem 'aws-sdk', '~> 1.5.7'
gem 'paperclip', "~> 4.2"
gem 'rmagick', '2.13.2'
stores_controller.rb
class StoresController < ApplicationController
def update
if !params[:store].nil? && !params[:store][:store_owner].nil?
params[:store][:store_owner][:email] = params[:store][:store_owner][:email].downcase
end
if params[:demo_store_changes]
params[:store][:active] = false
else
params[:store][:store_hours] = params[:store][:store_hours].to_json
params[:store][:minimum_seconds_per_scan] = (params[:store][:minimum_seconds_per_scan]).to_i * 3600
end
#store.store_owner.store_owner!
if params[:store][:scanning_distance].blank?
params[:store].delete :ms
else
if params[:store][:location_check]
if params[:store][:ms] == "mi"
params[:store][:scanning_distance] = ( params[:store][:scanning_distance].to_f * 5280 )
params[:store].delete :ms
else
params[:store][:scanning_distance] = params[:store][:scanning_distance].to_f
params[:store].delete :ms
end
end
end
if #store.update_attributes(store_params) and #store.build_categories(params[:category_ids].to_a)
if params[:demo_store_changes]
demo_message = #store.demo ? " is now demo store" : " is not a demo store anymore"
flash[:notice] = #store.name + demo_message
redirect_to stores_path('store[search]'=> params[:store_search], search_value: params[:search_value])
else
#password_chk=params[:store][:store_owner_attributes][:password]
redirect_to "#{store_path()}?myid=#{session[:bread_crum]}", notice: (I18n.t :update_store_success)
end
else
#store.store_hours = ActiveSupport::JSON.decode(#store.store_hours)
if #store.minimum_seconds_per_scan?
#store.minimum_seconds_per_scan = (#store.minimum_seconds_per_scan / 3600).to_i
end
#categories = Category.all.sort
#states = StoreConstants::STATES.sort
#main_location_stores = Store.with_no_branch(#store.id)
flash.now[:error] = (I18n.t :update_store_failed)
render action: "edit"
end
private
def store_params
params.require(:store).permit!
end
end
Here is log
Any help?, Thanks in advance
Look into the log
(:bucket_name=>"punchme-co-dev",:key=>"store_logos/masked_with_rewards_overlay/96/Casablanca.png") AWS::S3::Errors::NoSuchKey No Such Key)
AWS access key is taking your image file path.
I finally found the solution for this.
convert: no decode delegate for this image format `/home/likewise-open/ANNETSITE/1820/RailsWorkspace/Punchme_web_Test' # error/constitute.c/ReadImage/532.
convert: unable to open image `Builds/app/assets/images/card_overLay.png': # error/blob.c/OpenBlob/2587.
By renaming project folder Project_web_Test Builds to Project_web_Test_Builds, When you process image space is not allowed.
Related
I have already gone through all the answers to make the upload of my video to S3 work but they do not work. I keep on getting the error uninitialized constant Paperclip::Storage::S3::Aws.
I hope you could help me out on this one.
Gemfile
gem 'simple_form', '~> 3.4'
gem 'haml', '~> 4.0', '>= 4.0.7'
gem 'coffee-script-source', '1.8.0'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'paperclip'
gem 'carrierwave'
gem "paperclip-ffmpeg", "~> 1.0.1"
gem 'paperclip-av-transcoder'
gem 'aws-sdk', '~> 1.6'
gem "figaro"
config\s3.yml
AWS_ACCESS_KEY_ID: xxx
AWS_SECRET_ACCESS_KEY: xxx
S3_BUCKET_NAME: xxx
models\video.rb
class Video < ApplicationRecord
has_attached_file :clip, styles: {
medium: {
:geometry => "640x480",
:format => 'mp4'
},
thumb: { :geometry => "160x120",
:format => 'jpeg',
:time => 10}
},
processors: [:transcoder],
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml"
validates_attachment :clip,
content_type: { content_type: ['application/x-shockwave-flash', 'application/x-shockwave-flash', 'application/flv', 'video/x-flv']}
before_post_process :skip_for_audio
def skip_for_audio
! %w(audio/ogg application/ogg).include?(asset_content_type)
end
before_post_process :image?
def image?
!(data_content_type =~ /^image.*/).nil?
end
end
Videos_controller.rb
class VideosController < ApplicationController
def index
#videos = Video.all
#video = Video.order('created_at')
end
def new
#video = Video.new
end
def create
#video = Video.new(videos_params)
if #video.save
flash[:success] = "The step was added!"
redirect_to root_path
else
render 'New'
end
end
def destroy
#video = Video.find(params[:id])
#video.destroy
flash[:success] = "The step was destroyed."
redirect_to root_path
end
private
def videos_params
params.require(:video).permit(:title, :description, :clip)
end
end
initilizers\paperclip.rb
paperclip_defaults = Rails.application.config_for :paperclip
paperclip_defaults.symbolize_keys!
Paperclip::Attachment.default_options.merge! paperclip_defaults
initilizers\aws.rb
AWS.config(
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)
S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]
Have your tried this configuration?
# config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
.
# config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
.
I have a custom processor for my Paperclip styles: cropper.rb. Though it is not called and return NameError (uninitialized constant Paperclip::Cropper) error.
It has been discussed here : Rails3 and Paperclip but a while ago. It was concerning Rails 3 back then.
I am under Rails 5 (update from Rails 4.x)
Profilepic.rb
class Profilepic < ApplicationRecord
belongs_to :professionnels
has_attached_file :image, styles: { big: "1200x1200", medium: "400x400", small: "250x250"}
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: {less_than: 10.megabytes}
has_attached_file :finalimage, styles: { medium: "500x500", small: "200x200"}, whiny: false, use_timestamp: false, processors: [:cropper]
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
end
lib/paperclip_processors/cropper.rb
module Paperclip
class CustomCropper < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
if target.crop_w && target.crop_x
#current_geometry.width = target.crop_w
#current_geometry.height = target.crop_h
end
end
def target
#attachment.instance
end
def transformation_command
# call crop processing only if user inputs are there
if target.crop_w && target.crop_x
crop_command = [
"-crop",
"#{target.crop_w}x" \
"#{target.crop_h}+" \
"#{target.crop_x}+" \
"#{target.crop_y}",
"+repage"
]
crop_command + super
else
super
end
end
end
end
OK spent a day to realize the correct lib subfolder is actually paperclip and not paperclip_processors although Paperclip Git does mention both as valid and automatically loaded.
I try apply watermark to my paperclip, it keep show error message and unable update/upload image. It keep show **
uninitialized constant Paperclip::Watermark::PaperclipCommandLineError
**
gem file
gem "paperclip", '4.2' gem 'rails', '4.2.6' gem
'paperclip-compression'
paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
#watermark_path = options[:watermark_path]
#position = options[:position].nil? ? "SouthEast" : options[:position]
end
def make
src = #file
dst = Tempfile.new([#basename].compact.join("."))
dst.binmode
return super unless #watermark_path
params = "-gravity #{#position} #{transformation_command.join(" ")} #{#watermark_path} :source :dest"
begin
success = Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{#basename}" if #whiny
end
dst
end
end
end
lisitng.rb
class Listing < ActiveRecord::Base
require 'paperclip_processors/watermark'
has_attached_file :image,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/watermark.png"
},
:thumb => "100x100>",
}
I think you're subclassing wrong. Don't subclass Thumbnail, subclass Paperclip::Processor
module Paperclip
class Watermark < Processor
...
https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb
I'm using Ruby on Rails and the paper-clip Gem and I would like to to convert a .gif image to .jpeg if the .gif image is not a animated gif.
This is my code:
has_attached_file :image, styles: Proc.new { |file| file.instance.check_image_gif? ? {
:'960' => ["960>x960", :gif],
:'640' => ["640>x640", :gif],
:'320' => ["320>x320", :gif]
}:{
:'960' => ["960>x960", :jpg],
:'640' => ["640>x640", :jpg],
:'320' => ["320>x320", :jpg]
}
}
def check_image_gif?
# I want to check animation gif here.
image.instance.image_content_type =~ %r(gif) ? true : false
end
I resolved by myself.
def check_image_gif?
begin
file = image.instance.image.queued_for_write[:original]
img = Magick::Image.read(file.path)
animation = (img.size>1)
unless animation
image.instance.image_content_type = "image/jpeg"
end
rescue => e
end
image.instance.image_content_type =~ %r(gif) ? true : false
end
I added rmagick Gem.
I'm inserting a digital signature into a pdf file inside my processor, but keep getting an AWS::S3::ERRORS::Requestimeout error. What is this timeout? Is there any way i could keep the connection open until the file gets uploaded ?
Your socket connection to the server was not read from or written to
within the timeout period. Idle connections will be closed.
Here is my code :
model:
...
has_attached_file :receipt_file,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:s3_protocol => "https",
:styles => {dummy:""},
processors: [:SignPdf]
#process_in_background :receipt_file
...
Processor
module Paperclip
class SignPdf < Processor
attr_accessor :receipt_id,:style
S3_CONFIG = YAML.load_file("#{::Rails.root}/config/s3.yml")[Rails.env]
ORIGAMIDIR = "/ruby/1.9.1/gems/origami-1.2.4/lib"
def initialize(file, options = {}, attachment = nil)
#file = file
#current_format = File.extname(#file.path)
#basename = File.basename(#file.path, #current_format)
#attachment = attachment
end
def make
signPdf(#file)
#file
end
end
end
begin
require 'origami'
rescue LoadError
$: << ORIGAMIDIR
require 'origami'
end
include Origami
def signPdf(file)
certFile = "#{::Rails.root}/lib/assets/Cert.pem"
rsakeyFile = "#{::Rails.root}/lib/assets/pk.pem"
passphrase = "o2Receipts"
key4pem=File.read rsakeyFile
key = OpenSSL::PKey::RSA.new key4pem, passphrase
cert = OpenSSL::X509::Certificate.new(File.read certFile)
pdf = PDF.read(file)
page = pdf.get_page(1)
# Add signature annotation (so it becomes visibles in pdf document)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Portugal",
:contact => "email#email.pt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(file.path)
file
end
I have worked around this, by using the after save. See my answer related to this subject here
What you're looking for isn't in the documentation as of today. You'll need to create an AWS::S3::Client
I'm referencing: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb#L263
config.paperclip_defaults = {
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
s3_region: ENV['AWS_REGION'],
s3_protocol: :https,
s3_options: {
client: Aws::S3::Client.new(
access_key_id: ENV['S3_KEY'],
secret_access_key: ENV['S3_SECRET'],
http_open_timeout: 10,
http_read_timeout: 5,
http_idle_timeout: 20
)
}
}