carrierwave-base64 2.3.5 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +0 -1
- data/CHANGELOG.md +10 -0
- data/README.md +15 -5
- data/carrierwave-base64.gemspec +4 -3
- data/lib/carrierwave/base64.rb +1 -0
- data/lib/carrierwave/base64/adapter.rb +9 -3
- data/lib/carrierwave/base64/base64_string_io.rb +21 -12
- data/lib/carrierwave/base64/version.rb +1 -1
- data/spec/adapter_spec.rb +5 -3
- data/spec/base64_string_io_spec.rb +11 -8
- data/spec/fixtures/base64_image.fixture +1 -1
- data/spec/support/models.rb +4 -1
- data/spec/support/schema.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39e8fd07baef760e5937db3b05ed6ce369369e52
|
4
|
+
data.tar.gz: ed5d91745c21ac7d8642a0d9ee9edaed2e559466
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3471e8152f7431832ff5cf1e9e09fd8e4ef3dcc1659532da9702f884be8c32e10201e1a241335d738be8aed6dfcbc017564fa566868a6b1ec9ba9afbfd2eeab3
|
7
|
+
data.tar.gz: d339ff54885830de362f14a82dd8dc3adf57c83c5ec961d7f731187d67dbf4436d4d2905d403d35fdb281d36d58f998d345da5b558c5aa300d32838d48838f9d
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
# carrierwave-base64 changelog
|
2
2
|
|
3
|
+
## 2.4.0
|
4
|
+
|
5
|
+
- The `:file_name` option accepts a lambda with an argument, to which the model instance would be passed. This allows you to set the filename based on some model attribute (@lebedev-yury).
|
6
|
+
- The file extension for the uploaded base64 string is identified automatically, using `MIME::Types`. In case if the mime type for your upload is not identified, you need to add it, using `MIME::Types.add` (@lebedev-yury, @Quintasan, @adamcrown).
|
7
|
+
- **Deprecation**: Setting the `:file_name` option for the uploader to a string is deprecated. It has to be set to a Proc or lambda that returns a string instead (@lebedev-yury).
|
8
|
+
|
3
9
|
## 2.3.5
|
4
10
|
|
5
11
|
- Fixed issue with mongoid models, when `attribute_will_change!` method was called, that wasn't defined in Mongoid models (credits to @cuongnm53)
|
6
12
|
|
13
|
+
## 2.3.4
|
14
|
+
|
15
|
+
- Installation on the windows platform is fixed.
|
16
|
+
|
7
17
|
## 2.3.3
|
8
18
|
|
9
19
|
- Added proc support for the `:file_name` option for the `mount_base64_uploader` method. (credits to @hendricius)
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Upload files encoded as base64 to carrierwave.
|
|
8
8
|
|
9
9
|
This small gem can be useful for API's that interact with mobile devices.
|
10
10
|
|
11
|
-
|
11
|
+
This gem requires Ruby 2.0 or higher.
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
@@ -34,18 +34,28 @@ mount_base64_uploader :image, ImageUploader
|
|
34
34
|
|
35
35
|
Now you can also upload files by passing an encoded base64 string to the attribute.
|
36
36
|
|
37
|
+
## Upload file extension
|
38
|
+
|
39
|
+
The file extension for the uploaded base64 string is identified automatically using the [mime-types](https://github.com/mime-types/ruby-mime-types/) gem and `content_type` from the uploaded string.
|
40
|
+
|
41
|
+
If the required MIME type is not registered, you can add it, using [MIME::Types#add](http://www.rubydoc.info/gems/mime-types/MIME/Types#add-class_method).
|
42
|
+
|
37
43
|
## Setting the file name
|
38
44
|
|
39
|
-
|
45
|
+
You can set the `file_name` option to a lambda, that will return a filename without an extension, using the model instance:
|
40
46
|
|
41
47
|
```ruby
|
42
|
-
mount_base64_uploader :image, ImageUploader, file_name:
|
48
|
+
mount_base64_uploader :image, ImageUploader, file_name: -> (u) { u.username }
|
43
49
|
```
|
44
50
|
|
45
|
-
|
51
|
+
**[DEPRECATED: Settings this option to a string is deprecated, if you still want to set the filename to a fixed string, wrap it in a Proc]** To set the file name for the uploaded files, use the `:file_name` option (without extention):
|
46
52
|
|
47
53
|
```ruby
|
48
|
-
|
54
|
+
# Deprecated way:
|
55
|
+
mount_base64_uploader :image, ImageUploader, file_name: 'userpic'
|
56
|
+
|
57
|
+
# New way
|
58
|
+
mount_base64_uploader :image, ImageUploader, file_name: -> { 'userpic' }
|
49
59
|
```
|
50
60
|
|
51
61
|
## Data format
|
data/carrierwave-base64.gemspec
CHANGED
@@ -20,15 +20,16 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
-
spec.add_dependency 'carrierwave',
|
23
|
+
spec.add_dependency 'carrierwave', '>= 0.8.0'
|
24
|
+
spec.add_dependency 'mime-types', '~> 3.0'
|
24
25
|
|
25
26
|
spec.add_development_dependency 'rails', '>= 3.2.0'
|
26
27
|
spec.add_development_dependency 'sqlite3'
|
27
28
|
spec.add_development_dependency 'mongoid'
|
28
29
|
spec.add_development_dependency 'carrierwave-mongoid'
|
29
30
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
30
|
-
spec.add_development_dependency 'rake',
|
31
|
-
spec.add_development_dependency 'rspec',
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
32
33
|
spec.add_development_dependency 'sham_rack'
|
33
34
|
spec.add_development_dependency 'pry'
|
34
35
|
spec.add_development_dependency 'rubocop'
|
data/lib/carrierwave/base64.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Carrierwave
|
2
2
|
module Base64
|
3
3
|
module Adapter
|
4
|
-
# rubocop:disable Metrics/MethodLength
|
4
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
5
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
5
6
|
def mount_base64_uploader(attribute, uploader_class, options = {})
|
6
7
|
mount_uploader attribute, uploader_class, options
|
7
8
|
|
@@ -13,11 +14,16 @@ module Carrierwave
|
|
13
14
|
return super(data) unless data.is_a?(String) &&
|
14
15
|
data.strip.start_with?('data')
|
15
16
|
|
17
|
+
options[:file_name] ||= -> { attribute }
|
18
|
+
if options[:file_name].is_a?(Proc) && options[:file_name].arity == 1
|
19
|
+
options[:file_name] = options[:file_name].curry[self]
|
20
|
+
end
|
16
21
|
super(Carrierwave::Base64::Base64StringIO.new(
|
17
|
-
data.strip, options[:file_name]
|
22
|
+
data.strip, options[:file_name]
|
18
23
|
))
|
19
24
|
end
|
20
|
-
# rubocop:enable Metrics/MethodLength
|
25
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
26
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
@@ -3,37 +3,46 @@ module Carrierwave
|
|
3
3
|
class Base64StringIO < StringIO
|
4
4
|
class ArgumentError < StandardError; end
|
5
5
|
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :file_extension, :file_name
|
7
7
|
|
8
|
-
def initialize(encoded_file,
|
8
|
+
def initialize(encoded_file, file_name_proc_or_string)
|
9
9
|
description, encoded_bytes = encoded_file.split(',')
|
10
10
|
|
11
11
|
raise ArgumentError unless encoded_bytes
|
12
12
|
raise ArgumentError if encoded_bytes.eql?('(null)')
|
13
13
|
|
14
|
-
@file_name = extract_file_name(
|
15
|
-
@
|
14
|
+
@file_name = extract_file_name(file_name_proc_or_string)
|
15
|
+
@file_extension = get_file_extension description
|
16
16
|
bytes = ::Base64.decode64 encoded_bytes
|
17
17
|
|
18
18
|
super bytes
|
19
19
|
end
|
20
20
|
|
21
21
|
def original_filename
|
22
|
-
File.basename("#{@file_name}.#{@
|
22
|
+
File.basename("#{@file_name}.#{@file_extension}")
|
23
23
|
end
|
24
24
|
|
25
25
|
private
|
26
26
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
27
|
+
def get_file_extension(description)
|
28
|
+
content_type = description.split(';base64').first
|
29
|
+
mime_type = MIME::Types[content_type].first
|
30
|
+
unless mime_type
|
31
|
+
raise ArgumentError, "Unknown MIME type: #{content_type}"
|
32
|
+
end
|
33
|
+
mime_type.preferred_extension
|
30
34
|
end
|
31
35
|
|
32
|
-
def extract_file_name(
|
33
|
-
if
|
34
|
-
|
36
|
+
def extract_file_name(proc_or_string)
|
37
|
+
if proc_or_string.is_a?(Proc)
|
38
|
+
proc_or_string.call
|
35
39
|
else
|
36
|
-
|
40
|
+
warn(
|
41
|
+
'[Deprecation warning] Setting `file_name` option to a string is '\
|
42
|
+
'deprecated and will be removed in 2.5.0. If you want to keep the '\
|
43
|
+
'existing behaviour, wrap the string in a Proc'
|
44
|
+
)
|
45
|
+
proc_or_string
|
37
46
|
end
|
38
47
|
end
|
39
48
|
end
|
data/spec/adapter_spec.rb
CHANGED
@@ -3,8 +3,10 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
3
3
|
let(:uploader) { Class.new CarrierWave::Uploader::Base }
|
4
4
|
|
5
5
|
subject do
|
6
|
-
|
7
|
-
|
6
|
+
User.mount_base64_uploader(
|
7
|
+
:image, uploader, file_name: ->(u) { u.username }
|
8
|
+
)
|
9
|
+
User.new
|
8
10
|
end
|
9
11
|
|
10
12
|
let(:mongoid_model) do
|
@@ -52,7 +54,7 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
52
54
|
|
53
55
|
expect(
|
54
56
|
subject.image.current_path
|
55
|
-
).to eq file_path('../uploads', '
|
57
|
+
).to eq file_path('../uploads', 'batman.jpeg')
|
56
58
|
end
|
57
59
|
|
58
60
|
it 'sets will_change for the attribute' do
|
@@ -1,26 +1,29 @@
|
|
1
1
|
RSpec.describe Carrierwave::Base64::Base64StringIO do
|
2
|
-
%w(
|
2
|
+
%w(application/vnd.openxmlformats-officedocument.wordprocessingml.document
|
3
|
+
image/jpeg application/pdf audio/mpeg).each do |content_type|
|
3
4
|
context "correct #{content_type} data" do
|
4
5
|
let(:data) do
|
5
6
|
"data:#{content_type};base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
|
6
7
|
end
|
7
8
|
|
8
|
-
let(:
|
9
|
+
let(:file_extension) do
|
10
|
+
MIME::Types[content_type].last.preferred_extension
|
11
|
+
end
|
9
12
|
|
10
13
|
subject { described_class.new data, 'file' }
|
11
14
|
|
12
15
|
it 'determines the file format from the Data URI content type' do
|
13
|
-
expect(subject.
|
16
|
+
expect(subject.file_extension).to eql(file_extension)
|
14
17
|
end
|
15
18
|
|
16
19
|
it 'should respond to :original_filename' do
|
17
|
-
expect(subject.original_filename).to eql("file.#{
|
20
|
+
expect(subject.original_filename).to eql("file.#{file_extension}")
|
18
21
|
end
|
19
22
|
|
20
23
|
it 'calls a function that returns the file_name' do
|
21
|
-
method = -> {
|
22
|
-
|
23
|
-
expect(
|
24
|
+
method = ->(u) { u.username }
|
25
|
+
base64_string_io = described_class.new data, method.curry[User.new]
|
26
|
+
expect(base64_string_io.file_name).to eql('batman')
|
24
27
|
end
|
25
28
|
|
26
29
|
it 'accepts a string as the file name as well' do
|
@@ -39,7 +42,7 @@ RSpec.describe Carrierwave::Base64::Base64StringIO do
|
|
39
42
|
|
40
43
|
it 'raises ArgumentError if base64 data eql (null)' do
|
41
44
|
expect do
|
42
|
-
described_class.new('data:image/
|
45
|
+
described_class.new('data:image/jpeg;base64,(null)', 'file')
|
43
46
|
end.to raise_error(Carrierwave::Base64::Base64StringIO::ArgumentError)
|
44
47
|
end
|
45
48
|
end
|
@@ -1 +1 @@
|
|
1
|
-
data:image/
|
1
|
+
data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
|
data/spec/support/models.rb
CHANGED
data/spec/support/schema.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-base64
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Lebedev
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mime-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rails
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|