Paperclip 5.1.0 - Rails 5 - appears missing, after the image upload - imagemagick

if I try to upload a picture message appears:
"New Content
1 error prohibited this content from being saved:
That cover has contents are not what they are reported to be "
and then shows "missing"
in Git Bash, appears:
"[Paperclip] Content Type Spoof: Filename goku.jpg (image / jpeg from Headers, [" image / jpeg "] from Extension), content type discovered from file command:. See documentation to allow this combination.
   (0.0ms) begin transaction
Command :: -b --mime file "C: /Users/xxxxxx/AppData/Local/Temp/1151fe66500e3084eef890162322a11020170115-9100-1xumnlr.jpg"
[Paperclip] Content Type Spoof: Filename juegos-vestir-goku.jpg (image / jpeg from Headers, [ "image / jpeg"] from Extension), content type discovered from file command:. See documentation to allow this combination.
   (0.0ms) rollback transaction
  Rendering contents / new.html.erb Within layouts / application
  Rendered contents / _form.html.erb (4.0ms)
  Rendered contents / new.html.erb Within layouts / application (30.0ms)
  Rendered layouts / _header2.html.erb (2.0ms)
  Rendered layouts / _footer.html.erb (0.0ms)
Completed 200 OK in 627ms (Views: 511.2ms | ActiveRecord: 0.0ms) "
Also when I installed paperclip appeared the message:
"Post-install message from paperclip:
##################################################
# NOTES FOR UPGRADING FROM 4.3.0 OR EARLIER #
##################################################
Paperclip is now compatible with aws-sdk> = 2.0.0.
If you are using S3 storage, aws-sdk> = 2.0.0 requires you to make a few small
changes:
* You must September the `s3_region`
* If you are Explicitly setting permissions anywhere, such as in an initializer,
  notes That the format of the permissions changed from using an underscore to
  using a hyphen. For example, `: public_read` needs to be changed to
  `Public-read`.
For a walkthrough of upgrading from 4 to 5 and aws-sdk> = 2.0 you can watch
http://rubythursday.com/episodes/ruby-snack-27-upgrade-paperclip-and-aws-sdk-in-prep-for-rails-5 "
perform operations to install paperclip:
- Downloaded magemagick for windows
- Insert in Gemfile gem "paperclip", "~> 5.0.0"
- Bundle install
- In the app >> model >> content.rb I entered:
has_attached_file: cover, styles {medium: "300x>" thumb "100x>"}
validates_attachment_content_type: cover, content_type: /\Aimage\/.*\z/
- Rails generate paperclip content cover
- Rake db: migrate
- I added to my code in the app> views> contents> _form,:
 <%= form_for #content, html: { multipart: true } do |f| %>
<Div class = "field">
    <% = F.label: cover%>
    <% = F.file_field: cover%>
  </ Div>
- App> controllers> content, I entered:
def content_params
      params.require (: content) .permit (: title: description: price,: cover)
end
- App> views> content> show I entered:
<% = # Image_tag content.cover.url (: thumb)%>
- App> views> content> index> I entered:
<Td> <% = image_tag content.cover.url (: medium)%> </ td>
Versions used:
-Win10 x64
-ruby 2.3.3p222
-Rails 5.0.1
-paperclip 5.1.0
Tests carried out, but not decisive:
Added in app> model> content.rb:
:default_url => "/images/:style/missing.png"
Added in config / environments / development.rb:
Paperclip.options [: command_path] = "/ usr / local / bin /"
Paperclip.options [: command_path] = 'C: \ Program Files (x86) \ GnuWin32 \ bin
Added in - Added in app> model> content.rb:
Paperclip.options [: command_path] = 'C: \ Program Files (x86) \ GnuWin32 \ bin; C: \ Program Files \ ImageMagick-7.0.4-Q16 '
Paperclip.options [: swallow_stderr] = false

Related

Rails 5, paperclip not validating the attachment?

