paperclip_utils 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edb0136a20bafc183e70c8be005f63d73e6ce48b
4
- data.tar.gz: da33fd10aaeda193e7cdd14e73bd288b385ca8e0
3
+ metadata.gz: 3b4320db1228b252e0ec23254e663d9814219859
4
+ data.tar.gz: be9dc86758d73abff8b65e1f0551a0b87d3e7d0e
5
5
  SHA512:
6
- metadata.gz: e02327b9b03ed0818f04aca9142d4d8009384e854674a7ea093538941a55f8b626f993ba309458b30b297124344f6a2a08419360a69e0123c6f44bf5152e36de
7
- data.tar.gz: d0d39cbecf6bd53b80a58e5c4cb67e039fa2e29ce984670ae919418611e8db823f89b8308d75390e1aa98bcf8cb9651544c68fef3dab83fbf79282e07e068c94
6
+ metadata.gz: cb2afa7f6bcbb7170a5bd8643fcb1e265325acef96b0556010dff0e881e436a678b3db3a9882067457b66c76ef1fc6ce944d898c7b998a6ac77b5d5a475683f2
7
+ data.tar.gz: 85456b448b69026c6ab90ac67f0f7b8d77f6eb509cffcc00c8104164a7013dc05582b8565ed6a4fe797d2d97aaf9e220c7c5f9ff9ac5c3c05584d57bfdb282ff
data/README.md CHANGED
@@ -1,24 +1,26 @@
1
1
  # Paperclip Utils
2
2
  <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=VKY8YAWAS5XRQ&lc=CA&item_name=Weston%20Ganger&item_number=paperclip_utils&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest" target="_blank" title="Donate"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif" alt="Donate"/></a>
3
3
 
4
- Paperclip Utils is a helper class for easier dynamic processors and styles on your Paperclip file uploads
4
+ Paperclip Utils is a helper class for easier dynamic processors and styles on your Paperclip file uploads. It also has a collection of custom Paperclip processors.
5
5
 
6
6
 
7
7
  # Install
8
8
  ```ruby
9
+ # requires ruby >= 2.0
9
10
  gem install paperclip_utils
10
11
  ```
11
12
 
13
+ # Custom Processors
14
+ **Ghostscript** - `:ghostscript` - Fixes black boxes and errors thumbnail processing for PDF files. This is automatically included if processors includes :thumbnail which it does by default
12
15
 
13
- Paperclip Utils contains the following methods:
14
- ```ruby
15
- Paperclip::Utils.get_processors(content_type, processors_if_allowed, fallback_processors, allowed_content_types)
16
+ **XLS to CSV** - `:xls_to_csv` - Converts XLS/XLSX files to CSV
16
17
 
17
- Paperclip::Utils.get_styles(content_type, styles_if_allowed, fallback_processors, allowed_content_types)
18
- ```
18
+ **PDF Merge** - `:pdf_merge` - A way to handle multiple pdf uploads, requires some custom wiring, must add a `pdf_files` method to your model and temporarily save files with this though.
19
+
20
+ Check the processor source to see examples if required. [See all processors & examples here](https://github.com/westonganger/paperclip_utils/tree/master/lib/paperclip_processors).
19
21
 
20
22
 
21
- # Usage
23
+ # Helper Methods Usage
22
24
  ```ruby
23
25
  class Post < ActiveRecord::Base
24
26
  has_attachment :my_attachment,
@@ -27,18 +29,35 @@ class Post < ActiveRecord::Base
27
29
  path: "public/system/:class/:attachment/:id_partition/:style/:filename",
28
30
  url: "#{ActionController::Base.relative_url_root}/system/:class/:attachment/:id_partition/:style/:filename"
29
31
  end
32
+
33
+ # OR using any or all of the custom options
34
+
35
+ class Post < ActiveRecord::Base
36
+ has_attachment :my_attachment,
37
+ styles: lambda{|x| Paperclip::Utils.get_styles(x.instance.my_attachment.content_type, styles: {preview: "800x600>", thumb: "100x100>"}, fallback_styles: nil, allowed_content_types: ['application/pdf']) },
38
+ processors: lambda{|x| Paperclip::Utils.get_processors(x.my_attachment.content_type, processors: [:thumbnail, :some_other_custom_processor], fallback_processors: [:another_custom_processor], allowed_content_types: ['application/pdf']) },
39
+ path: "public/system/:class/:attachment/:id_partition/:style/:filename",
40
+ url: "#{ActionController::Base.relative_url_root}/system/:class/:attachment/:id_partition/:style/:filename"
41
+ end
30
42
  ```
31
43
 
44
+ # Helper Methods & Options
32
45
 
46
+ <br>
47
+ #### `get_styles(content_type, *optional_options)`
48
+ **styles** - Array - Default: `[:thumbnail]`
33
49
 
34
- # Methods
50
+ **fallback_styles** - Hash - Default -`{}`
35
51
 
36
- **get_processors** - `String(file content_type), Array(optional - default=[:ghostscript,:thumbnail]), Array(optional - default=[]), Array(optional - allowed content types)`
52
+ **allowed_content_types** - Array - Default: `['application/pdf', 'image/png', 'image/x-png', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/tif, ''image/tiff', 'image/x-tiff']`
37
53
 
38
- **get_styles** - `String(file content type)Hash(optional - default={preview: "800x600>", thumb: "100x100>"}), Hash(optional - default={}), Array(optional - allowed content types)`
54
+ <br>
55
+ #### `get_processors(content_type, *optional_options)`
56
+ **processors** - Array - Default: `[:ghostscript, :thumbnail]` - Notes: Automatically includes ghostscript processor if processors includes :thumbnail which it does by default
39
57
 
40
- **ALLOWED_CONTENT_TYPES** - Default allowed content types. `['application/pdf', 'image/png', 'image/x-png', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/tif, ''image/tiff', 'image/x-tiff']`
58
+ **fallback_processors** - Array - Default: `[]`
41
59
 
60
+ **allowed_content_types** - Array - Default: `['application/pdf', 'image/png', 'image/x-png', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/tif, ''image/tiff', 'image/x-tiff']`
42
61
 
43
62
 
44
63
  # Credits
@@ -0,0 +1,69 @@
1
+ # Merge pdf files (for transmittals)
2
+ #https://gist.github.com/jessieay/5832466
3
+ # Ex. http://stackoverflow.com/questions/17203270/combine-multi-page-pdfs-into-one-pdf-with-imagemagick
4
+ #
5
+ # First save your files to a temporary location then save your model with any arbitrary file attached which will not be saved.
6
+ #
7
+ # Example Model:
8
+ # class SomeCsvAttachement < ActiveRecord::Base
9
+ # has_attached_file :data,
10
+ # path: ":rails_root/data/some_csv_attachements/:id_partition/:basename.:extension",
11
+ # processors: [:pdf_merge]
12
+ #
13
+ # def pdf_files
14
+ # base_path = File.join(Rails.root, 'tmp')
15
+ #
16
+ # # Specific files
17
+ # [ File.join(base_path, 'path/to/file.pdf'), File.join(base_path, 'another/file/path.pdf') ]
18
+ #
19
+ # # Or all files in a folder
20
+ # Dir.glob(File.join(base_path,"/path/to/folder/*"))
21
+ # end
22
+ #
23
+ # validates_attachment_content_type :data, content_type: ['application/pdf']
24
+ # end
25
+
26
+ module Paperclip
27
+ class PdfMerge < Processor
28
+
29
+ def initialize file, options = {}, attachment = nil
30
+ super
31
+ @format = File.extname(@file.path)
32
+ @basename = File.basename(@file.path, @format)
33
+ @files = attachment.instance.pdf_files
34
+ end
35
+
36
+ def make
37
+ src = @file
38
+ if @current_format && @current_format.downcase == '.pdf'
39
+ dst = Tempfile.new([@basename, @format])
40
+ dst.binmode
41
+
42
+ file_paths = @files.map {|file| "#{Rails.root}/tmp/#{file.original_filename}"}
43
+
44
+ all_page_file_paths = ""
45
+
46
+ file_paths.each do |file_path|
47
+ pdf = PDF::Reader.new(file_path)
48
+ count = pdf.page_count
49
+ counter = 0
50
+
51
+ count.times do
52
+ all_page_file_paths += "#{file_path}[#{counter}] "
53
+ counter += 1
54
+ end
55
+ end
56
+
57
+ Paperclip.run(
58
+ 'convert',
59
+ "-density 150 #{all_page_file_paths} #{File.expand_path(dst.path)}"
60
+ )
61
+
62
+ return dst
63
+ else
64
+ return src
65
+ end
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,72 @@
1
+ # https://github.com/igor-alexandrov/xls_to_csv-paperclip-processor
2
+ #
3
+ # Example Model:
4
+ # class SomeCsvAttachement < ActiveRecord::Base
5
+ # has_attached_file :data,
6
+ # styles: {
7
+ # converted: {
8
+ # format: "csv",
9
+ # params: "-c, -q 3"
10
+ # }
11
+ # },
12
+ # path: ":rails_root/data/some_csv_attachements/:id_partition/:basename.:extension",
13
+ # processors: [:xls_to_csv]
14
+ #
15
+ # validates_attachment_content_type :data, content_type: ['text/csv','text/comma-separated-values','text/csv','application/csv','application/excel','application/vnd.ms-excel','application/vnd.msexcel','text/anytext','text/plain']
16
+ # end
17
+
18
+ class Paperclip::XlsToCsv < Paperclip::Processor
19
+ attr_accessor :file, :params, :format
20
+
21
+ def initialize(file, options = {}, attachment = nil)
22
+ super
23
+
24
+ @file = file
25
+ @params = options[:params]
26
+ @current_format = File.extname(@file.path)
27
+ @basename = File.basename(@file.path, @current_format)
28
+ @format = options[:format]
29
+ end
30
+
31
+ def make
32
+ src = @file
33
+
34
+ if @current_format && ['.xls','xlsx'].include?(@current_format.downcase)
35
+ dst = Tempfile.new([@basename, @format ? "#{@format}" : 'csv'].compact.join("."))
36
+ begin
37
+ Paperclip.run(self.command, self.parameters(src, dst))
38
+ rescue StandardError => e
39
+ raise "There was an error converting #{@basename} to csv: #{e.message}"
40
+ end
41
+ return dst
42
+ else
43
+ return src
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def command
50
+ case @current_format
51
+ when '.xls'
52
+ 'xls2csv'
53
+ when '.xlsx'
54
+ 'xlsx2csv'
55
+ else
56
+ 'cp'
57
+ end
58
+ end
59
+
60
+ def parameters(src, dst)
61
+ p = []
62
+
63
+ if self.command == 'xls2csv'
64
+ p << [@params, File.expand_path(src.path).shellescape, "> #{File.expand_path(dst.path).shellescape}"]
65
+ else
66
+ p << [File.expand_path(src.path).shellescape, File.expand_path(dst.path).shellescape]
67
+ end
68
+
69
+ p.flatten.compact.join(" ").strip.squeeze(" ")
70
+ end
71
+
72
+ end
@@ -1,3 +1,3 @@
1
1
  module PaperclipUtils
2
- VERSION = '1.2.2'
2
+ VERSION = '1.3.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
@@ -106,6 +106,8 @@ files:
106
106
  - README.md
107
107
  - Rakefile
108
108
  - lib/paperclip_processors/ghostscript.rb
109
+ - lib/paperclip_processors/pdf_merge.rb
110
+ - lib/paperclip_processors/xls_to_csv.rb
109
111
  - lib/paperclip_utils.rb
110
112
  - lib/paperclip_utils/version.rb
111
113
  - test/database.yml