carrierwave-mimetype-magic 0.0.2

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: 26002819171a212e2cf620bb3493cd37debff895
4
+ data.tar.gz: 0078c461a3bddb85b7a6e2c7401e368e10e80981
5
+ SHA512:
6
+ metadata.gz: b4bbd99413ac836b6bf59a515a5d4eb2e83568d2ae43dc3026335326a0ce0873be85f533450ec91990bdb8a79387b5bdac292ae09a1755e62c7866dde1869875
7
+ data.tar.gz: 5dfb011fd71be4d3655683af9cb0cd7ed4a3d77494e92c9675f7c2781a61f549be85d9d9a62019f96acd75556f67a8afaea595aeaa32181849ea5ee85500e961
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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # changelog
2
+
3
+ ## `0.0.2`
4
+
5
+ * Fix Gemfile
6
+
7
+ ## `0.0.1`
8
+
9
+ * Added initial functionality.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Piton4eg
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,41 @@
1
+ # CarrierWave::MimetypeMagic
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-magic'
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install carrierwave-mimetype-magic
20
+
21
+ ## Usage
22
+
23
+ Just include the module in your uploader:
24
+
25
+ class ImageUploader < CarrierWave::Uploader::Base
26
+ include CarrierWave::MimetypeMagic
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-mymetype-fu](https://github.com/deviantech/carrierwave-mimetype-fu) (that based on [carrierwave-magic](https://github.com/glebtv/carrierwave-magic)) gem, but using [ruby-filemagic](https://github.com/blackwinter/ruby-filemagic) rather than MimetypeFu, that works incorrect with MS files.
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
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-magic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrierwave-mimetype-magic"
8
+ gem.version = CarrierWave::MimetypeMagic::VERSION
9
+ gem.authors = ["Piton4eg"]
10
+ gem.email = ["piton4eg@mail.ru"]
11
+ gem.description = %q{Real mime-types for carrierwave}
12
+ gem.summary = %q{Carrierwave extension to set file content type and extension with ruby-filemagic from real mime type}
13
+ gem.homepage = "https://github.com/piton4eg/carrierwave-mimetype-magic"
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 "ruby-filemagic", '~> 0.7.1'
21
+ gem.add_runtime_dependency "carrierwave", '~> 0.10.0'
22
+
23
+ gem.license = 'MIT'
24
+ end
@@ -0,0 +1,48 @@
1
+ require "carrierwave-mimetype-magic/version"
2
+
3
+ module CarrierWave
4
+ module MimetypeMagic
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ alias_method_chain :cache!, :mimetype_magic
9
+
10
+ begin
11
+ require 'filemagic'
12
+ rescue LoadError => e
13
+ e.message << ' (You may need to install the ruby-filemagic gem)'
14
+ raise e
15
+ end
16
+ end
17
+
18
+ def cache_with_mimetype_magic!(new_file = sanitized_file)
19
+ # Only step in on the initial file upload
20
+ opened_file = case new_file
21
+ when CarrierWave::Uploader::Download::RemoteFile then new_file.send(:file)
22
+ when ActionDispatch::Http::UploadedFile then File.open(new_file.path)
23
+ else nil
24
+ end
25
+
26
+ return cache_without_mimetype_magic!(new_file) unless opened_file
27
+
28
+
29
+ begin
30
+ # Collect information about the real content type
31
+ real_content_type = ::FileMagic.new(::FileMagic::MAGIC_MIME).file(opened_file.path).split(';').first
32
+ valid_extensions = Array(MIME::Types[real_content_type].try(:first).try(:extensions))
33
+
34
+ # Set proper content type, and update filename if current name doesn't match reach content type
35
+ new_file = CarrierWave::SanitizedFile.new(new_file)
36
+ new_file.content_type = real_content_type
37
+ base, ext = new_file.send(:split_extension, new_file.original_filename)
38
+ ext = valid_extensions.first unless valid_extensions.include?(ext)
39
+
40
+ new_file.instance_variable_set '@original_filename', [base, ext].join('.')
41
+ rescue StandardError => e
42
+ Rails.logger.warn "[carrierwave-mimetype] Exception raised, not fixing image extension. #{e}"
43
+ ensure
44
+ cache_without_mimetype_magic!(new_file)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module MimetypeMagic
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-mimetype-magic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Piton4eg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-filemagic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.1
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.10.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.10.0
41
+ description: Real mime-types for carrierwave
42
+ email:
43
+ - piton4eg@mail.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - carrierwave-mimetype-magic.gemspec
55
+ - lib/carrierwave-mimetype-magic.rb
56
+ - lib/carrierwave-mimetype-magic/version.rb
57
+ homepage: https://github.com/piton4eg/carrierwave-mimetype-magic
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.5.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Carrierwave extension to set file content type and extension with ruby-filemagic
81
+ from real mime type
82
+ test_files: []