Carrierwave upload and extract zip before save rails4 - ruby-on-rails

I want to upload a zip file contains documents and after uploading it to server I want to extract those on server.
I have used Carrierwave Gem to upload zip file code is below for that :
class AttachmentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(zip)
#%w(pdf doc htm html docx)
end
end
No what I want to know that which Gem can help me to achieve the extraction part and where that code will be placed in this uploader class or anywhere else.. I'm very new to this rails stuff so if this question require anything more pls do let me know.

Related

Minimagick gem fails to convert multipage PDF to several png images

I am new to RoR and I'm trying to develop a slide sharing app through which users can upload and share a multipage PDFs containing a deck of slides (one slide per page). Viewers can then view these slides on a "show" page.
Behind the scenes I am trying to convert the multipage pdf into several .png files (1 pdf page -> 1.png file), and then display them in a carousel/slider fashion.
Here is my uploader:
# encoding: utf-8
class DocUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# 'public/doc_uploads/'
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :filename
process :generate_png
def generate_png
pdf = MiniMagick::Image.new(self.file.path)
pdf.pages.each_with_index do |page, index|
page.write("page#{index}.png")
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(pdf)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
'doc.pdf' if original_filename
end
end
When I try to upload a multipage pdf document, it throws me this error:
'identify /public/uploads/tmp/1443619784-406-3861/doc.pdf' failed with error: at the line pdf.pages.each_with_index do |page, index|
I have tried different solutions to converting the document, but none of them have worked for me so far. Why would a run of the mill .pdf file fail minimagick's validation? Is there anything I can do to avoid running into this issue? Any advice will be greatly appreciated!

How to customize version filename in Carrierwave?

I have a strange issue. I am trying to use master branch of carrierwave with Rails 3.2.xx project. I need to customize filenames of the versions. But when I add full_filename method in the version block, my original file also gets reduced to the dimensions specified for version.
When I remove full_filename method, it all works as expected, but thumb filename has thumb_ prefix which I don't want.
Is there a new way to customize version filenames. I have been using this way successfully in 0.10.0 and before.
Below is my uploader. This is a generated uploader with store_dir overrides.
class TestUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [200, 200]
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}/#{version_name}"
end
def full_filename(for_file = model.logo.file)
super(for_file).sub(version_name.to_s + '_', '')
end
end
end
Any ideas? All I need to do is to remove version_name part from its filename, since I am saving the versions in separate folders. I searched through Wiki and internet, but couldn't find a new way of doing this.
Add an initializer carrierwave.rb and mokeypatch the gem with:
module CarrierWave
module Uploader
module Versions
private
# Use original file name instead of prepending version name
# Can't overload in app's uploader class because `super` already has
# the version name
def full_filename(for_file)
directory = version_name.to_s
# if you want the original version in a sub-directory also
# directory = (version_name || 'original').to_s
File.join(directory, super(for_file))
end
end
end
end
Also, leave off the store_dir and full_filename methods from the versions.

cant see the aws-s3 url saved in rails database

I have successfully implemented image uploading using carrierwave, fog and Amazon S3. In my imageuploader am using only fog as storage. But when i check my database i can see that just the file name is written instead of the amazon url. In my views its fetching correctly from aws without any issues.
Is it supposed to be like this?
If so how the application figure out the exact url to s3?
imageuploader.rb`
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::MiniMagick
#Include the sprockets-rails helper for Rails 4+ compatibility:
include Sprockets::Rails::Helper
storage :fog
version :index_size do
process :resize_to_fill => [258, 173]
end
version :thumb_size do
process :resize_to_fill => [100, 100]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Your config has the bucket name and the database has the filename. These are the only two pieces of information that are actually required to construct a filename (and it can be done without other API calls). The urls are actually pretty regular, so it is fairly straightforward for the server to do this. Hope that helps!

How can I maintain images when switching from Rackspace to S3

I'm having performance issues with carrierwave uploads on images using Rackspace. There have been no other reasons or bugs that would identify what the issue is, and a quick test deploy using S3 solves the issues completely.
Is there any way to maintain the rackspace links and images when making the change to s3? When we change them on a staging server all url references are removed and replaced with the new url, making all images go 404.
class PostimageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# These settings are for image updloads to rackspace
if Rails.env.production?
storage :fog
else
storage :file
end
# Do not edit this file structure
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Thanks for any help.

How can I use Carrierwave with cloudinary in seeds file

I want to upload multiple images to cloudinary by association of a carrierwave active record. How can I do this in a seeds file, using an array of remote urls?
Ive read countless articles how to use carrierwave/cloudinary helper tags to target an image upload within an html form, but nothing on doing this directly within code, any ideas?
That's not so hard to do at all.
So what I did:
# Gemfile
gem 'cloudinary'
gem 'carrierwave'
#config/cloudinary.yml
development:
cloud_name: "carrierwave-example"
api_key: "YOUR CLOUDINARY CREDENTIALS"
api_secret: "YOUR CLOUDINARY CREDENTIALS"
# in your uploader
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave #include cloudinary lib
# storage :file - comment this out
# def store_dir - comment this too
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end
end
# You model, that uses uploader
class Picture < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
After writting this simple staff you can create Pictures, that will store images in clodinary like this:
Picture.create(image: "/path/to/image")
or if you have remote links of images, U just itterate through them
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end
Just remember to use remote_#{your_column_name}_url if you have remote link

Resources