I am getting this response:
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-b6btes.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.0ms) BEGIN
Command :: file -b --mime "C:/Users/Ben/AppData/Local/Temp/5523c88dd347d1b7cc617f632b7efdb720171021-12076-1fvpv4a.jpg"
[paperclip] Content Type Spoof: Filename bg.jpg (image/jpeg from Headers, ["image/jpeg"] from Extension), content type discovered from file command: . See documentation to allow this combination.
(0.6ms) ROLLBACK
On Windows
And I have this in my model:
has_attached_file :image
# Validate content type
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
# Explicitly do not validate
do_not_validate_attachment_file_type :image
And the request just fails, I dont understand why is it happening?
Try:
config/initializers/paperclip.rb:
Paperclip.options[:command_path] = '/usr/bin/' # Path to ImageMagick

How to download each zip file from a url and unpack using rails

Right now I have a URL which is populated with a list of .zip files in the browser. I am trying to use rails to download the files and then open them using Zip::File from the rubyzip gem. Currently I am doing this using the typhoeus gem:
response = Typhoeus.get("http://url_with_zip_files.com")
But the response.response_body is an HTML doc inside a string. I am new to programming so a hint in the right direction using best practices would help a lot.
response.response_body => "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<html>\n <head>\n <title>Index of /mainstream/posts</title>\n </head>\n <body>\n<h1>Index of /mainstream/posts</h1>\n<table><tr><th>Name</th><th>Last modified</th><th>Size</th><th>Description</th></tr><tr><th colspan=\"4\"><hr></th></tr>\n<tr><td>Parent Directory</td><td> </td><td align=\"right\"> - </td><td> </td></tr>\n<tr><td>1476536091739.zip</td><td align=\"right\">15-Oct-2016 16:01 </td><td align=\"right\"> 10M</td><td> </td></tr>\n<tr><td>1476536487496.zip</td><td align=\"right\">15-Oct-2016 16:04 </td><td align=\"right\"> 10M</td><td> </td></tr>"
To break this down you need to:
Get the initial HTML index page with Typhoeus
base_url = "http://url_with_zip_files.com/"
response = Typhoeus.get(base_url)
Then Use Nokogiri to parse that HTML to extract all the links to the zip files (see: extract links (URLs), with nokogiri in ruby, from a href html tags?)
doc = Nokogiri::HTML(response)
links = doc.css('a').map { |link| link['href'] }
links.map { |link| base_url + '/' + link}
# Should look like:
# links = ["http://url_with_zip_files.com/1476536091739.zip", "http://url_with_zip_files.com/1476536487496.zip" ...]
# The first link is a link to Parent Directory which you should probably drop
# looks like: "/5Rh5AMTrc4Pv/mainstream/"
links.pop
Once you have all the links: you then visit all the extracted links to download the zip files with ruby and unzip them (see: Ruby: Download zip file and extract)
links.each do |link|
download_and_parse(link)
end
def download_and_parse(zip_file_link)
input = Typhoeus.get(zip_file_link).body
Zip::InputStream.open(StringIO.new(input)) do |io|
while entry = io.get_next_entry
puts entry.name
parse_zip_content io.read
end
end
end
If you want to use Typhoeus to stream the file contents from the url to memory see the Typhoeus documentation section titled: "Streaming the response body". You can also use Typhoeus to download all of the files in paralell which would increase your performance.
I believe Nokogiri will be your best bet.
base_url = "http://url_with_zip_files.com/"
doc = Nokogiri::HTML(Typhoeus.get(base_url))
zip_array = []
doc.search('a').each do |link|
if link.attr("href").match /.+\.zip/i
zip_array << Typhoeus.get(base_url + link.attr("href"))
end
end

Paperclip: The following errors were found: Image has contents that are not what they are reported to be

