carrierwave-magic 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -2,12 +2,30 @@
2
2
 
3
3
  This allows to set file type with ruby-filemagic instead of an extension
4
4
 
5
- It also sets correct file extension with mime-types gem.
6
-
7
- I recommend using 'mime_types' gem from git:
5
+ To set correct file extension on resulting files:
6
+ 1) Add 'mime-types' gem I reccomend using 'mime_types' 1.20 from git:
8
7
 
9
8
  gem 'mime-types', github: 'halostatue/mime-types'
10
9
 
10
+ 2) Add this to your uploader (or any other way)
11
+
12
+ def filename
13
+ if original_filename.present?
14
+ ext = MIME::Types[file.content_type].nil? ? file.extension : MIME::Types[file.content_type].first.extensions.first
15
+ if version_name != :jpg
16
+ "#{secure_token(10)}.#{ext}"
17
+ else
18
+ "#{secure_token(10)}.jpg"
19
+ end
20
+ end
21
+ end
22
+
23
+ protected
24
+ def secure_token(length=16)
25
+ var = :"@#{mounted_as}_secure_token"
26
+ model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
27
+ end
28
+
11
29
 
12
30
  Idea and some of the code from this pull request: https://github.com/jnicklas/carrierwave/pull/949
13
31
 
@@ -19,5 +19,4 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_runtime_dependency "ruby-filemagic"
21
21
  gem.add_runtime_dependency "carrierwave"
22
- gem.add_runtime_dependency "mime-types"
23
22
  end
@@ -27,6 +27,12 @@ module CarrierWave
27
27
  end
28
28
  end
29
29
 
30
+ GENERIC_CONTENT_TYPES = %w[application/octet-stream binary/octet-stream]
31
+
32
+ def generic_content_type?(content_type)
33
+ GENERIC_CONTENT_TYPES.include? content_type
34
+ end
35
+
30
36
  ##
31
37
  # Changes the file content_type using the ruby-filemagic gem
32
38
  #
@@ -39,14 +45,6 @@ module CarrierWave
39
45
  else
40
46
  file.instance_variable_set(:@content_type, new_content_type)
41
47
  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
48
  end
51
49
  rescue ::Exception => e
52
50
  raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.magic_mime_types_processing_error", e: e, default: 'Failed to process file with FileMagic, Original Error: %{e}')
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Magic
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
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
46
  description: ruby-filemagic and mime-types for carrierwave
63
47
  email:
64
48
  - glebtv@gmail.com