carrierwave-mimetype-fu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6e5e6752a68e3ef4f472f7f446672108455aacb
4
+ data.tar.gz: dea660a2a5b36a4a57b3a349b52d3ff5a57eb8e7
5
+ SHA512:
6
+ metadata.gz: b61c5576863443451b2c6ed45225a0aa633217d3e87225d7cdaedf7f21525cbd904d091827f11fa94db6196422e0c54d6fc2d12143273011ea16f4f0d2dd4355
7
+ data.tar.gz: b803bd3bfdeec0e0624967c0f3c771126b06615778eb1ee504a9fd75a9b17ec484ccb8446e1204066e17d4c83346121b358fb7b8068efb253a0fbcf28d713b56
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-mimetype-fu.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Deviantech, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # CarrierWave::MimetypeFu
2
+
3
+ By default, carrierwave uses the uploaded file's extension to guess the content type. Sometimes you'd prefer to actually look at the file and set the content type based on that, so users can't upload php files as *i\_am\_lying.jpg* and have the server try to process them as images.
4
+
5
+ This gem checks the file when it's first uploaded, sets the content type as appropriate and, if the filename's original extension doesn't match the content type, renames it so it does.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'carrierwave-mimetype-fu'
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install carrierwave-mimetype-fu
20
+
21
+ ## Usage
22
+
23
+ Just include the module in your uploader:
24
+
25
+ class ImageUploader < CarrierWave::Uploader::Base
26
+ include CarrierWave::MimetypeFu
27
+ end
28
+
29
+ And now uploaded files' content\_type will be set appropriately, and uploads will automatically be renamed before being passed on to the processors. Given a jpeg file named *test\_1.pdf*, the file will be renamed *test\_1.jpg* before being passed off to normal carrierwave processing.
30
+
31
+ ## History
32
+
33
+ Originally based on the [carrierwave-magic](https://github.com/glebtv/carrierwave-magic) gem, but using [MimetypeFu](https://github.com/mattetti/mimetype-fu) rather than requiring installation of libmagic. Shifted to a different approach in order to check the content type early enough to automatically rename the upload with the proper extension before the processors go to work.
34
+
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave-mimetype-fu/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrierwave-mimetype-fu"
8
+ gem.version = CarrierWave::MimetypeFu::VERSION
9
+ gem.authors = ["Kali Donovan"]
10
+ gem.email = ["kali@deviantech.com"]
11
+ gem.description = %q{mimetype-fu for carrierwave}
12
+ gem.summary = %q{Carrierwave extension to set file content type and extension with mimetype-fu}
13
+ gem.homepage = "https://github.com/deviantech/carrierwave-mimetype-fu"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "mimetype-fu"
21
+ gem.add_runtime_dependency "carrierwave"
22
+ end
@@ -0,0 +1,42 @@
1
+ require "carrierwave-mimetype-fu/version"
2
+
3
+ module CarrierWave
4
+ module MimetypeFu
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ alias_method_chain :cache!, :mimetype_fu_magic
9
+
10
+ begin
11
+ require 'mimetype_fu'
12
+ rescue LoadError => e
13
+ e.message << ' (You may need to install the mimetype-fu gem)'
14
+ raise e
15
+ end
16
+ end
17
+
18
+ def cache_with_mimetype_fu_magic!(new_file = sanitized_file)
19
+ # Only step in on the initial file upload
20
+ return cache_without_mimetype_fu_magic!(new_file) unless new_file.is_a?(ActionDispatch::Http::UploadedFile)
21
+
22
+ begin
23
+ # Collect information about the real content type
24
+ real_content_type = File.mime_type?( File.open(new_file.path) ).split(';').first
25
+ valid_extensions = Array(MIME::Types[real_content_type].try(:first).try(:extensions))
26
+
27
+ # Set proper content type, and update filename if current name doesn't match reach content type
28
+ new_file.content_type = real_content_type
29
+ new_file = CarrierWave::SanitizedFile.new(new_file)
30
+ base, ext = new_file.send(:split_extension, new_file.original_filename)
31
+ ext = valid_extensions.first unless valid_extensions.include?(ext)
32
+
33
+ new_file.instance_variable_set '@original_filename', [base, ext].join('.')
34
+ rescue StandardError => e
35
+ Rails.logger.warn "[carrierwave-mimetype-fu] Exception raised, not fixing image extension. #{e}"
36
+ ensure
37
+ cache_without_mimetype_fu_magic!(new_file)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module MimetypeFu
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-mimetype-fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kali Donovan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mimetype-fu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: carrierwave
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: mimetype-fu for carrierwave
42
+ email:
43
+ - kali@deviantech.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - carrierwave-mimetype-fu.gemspec
54
+ - lib/carrierwave-mimetype-fu.rb
55
+ - lib/carrierwave-mimetype-fu/version.rb
56
+ homepage: https://github.com/deviantech/carrierwave-mimetype-fu
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.1.11
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Carrierwave extension to set file content type and extension with mimetype-fu
79
+ test_files: []