I am using paperclip with a rails app and always I get the following error:
I, [2015-06-06T20:10:25.310071 #37358] INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-ouvtzl.png'
I, [2015-06-06T20:10:25.317478 #37358] INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 #friendly={"en"=>"Portable Network Graphics (PNG)"}, #system=nil, #obsolete=false, #registered=true, #use_instead=nil, #signature=false, #content_type="image/png", #raw_media_type="image", #raw_sub_type="png", #simplified="image/png", #i18n_key="image.png", #media_type="image", #sub_type="png", #docs=[], #encoding="base64", #extensions=["png"], #references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], #xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.
I, [2015-06-06T20:10:25.349416 #37358] INFO -- : Command :: file -b --mime '/tmp/58e53d1324eef6265fdb97b08ed9aadf20150606-37358-1n574dp.png'
I, [2015-06-06T20:10:25.356667 #37358] INFO -- : [paperclip] Content Type Spoof: Filename ruby.png (application/octet-stream from Headers, [#<MIME::Type:0x000000053624c8 #friendly={"en"=>"Portable Network Graphics (PNG)"}, #system=nil, #obsolete=false, #registered=true, #use_instead=nil, #signature=false, #content_type="image/png", #raw_media_type="image", #raw_sub_type="png", #simplified="image/png", #i18n_key="image.png", #media_type="image", #sub_type="png", #docs=[], #encoding="base64", #extensions=["png"], #references=["IANA", "[Glenn_Randers-Pehrson]", "{image/png=http://www.iana.org/assignments/media-types/image/png}"], #xrefs={"person"=>["Glenn_Randers-Pehrson"], "template"=>["image/png"]}>] from Extension), content type discovered from file command: image/png. See documentation to allow this combination.
Mongoid::Errors::Validations (
Problem:
Validation of Mock failed.
Summary:
The following errors were found: Image has contents that are not what they are reported to be
Resolution:
Try persisting the document with valid data or remove the validations.):
app/api/mockaccino/api.rb:23:in `block (2 levels) in <class:API>'
Curl command for test:
curl -X POST -i -F image=#/home/user/workspace/ruby.png -F name=mock http://localhost:3000/api/mock
I am traying send an image and a parameter called "name" to the server and creating a model with this attribute and image.
Model:
class Mock
include Mongoid::Document
include Mongoid::Paperclip
field :name, type: String
has_mongoid_attached_file :image
validates_attachment_presence :image
validates_attachment_size :image, :less_than => 1.megabytes
validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
end
Controller (Grape):
desc "Create an mock."
params do
requires :name, type: String, desc: "Mock name"
requires :image, :type => Rack::Multipart::UploadedFile, :desc => "Image file."
end
post do
mock = Mock.create!(
name: params[:name],
image: ActionDispatch::Http::UploadedFile.new(params[:image])
)
end
curl -i -X POST -H "Content-Type:multipart/form-data" -F image=#\"./ruby.png\";type=image/png;filename=\"ruby.png\"" http://localhost:3000/api/mock
I have one user who is seeing it with various browsers.. I can't duplicate it on my local development server or on production. It works for me, doesn't work for them. Haven't been able to isolate anything that is different (tried PC vs Mac, various browsers, etc.)
Pushing paperclip back from 4.3 to 4.2.1 makes the issue go away. This switches activemodel and activesupport from 3.2 back to 3.0 and it eliminates the dependency on mimemagic. My guess is that it's something subtle and complex in mimemagic.

Rails 3 carrierwave-azure Azure::Core::Http::HTTPError OutOfRangeInput (400): One of the request inputs is out of range

I'm trying to use the carrierwave and carrierwave-azure gems to save uploaded files in my app to the azure storage blob. The documentation for the carrierwave-azure gem seems a bit lite on github, but I believe have followed all the setup directions correctly (https://github.com/unosk/carrierwave-azure). However it still doesn't work.
When I attempt to upload a file I get the following error:
Azure::Core::Http::HTTPError in DownstreamsController#create
OutOfRangeInput (400): One of the request inputs is out of range.
My carrierwave.rb looks like this:
CarrierWave.configure do |config|
config.azure_credentials = {
storage_account_name: 'myaccountname',
storage_access_key: 'reallylongstringwithcapandlowercaselettersand+signs=='
}
config.azure_container = 'http://myapp.blob.core.windows.net/shipmentdocs'
end
My uploader:
include CarrierWave::RMagick
storage :azure
def store_dir
"http://myapp.blob.core.windows.net/shipmentdocs/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :convert => 'jpg'
process :resize_to_limit => [100, 100]
end
def extension_white_list
%w(jpg jpeg gif png pdf doc)
end
I've included both gems in my gem file. If I switch the storage from :azure to :file the upload works however it stores in the app directory, I want to store the file as a blob in Azure.
Not sure that it matters, but I am trying to do this from my local dev environment via localhost. Any help would be greatly appreciated.
UPDATE:
Here are the request parameters being submitted:
> {"utf8"=>"✓",
> "authenticity_token"=>"X/Q1ADOLgdaLJVsdrerdHdK9S/kJtSfjkjiutEuYsTYRY=",
> "downstream"=>{"bols_attributes"=>{"0"=>{"file_name"=>#<ActionDispatch::Http::UploadedFile:0x00000107c58340
> #original_filename="test-azure-storage-bol-file.png",
> #content_type="image/png", #headers="Content-Disposition: form-data;
> name=\"downstream[bols_attributes][0][file_name]\";
> filename=\"test-azure-storage-bol-file.png\"\r\nContent-Type:
> image/png\r\n",
> #tempfile=#<Tempfile:/var/folders/dc/y286vygx1jq5wjw3f6b6bcww0000gn/T/RackMultipart20140514-8268-ngdw0m>>}},
> "sales_order"=>"RTEWW423", "to_company_id"=>"2",
> "ship_date"=>"2014-05-14", "ship_total_pallets"=>"1",
> "downstreamDetails_attributes"=>{"0"=>{"tb_product_type_id"=>"17",
> "ship_total_net_weight"=>"3000", "ship_total_gross_weight"=>"3000",
> "_destroy"=>"false"}}}, "commit"=>"Submit"}
The answer turned out to be that in my carrierwave.rb file I was using the full URL for my config.azure_container setting (ie. 'http://myapp.blob.core.windows.net/shipmentdocs'). It turns out you should provide the container name as in:
config.azure_container = 'shipmentdocs'
It's also helpful to note that making this change requires a restart of the server for it to take effect because the carrierwave.rb file should be in your config/initializers folder.

Ruby on Rails Edit File Template?

I have a HTML/CSS File laid out with a bunch of areas that need adding. I have a ruby on rails application that would have a bunch of form elements that would then need to be added to this HTML/CSS file (it doesn't need to be shown or anything, just edited and then saved). I don't really understand how I can do this. I was looking at the file class, but got lost very quickly.
Any easy way to do this?
Writing to a file in Ruby is very simple:
File.open(filename, 'w') do |f|
f.write(content)
end
For an example in Rails try the following steps. Generate a new Rails app and a dummy scaffold by running:
rails new erbfun
cd erbfun
rails g scaffold Stylesheet custom_css:text
rake db:migrate
mkdir -p public/system/stylesheets
Then do something like this in your model:
class Stylesheet < ActiveRecord::Base
require 'erb'
FOLDER = File.join(Rails.public_path,'system/stylesheets')
TEMPLATE = <<-CSS
body {
font-family: Helvetica;
}
<%= custom_css %>
/* some css comment here ... */
CSS
def save_to_file
template = ERB.new(TEMPLATE)
document = template.result(binding)
filename = File.join(FOLDER,"stylesheet-#{Time.now.to_i}.css")
File.open(filename, 'w') do |f|
f.write(document)
end
end
end
and then try it out:
$ rails c
Loading development environment (Rails 3.2.2)
1.9.3p125 :001 > s = Stylesheet.new custom_css: 'foobar'
=> #<Stylesheet id: nil, custom_css: "foobar", created_at: nil, updated_at: nil>
1.9.3p125 :002 > s.save!
...
=> true
1.9.3p125 :003 > s.save_to_file
=> 94
1.9.3p125 :004 > exit
$ cat public/system/stylesheets/stylesheet-1332633386.css
body {
font-family: Helvetica;
}
foobar
/* some css comment here ... */

Resources