carrierwave-magic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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-magic.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gleb Tv
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.
@@ -0,0 +1,41 @@
1
+ # CarrierWave::Magic
2
+
3
+ This allows to set file type with ruby-filemagic instead of an extension
4
+
5
+ It also sets correct file extension with mime-types gem.
6
+
7
+ I recommend using 'mime_types' gem from git:
8
+
9
+ gem 'mime-types', github: 'halostatue/mime-types'
10
+
11
+
12
+ Idea and some of the code from this pull request: https://github.com/jnicklas/carrierwave/pull/949
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'carrierwave-magic'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install carrierwave-magic
27
+
28
+ ## Usage
29
+
30
+ class ImageUploader < CarrierWave::Uploader::Base
31
+ include CarrierWave::Magic
32
+ process :set_magic_content_type
33
+ end
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
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrierwave-magic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrierwave-magic"
8
+ gem.version = CarrierWave::Magic::VERSION
9
+ gem.authors = ["Gleb Tv"]
10
+ gem.email = ["glebtv@gmail.com"]
11
+ gem.description = %q{ruby-filemagic and mime-types for carrierwave}
12
+ gem.summary = %q{Set file mime type with ruby-filemagic and extension from mime type with mime-types for carrierwave}
13
+ gem.homepage = ""
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"
21
+ gem.add_runtime_dependency "carrierwave"
22
+ gem.add_runtime_dependency "mime-types"
23
+ end
@@ -0,0 +1,56 @@
1
+ require "carrierwave-magic/version"
2
+
3
+ module CarrierWave
4
+ module Magic
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ begin
9
+ require 'filemagic'
10
+ rescue LoadError => e
11
+ e.message << ' (You may need to install the ruby-filemagic gem)'
12
+ raise e
13
+ end
14
+
15
+ begin
16
+ require "mime/types"
17
+ rescue LoadError => e
18
+ e.message << " (You may need to install the mime-types gem)"
19
+ raise e
20
+ end
21
+
22
+ end
23
+
24
+ module ClassMethods
25
+ def set_magic_content_type(override=false)
26
+ process :set_magic_content_type => override
27
+ end
28
+ end
29
+
30
+ ##
31
+ # Changes the file content_type using the ruby-filemagic gem
32
+ #
33
+ def set_magic_content_type(override=false)
34
+ if override || file.content_type.blank? || generic_content_type?(file.content_type)
35
+ new_content_type = ::FileMagic.new(::FileMagic::MAGIC_MIME).file( file.path ).split(';').first
36
+
37
+ if file.respond_to?(:content_type=)
38
+ file.content_type = new_content_type
39
+ else
40
+ file.instance_variable_set(:@content_type, new_content_type)
41
+ end
42
+
43
+ unless MIME::Types[new_content_type].first.nil?
44
+ if file.respond_to?(:extension=)
45
+ file.extension = MIME::Types[new_content_type].first.extensions.first
46
+ else
47
+ file.instance_variable_set(:@extension, MIME::Types[new_content_type].first.extensions.first)
48
+ end
49
+ end
50
+ end
51
+ rescue ::Exception => e
52
+ raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.magic_mime_types_processing_error", e: e, default: 'Failed to process file with FileMagic, Original Error: %{e}')
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ module CarrierWave
2
+ module Magic
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-magic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gleb Tv
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-filemagic
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: carrierwave
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mime-types
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ruby-filemagic and mime-types for carrierwave
63
+ email:
64
+ - glebtv@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - carrierwave-magic.gemspec
75
+ - lib/carrierwave-magic.rb
76
+ - lib/carrierwave-magic/version.rb
77
+ homepage: ''
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.22
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Set file mime type with ruby-filemagic and extension from mime type with
101
+ mime-types for carrierwave
102
+ test_files: []
103
+ has_rdoc: