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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43ded0d0612bfca0092a8f69bf75f2cea936c1d2
4
- data.tar.gz: 4ade936bbe6f44cc7e25e5aac7f34cc11a6866fe
3
+ metadata.gz: 39e8fd07baef760e5937db3b05ed6ce369369e52
4
+ data.tar.gz: ed5d91745c21ac7d8642a0d9ee9edaed2e559466
5
5
  SHA512:
6
- metadata.gz: 502a3d71f3829b95dd92f47b1079e338976189ecf448a1c1c5a680b927f267006a3bc084a376d27f9deb7910a0971f29c3e2ab32a8464a389e70144129b3fdef
7
- data.tar.gz: 7c18f72082d986903560a01c20ff51e6e04fb5afc6e03054e7e3535e3e0a806d7e7158063c1509d5a54f30f13b71a34a3c2fef99d63f04a1b46defada8cc54a4
6
+ metadata.gz: 3471e8152f7431832ff5cf1e9e09fd8e4ef3dcc1659532da9702f884be8c32e10201e1a241335d738be8aed6dfcbc017564fa566868a6b1ec9ba9afbfd2eeab3
7
+ data.tar.gz: d339ff54885830de362f14a82dd8dc3adf57c83c5ec961d7f731187d67dbf4436d4d2905d403d35fdb281d36d58f998d345da5b558c5aa300d32838d48838f9d
@@ -1,2 +1,6 @@
1
1
  Documentation:
2
2
  Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - "spec/**/*"
@@ -1,7 +1,6 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.0.0
5
4
  - 2.1.9
6
5
  - 2.2.5
7
6
  - 2.3.1
@@ -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
- As of version 2.3.0, this gem requires Ruby 2.0 or higher
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
- To set the file name for the uploaded files, use the `:file_name` option (without extention):
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: 'userpic'
48
+ mount_base64_uploader :image, ImageUploader, file_name: -> (u) { u.username }
43
49
  ```
44
50
 
45
- You can also pass a Proc for the file-name to allow dynamic filenames.
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
- mount_base64_uploader :image, ImageUploader, file_name: -> { "file-#{DateTime.now.to_i}" }
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
@@ -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', ['>= 0.8.0']
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', ['~> 10.0']
31
- spec.add_development_dependency 'rspec', ['~> 2.14']
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'
@@ -1,3 +1,4 @@
1
+ require 'mime/types/full'
1
2
  require 'carrierwave/base64/version'
2
3
  require 'carrierwave/base64/base64_string_io'
3
4
  require 'carrierwave/base64/adapter'
@@ -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] || 'file'
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 :file_format, :file_name
6
+ attr_accessor :file_extension, :file_name
7
7
 
8
- def initialize(encoded_file, file_name_method_or_string)
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(file_name_method_or_string)
15
- @file_format = get_file_format description
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}.#{@file_format}")
22
+ File.basename("#{@file_name}.#{@file_extension}")
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def get_file_format(description)
28
- regex = /([a-z0-9]+);base64\z/
29
- regex.match(description).try(:[], 1)
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(method_or_string)
33
- if method_or_string.is_a?(Proc)
34
- method_or_string.call
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
- method_or_string
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
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Base64
3
- VERSION = '2.3.5'.freeze
3
+ VERSION = '2.4.0'.freeze
4
4
  end
5
5
  end
@@ -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
- Post.mount_base64_uploader(:image, uploader)
7
- Post.new
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', 'file.jpg')
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(image/jpg application/pdf audio/mp3).each do |content_type|
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(:file_format) { content_type.split('/').last }
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.file_format).to eql(file_format)
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.#{file_format}")
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 = -> { 'file-name-through-method' }
22
- model = described_class.new data, method
23
- expect(model.file_name).to eql('file-name-through-method')
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/jpg;base64,(null)', 'file')
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/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
1
+ data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
@@ -1,4 +1,7 @@
1
- class Post < ActiveRecord::Base
1
+ class User < ActiveRecord::Base
2
+ def username
3
+ 'batman'
4
+ end
2
5
  end
3
6
 
4
7
  class MongoidModel
@@ -1,7 +1,7 @@
1
1
  ActiveRecord::Schema.define do
2
2
  self.verbose = false
3
3
 
4
- create_table 'posts', force: :cascade do |t|
4
+ create_table 'users', force: :cascade do |t|
5
5
  t.string 'image'
6
6
  t.datetime 'created_at', null: false
7
7
  t.datetime 'updated_at', null: false
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.3.5
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