carrierwave-base64 2.6.1 → 2.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +10 -0
- data/.travis.yml +10 -3
- data/CHANGELOG.md +16 -0
- data/README.md +4 -6
- data/Rakefile +0 -7
- data/carrierwave-base64.gemspec +11 -11
- data/lib/carrierwave/base64.rb +2 -10
- data/lib/carrierwave/base64/adapter.rb +30 -33
- data/lib/carrierwave/base64/base64_string_io.rb +31 -5
- data/lib/carrierwave/base64/exceptions.rb +2 -0
- data/lib/carrierwave/base64/mounting_helper.rb +63 -0
- data/lib/carrierwave/base64/railtie.rb +15 -0
- data/lib/carrierwave/base64/version.rb +1 -1
- data/spec/adapter_spec.rb +44 -11
- data/spec/base64_string_io_spec.rb +7 -5
- data/spec/fixtures/base64_image.fixture +1 -1
- data/spec/fixtures/test.docx +0 -0
- data/spec/fixtures/test.jpeg +0 -0
- data/spec/fixtures/test.mp3 +0 -0
- data/spec/fixtures/test.pdf +0 -0
- data/spec/fixtures/{test.jpg → test.png} +0 -0
- data/spec/spec_helper.rb +12 -1
- metadata +60 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dcc0254f7258899a78ff178e32c60221aea32cf3551a1dc0702ff1675645024b
|
4
|
+
data.tar.gz: dc0f4f9de01fe428086ac3a3289ae808e53c2a0f80713cee51b8ef177ad542d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e9df88c09c107a39403c969dedd523e7bcd9a152bdcb548ee3250ea63b4e8498b80a33a52903f66530f4ba95bc61c3b63081de7c08e64f62779613cc9ff486c
|
7
|
+
data.tar.gz: d104d6d00b5ee4b3772dd696f9136c0d3335aa460d3fd8543fa484050c22817a1189b21d731a748960f14fbf550c5b060f8e353c137017cda5377be19811a215
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -1,20 +1,27 @@
|
|
1
1
|
language: ruby
|
2
2
|
|
3
3
|
rvm:
|
4
|
-
- 2.
|
5
|
-
- 2.
|
6
|
-
- 2.
|
4
|
+
- 2.4.9
|
5
|
+
- 2.5.7
|
6
|
+
- 2.6.5
|
7
|
+
- 2.7.0
|
7
8
|
- ruby-head
|
9
|
+
- jruby-head
|
8
10
|
|
9
11
|
matrix:
|
10
12
|
allow_failures:
|
11
13
|
- rvm: ruby-head
|
14
|
+
- rvm: jruby-head
|
12
15
|
|
13
16
|
gemfile:
|
14
17
|
- Gemfile
|
15
18
|
|
16
19
|
sudo: false
|
17
20
|
|
21
|
+
before_install:
|
22
|
+
- gem update --system
|
23
|
+
- gem update bundler
|
24
|
+
|
18
25
|
script:
|
19
26
|
- bundle exec rspec
|
20
27
|
- bundle exec rubocop
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# carrierwave-base64 changelog
|
2
2
|
|
3
|
+
## 2.10.0
|
4
|
+
|
5
|
+
- Allow `mount_base64_uploader` to take a block as an argument (#83, @shuuuuun)
|
6
|
+
|
7
|
+
## 2.9.0
|
8
|
+
|
9
|
+
- Mimemagic gem is replaced with Marcel (#87, @oakbow)
|
10
|
+
|
11
|
+
## 2.8.0
|
12
|
+
|
13
|
+
- Mime Type is now detected from file contents, falling back to the type, specified by the user. (#73, @justisb)
|
14
|
+
|
15
|
+
## 2.7.0
|
16
|
+
|
17
|
+
- Railtie is not required by default, so the gem can be used in non Rails app (@sleepingstu)
|
18
|
+
|
3
19
|
## 2.6.1
|
4
20
|
|
5
21
|
This release fixes the issue that the public API of the gem was changed in 2.6.0, by switching from RFC 2045 to RFC 4648 standard for base64 strings.
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Carrierwave::Base64
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/carrierwave-base64.svg)](http://badge.fury.io/rb/carrierwave-base64)
|
4
|
-
[![Build Status](https://travis-ci.org/
|
5
|
-
[![Code Climate](https://codeclimate.com/github/
|
4
|
+
[![Build Status](https://travis-ci.org/y9v/carrierwave-base64.svg?branch=master)](https://travis-ci.org/y9v/carrierwave-base64)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/y9v/carrierwave-base64/badges/gpa.svg)](https://codeclimate.com/github/y9v/carrierwave-base64)
|
6
6
|
|
7
7
|
Upload files encoded as base64 to carrierwave.
|
8
8
|
|
@@ -32,7 +32,7 @@ Mount the uploader to your model:
|
|
32
32
|
mount_base64_uploader :image, ImageUploader
|
33
33
|
```
|
34
34
|
|
35
|
-
Now you can also upload files by passing an encoded base64 string to the attribute.
|
35
|
+
Now you can also upload files by passing an encoded base64 string to the attribute. This also works for normal file uploads from file fields inside an HTML form, so you can safely replace `mount_uploader` with `mount_base64_uploader` to support both file input and base64 encoded input
|
36
36
|
|
37
37
|
## Upload file extension
|
38
38
|
|
@@ -68,9 +68,7 @@ mount_base64_uploader :image, ImageUploader, file_name: -> { 'userpic' }
|
|
68
68
|
|
69
69
|
## Data format
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
Keep in mind, that the content type specified in this string, would be used to identified the file extension. So if you have some custom content type uploaded, don't forget to register a Mime Type for it.
|
71
|
+
File extention for the uploaded file is identified automatically based on the file contents. If it can't be identified automaticaly, it falls back to the content-type, specified in the data string.
|
74
72
|
|
75
73
|
```
|
76
74
|
data:image/jpeg;base64,(base64 encoded data)
|
data/Rakefile
CHANGED
data/carrierwave-base64.gemspec
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
4
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
3
|
|
6
4
|
require 'carrierwave/base64/version'
|
@@ -21,17 +19,19 @@ Gem::Specification.new do |spec|
|
|
21
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
20
|
spec.require_paths = ['lib']
|
23
21
|
|
24
|
-
spec.add_dependency 'carrierwave', '>=
|
22
|
+
spec.add_dependency 'carrierwave', '>= 2.2.1'
|
25
23
|
spec.add_dependency 'mime-types', '~> 3.0'
|
24
|
+
spec.add_dependency 'marcel', '~> 1.0.0'
|
26
25
|
|
27
|
-
spec.add_development_dependency '
|
28
|
-
spec.add_development_dependency 'sqlite3'
|
29
|
-
spec.add_development_dependency 'mongoid'
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
30
27
|
spec.add_development_dependency 'carrierwave-mongoid'
|
31
|
-
spec.add_development_dependency '
|
32
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
-
spec.add_development_dependency 'rspec', '~> 2.14'
|
34
|
-
spec.add_development_dependency 'sham_rack'
|
28
|
+
spec.add_development_dependency 'mongoid'
|
35
29
|
spec.add_development_dependency 'pry'
|
30
|
+
spec.add_development_dependency 'rails', '~> 5'
|
31
|
+
spec.add_development_dependency 'rake', '~> 12.3.3'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
36
33
|
spec.add_development_dependency 'rubocop'
|
34
|
+
spec.add_development_dependency 'sham_rack'
|
35
|
+
spec.add_development_dependency 'sqlite3'
|
36
|
+
spec.add_development_dependency 'yard'
|
37
37
|
end
|
data/lib/carrierwave/base64.rb
CHANGED
@@ -2,19 +2,11 @@ require 'mime/types/full'
|
|
2
2
|
require 'carrierwave/base64/version'
|
3
3
|
require 'carrierwave/base64/exceptions'
|
4
4
|
require 'carrierwave/base64/base64_string_io'
|
5
|
+
require 'carrierwave/base64/mounting_helper'
|
5
6
|
require 'carrierwave/base64/adapter'
|
7
|
+
require 'carrierwave/base64/railtie' if defined?(Rails)
|
6
8
|
|
7
9
|
module Carrierwave
|
8
10
|
module Base64
|
9
|
-
class Railtie < Rails::Railtie
|
10
|
-
ActiveSupport.on_load :active_record do
|
11
|
-
ActiveRecord::Base.extend Carrierwave::Base64::Adapter
|
12
|
-
end
|
13
|
-
|
14
|
-
ActiveSupport.on_load :mongoid do
|
15
|
-
Mongoid::Document::ClassMethods.send :include,
|
16
|
-
Carrierwave::Base64::Adapter
|
17
|
-
end
|
18
|
-
end
|
19
11
|
end
|
20
12
|
end
|
@@ -1,42 +1,39 @@
|
|
1
1
|
module Carrierwave
|
2
2
|
module Base64
|
3
|
+
# Module with .mount_base64_uploader method, that is mixed in to
|
4
|
+
# ActiveRecord::Base or Mongoid::Document::ClassMethods
|
3
5
|
module Adapter
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
6
|
+
# Mounts the carrierwave uploader that can accept a base64 encoded
|
7
|
+
# string as input. It also accepts regular file uploads.
|
8
|
+
#
|
9
|
+
# @param attribute [Symbol] the attribute to mount this uploader on
|
10
|
+
# @param uploader_class [Carrierwave::Uploader] the uploader class to
|
11
|
+
# mount
|
12
|
+
# @param options [Hash{Symbol => Object}] a set of options
|
13
|
+
# @option options [Proc] :file_name Proc that must return a file name
|
14
|
+
# without extension
|
15
|
+
#
|
16
|
+
# @example Mount the uploader and specify the file name
|
17
|
+
# mount_base64_uploader :image, ImageUploader,
|
18
|
+
# file_name: -> (u) { u.username }
|
19
|
+
#
|
20
|
+
# @example Mount the uploader with a block, overriding the store_dir
|
21
|
+
# mount_base64_uploader :image, ImageUploader do
|
22
|
+
# def store_dir
|
23
|
+
# 'images'
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# @return [Symbol] the defined writer method name
|
28
|
+
def mount_base64_uploader(attribute, uploader_class, options = {}, &block)
|
29
|
+
mount_uploader attribute, uploader_class, options, &block
|
9
30
|
options[:file_name] ||= proc { attribute }
|
10
31
|
|
11
|
-
|
12
|
-
warn(
|
13
|
-
'[Deprecation warning] Setting `file_name` option to a string is '\
|
14
|
-
'deprecated and will be removed in 3.0.0. If you want to keep the '\
|
15
|
-
'existing behaviour, wrap the string in a Proc'
|
16
|
-
)
|
17
|
-
end
|
32
|
+
Carrierwave::Base64::MountingHelper.check_for_deprecations(options)
|
18
33
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if respond_to?("#{attribute}_will_change!") && data.present?
|
23
|
-
send "#{attribute}_will_change!"
|
24
|
-
end
|
25
|
-
|
26
|
-
return super(data) unless data.is_a?(String) &&
|
27
|
-
data.strip.start_with?('data')
|
28
|
-
|
29
|
-
filename = if options[:file_name].respond_to?(:call)
|
30
|
-
options[:file_name].call(self)
|
31
|
-
else
|
32
|
-
options[:file_name]
|
33
|
-
end.to_s
|
34
|
-
|
35
|
-
super Carrierwave::Base64::Base64StringIO.new(data.strip, filename)
|
36
|
-
end
|
37
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
38
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
39
|
-
# rubocop:enable Metrics/PerceivedComplexity
|
34
|
+
Carrierwave::Base64::MountingHelper.define_writer(
|
35
|
+
self, attribute, options
|
36
|
+
)
|
40
37
|
end
|
41
38
|
end
|
42
39
|
end
|
@@ -1,8 +1,27 @@
|
|
1
|
+
require 'marcel'
|
2
|
+
|
1
3
|
module Carrierwave
|
2
4
|
module Base64
|
5
|
+
# Class that decodes a base64 string, builds a StringIO for the
|
6
|
+
# decoded bytes, and extracts the file MIME type to build a file
|
7
|
+
# name with extension.
|
3
8
|
class Base64StringIO < StringIO
|
4
|
-
|
9
|
+
# @return [String] the file name without extension
|
10
|
+
attr_reader :file_name
|
11
|
+
|
12
|
+
# @return [String] the file extension for the uploaded file
|
13
|
+
attr_reader :file_extension
|
5
14
|
|
15
|
+
# Returns a StringIO with decoded bytes from the base64 encoded
|
16
|
+
# string and builds a file name with extension for the uploaded file,
|
17
|
+
# based on the MIME type specified in the base64 encoded string.
|
18
|
+
#
|
19
|
+
# @param encoded_file [String] the base64 encoded file contents
|
20
|
+
# @param file_name [String] the file name without extention
|
21
|
+
#
|
22
|
+
# @raise [ArgumentError] If the base64 encoded string is empty
|
23
|
+
#
|
24
|
+
# @return [StringIO] StringIO with decoded bytes
|
6
25
|
def initialize(encoded_file, file_name)
|
7
26
|
description, encoded_bytes = encoded_file.split(',')
|
8
27
|
|
@@ -10,21 +29,28 @@ module Carrierwave
|
|
10
29
|
raise ArgumentError if encoded_bytes.eql?('(null)')
|
11
30
|
|
12
31
|
@file_name = file_name
|
13
|
-
@file_extension = get_file_extension description
|
14
32
|
bytes = ::Base64.decode64 encoded_bytes
|
33
|
+
@file_extension = get_file_extension description, bytes
|
15
34
|
|
16
35
|
super bytes
|
17
36
|
end
|
18
37
|
|
38
|
+
# Returns a file name with extension, based on the MIME type specified
|
39
|
+
# in the base64 encoded string.
|
40
|
+
#
|
41
|
+
# @return [String] File name with extention
|
19
42
|
def original_filename
|
20
43
|
File.basename("#{@file_name}.#{@file_extension}")
|
21
44
|
end
|
22
45
|
|
23
46
|
private
|
24
47
|
|
25
|
-
|
26
|
-
|
27
|
-
|
48
|
+
# Determine content type from input, with provided type as fallback
|
49
|
+
def get_file_extension(description, bytes)
|
50
|
+
detected_type = Marcel::Magic.by_magic(bytes)
|
51
|
+
content_type = (detected_type && detected_type.type) ||
|
52
|
+
description.split(';base64').first
|
53
|
+
mime_type = MIME::Types[content_type].last
|
28
54
|
unless mime_type
|
29
55
|
raise Carrierwave::Base64::UnknownMimeTypeError,
|
30
56
|
"Unknown MIME type: #{content_type}"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Carrierwave
|
2
|
+
module Base64
|
3
|
+
# Module with helper functions for mounting uploaders
|
4
|
+
module MountingHelper
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# Checks for deprecations and prints a warning if found any.
|
8
|
+
# @private
|
9
|
+
#
|
10
|
+
# @param options [Hash{Symbol => Object}] the uploader options
|
11
|
+
# @return [void]
|
12
|
+
def check_for_deprecations(options)
|
13
|
+
return unless options[:file_name].is_a?(String)
|
14
|
+
|
15
|
+
warn(
|
16
|
+
'[Deprecation warning] Setting `file_name` option to a string is '\
|
17
|
+
'deprecated and will be removed in 3.0.0. If you want to keep the '\
|
18
|
+
'existing behaviour, wrap the string in a Proc'
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns a file name for the uploaded file.
|
23
|
+
# @private
|
24
|
+
#
|
25
|
+
# @param model_instance [Object] the model instance object
|
26
|
+
# @param options [Hash{Symbol => Object}] the uploader options
|
27
|
+
# @return [String] File name without extension
|
28
|
+
def file_name(model_instance, options)
|
29
|
+
if options[:file_name].respond_to?(:call)
|
30
|
+
options[:file_name].call(model_instance)
|
31
|
+
else
|
32
|
+
options[:file_name]
|
33
|
+
end.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
# Defines an attribute writer method on the class with mounted uploader.
|
37
|
+
# @private
|
38
|
+
#
|
39
|
+
# @param klass [Class] the class with mounted uploader
|
40
|
+
# @param attr [Symbol] the attribute for which the writer will be defined
|
41
|
+
# @param options [Hash{Symbol => Object}] a set of options
|
42
|
+
# @return [Symbol] the defined writer method name
|
43
|
+
def define_writer(klass, attr, options)
|
44
|
+
klass.send(:define_method, "#{attr}=") do |data|
|
45
|
+
# rubocop:disable Lint/NonLocalExitFromIterator
|
46
|
+
return if data.to_s.empty? || data == send(attr).to_s
|
47
|
+
|
48
|
+
# rubocop:enable Lint/NonLocalExitFromIterator
|
49
|
+
|
50
|
+
send("#{attr}_will_change!") if respond_to?("#{attr}_will_change!")
|
51
|
+
|
52
|
+
return super(data) unless data.is_a?(String) &&
|
53
|
+
data.strip.start_with?('data')
|
54
|
+
|
55
|
+
super Carrierwave::Base64::Base64StringIO.new(
|
56
|
+
data.strip,
|
57
|
+
Carrierwave::Base64::MountingHelper.file_name(self, options)
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Carrierwave
|
2
|
+
module Base64
|
3
|
+
# Railtie class to load the carrierwave-base64 adapters
|
4
|
+
# Loads adapters for ActiveRecord and Mongoid
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
ActiveRecord::Base.extend Carrierwave::Base64::Adapter
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load :mongoid do
|
11
|
+
Mongoid::Document::ClassMethods.include Carrierwave::Base64::Adapter
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/adapter_spec.rb
CHANGED
@@ -30,11 +30,10 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
30
30
|
|
31
31
|
it 'creates a file' do
|
32
32
|
subject.save!
|
33
|
-
subject.reload
|
34
33
|
|
35
34
|
expect(
|
36
35
|
subject.image.current_path
|
37
|
-
).to eq file_path('../uploads', 'image.
|
36
|
+
).to eq file_path('../uploads', 'image.png')
|
38
37
|
end
|
39
38
|
end
|
40
39
|
end
|
@@ -86,7 +85,6 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
86
85
|
|
87
86
|
it 'saves the file' do
|
88
87
|
subject.save!
|
89
|
-
subject.reload
|
90
88
|
|
91
89
|
expect(
|
92
90
|
subject.image.current_path
|
@@ -103,11 +101,10 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
103
101
|
|
104
102
|
it 'creates a file' do
|
105
103
|
subject.save!
|
106
|
-
subject.reload
|
107
104
|
|
108
105
|
expect(
|
109
106
|
subject.image.current_path
|
110
|
-
).to eq file_path('../uploads', 'batman.
|
107
|
+
).to eq file_path('../uploads', 'batman.png')
|
111
108
|
end
|
112
109
|
|
113
110
|
it 'sets will_change for the attribute' do
|
@@ -126,10 +123,9 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
126
123
|
it 'should invoke the file_name proc upon each upload' do
|
127
124
|
subject.save!
|
128
125
|
another_subject.save!
|
129
|
-
another_subject.reload
|
130
126
|
expect(
|
131
127
|
another_subject.image.current_path
|
132
|
-
).to eq file_path('../uploads', 'robin.
|
128
|
+
).to eq file_path('../uploads', 'robin.png')
|
133
129
|
end
|
134
130
|
end
|
135
131
|
end
|
@@ -140,21 +136,58 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
140
136
|
file_path('fixtures', 'base64_image.fixture')
|
141
137
|
).strip
|
142
138
|
subject.save!
|
143
|
-
subject.reload
|
144
139
|
end
|
145
140
|
|
146
141
|
it 'keeps the file when setting the attribute to existing value' do
|
147
|
-
expect(File.exist?(subject.
|
142
|
+
expect(File.exist?(subject.image.file.file)).to be_truthy
|
148
143
|
subject.update!(image: subject.image.to_s)
|
149
|
-
expect(File.exist?(subject.
|
144
|
+
expect(File.exist?(subject.image.file.file)).to be_truthy
|
150
145
|
end
|
151
146
|
|
152
147
|
it 'removes files when remove_* is set to true' do
|
153
148
|
subject.remove_image = true
|
154
149
|
subject.save!
|
155
|
-
expect(subject.
|
150
|
+
expect(subject.image.file).to be_nil
|
156
151
|
end
|
157
152
|
end
|
158
153
|
end
|
154
|
+
|
155
|
+
context 'models with presence validation on attribute with uploader' do
|
156
|
+
subject do
|
157
|
+
User.validates(:image, presence: true)
|
158
|
+
User.mount_base64_uploader(:image, uploader)
|
159
|
+
User.new
|
160
|
+
end
|
161
|
+
|
162
|
+
before(:each) do
|
163
|
+
subject.image = File.read(
|
164
|
+
file_path('fixtures', 'base64_image.fixture')
|
165
|
+
).strip
|
166
|
+
subject.save!
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'gives no false positive on presence validation' do
|
170
|
+
expect { subject.update!(username: 'new-username') }.not_to raise_error
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'models with a block for the uploader' do
|
175
|
+
subject do
|
176
|
+
User.mount_base64_uploader(:image, uploader) do
|
177
|
+
def monkey
|
178
|
+
'blah'
|
179
|
+
end
|
180
|
+
end
|
181
|
+
User.new
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'should return an instance of a subclass of CarrierWave::Uploader::Base' do
|
185
|
+
expect(subject.image).to be_a(CarrierWave::Uploader::Base)
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'should apply any custom modifications' do
|
189
|
+
expect(subject.image.monkey).to eq('blah')
|
190
|
+
end
|
191
|
+
end
|
159
192
|
end
|
160
193
|
end
|
@@ -2,17 +2,19 @@ RSpec.describe Carrierwave::Base64::Base64StringIO do
|
|
2
2
|
%w[application/vnd.openxmlformats-officedocument.wordprocessingml.document
|
3
3
|
image/jpeg application/pdf audio/mpeg].each do |content_type|
|
4
4
|
context "correct #{content_type} data" do
|
5
|
-
let(:data) do
|
6
|
-
"data:#{content_type};base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
|
7
|
-
end
|
8
|
-
|
9
5
|
let(:file_extension) do
|
10
6
|
MIME::Types[content_type].last.preferred_extension
|
11
7
|
end
|
12
8
|
|
9
|
+
let(:data) do
|
10
|
+
file_name = "test.#{file_extension}"
|
11
|
+
bytes = File.read(file_path('fixtures', file_name))
|
12
|
+
"data:#{content_type};base64,#{::Base64.encode64(bytes)}"
|
13
|
+
end
|
14
|
+
|
13
15
|
subject { described_class.new data, 'file' }
|
14
16
|
|
15
|
-
it 'determines the file format from the
|
17
|
+
it 'determines the file format from the content type' do
|
16
18
|
expect(subject.file_extension).to eql(file_extension)
|
17
19
|
end
|
18
20
|
|
@@ -1 +1 @@
|
|
1
|
-
data:image/
|
1
|
+
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
@@ -14,7 +14,6 @@ require 'carrierwave/mongoid'
|
|
14
14
|
|
15
15
|
require 'carrierwave/base64'
|
16
16
|
|
17
|
-
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
18
17
|
ActiveRecord::Base.establish_connection(
|
19
18
|
adapter: 'sqlite3',
|
20
19
|
database: ':memory:'
|
@@ -29,3 +28,15 @@ def file_path(*paths)
|
|
29
28
|
end
|
30
29
|
|
31
30
|
CarrierWave.root = ''
|
31
|
+
|
32
|
+
# Add preferred file types similar to how users would implement custom types
|
33
|
+
content_types = {
|
34
|
+
'audio/mpeg' => 'mp3'
|
35
|
+
}
|
36
|
+
content_types.each do |content_type, extension|
|
37
|
+
MIME::Types.add(
|
38
|
+
MIME::Type.new(content_type).tap do |type|
|
39
|
+
type.preferred_extension = extension
|
40
|
+
end
|
41
|
+
)
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-base64
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Lebedev
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.2.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: mime-types
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,21 +39,35 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: marcel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
61
|
+
version: '2.1'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '2.1'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
70
|
+
name: carrierwave-mongoid
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - ">="
|
@@ -81,7 +95,7 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: pry
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
@@ -95,33 +109,33 @@ dependencies:
|
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: rails
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
115
|
- - "~>"
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
117
|
+
version: '5'
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
122
|
- - "~>"
|
109
123
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
124
|
+
version: '5'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: rake
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
114
128
|
requirements:
|
115
129
|
- - "~>"
|
116
130
|
- !ruby/object:Gem::Version
|
117
|
-
version:
|
131
|
+
version: 12.3.3
|
118
132
|
type: :development
|
119
133
|
prerelease: false
|
120
134
|
version_requirements: !ruby/object:Gem::Requirement
|
121
135
|
requirements:
|
122
136
|
- - "~>"
|
123
137
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
138
|
+
version: 12.3.3
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: rspec
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +150,20 @@ dependencies:
|
|
136
150
|
- - "~>"
|
137
151
|
- !ruby/object:Gem::Version
|
138
152
|
version: '2.14'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rubocop
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
139
167
|
- !ruby/object:Gem::Dependency
|
140
168
|
name: sham_rack
|
141
169
|
requirement: !ruby/object:Gem::Requirement
|
@@ -151,7 +179,7 @@ dependencies:
|
|
151
179
|
- !ruby/object:Gem::Version
|
152
180
|
version: '0'
|
153
181
|
- !ruby/object:Gem::Dependency
|
154
|
-
name:
|
182
|
+
name: sqlite3
|
155
183
|
requirement: !ruby/object:Gem::Requirement
|
156
184
|
requirements:
|
157
185
|
- - ">="
|
@@ -165,7 +193,7 @@ dependencies:
|
|
165
193
|
- !ruby/object:Gem::Version
|
166
194
|
version: '0'
|
167
195
|
- !ruby/object:Gem::Dependency
|
168
|
-
name:
|
196
|
+
name: yard
|
169
197
|
requirement: !ruby/object:Gem::Requirement
|
170
198
|
requirements:
|
171
199
|
- - ">="
|
@@ -200,11 +228,17 @@ files:
|
|
200
228
|
- lib/carrierwave/base64/adapter.rb
|
201
229
|
- lib/carrierwave/base64/base64_string_io.rb
|
202
230
|
- lib/carrierwave/base64/exceptions.rb
|
231
|
+
- lib/carrierwave/base64/mounting_helper.rb
|
232
|
+
- lib/carrierwave/base64/railtie.rb
|
203
233
|
- lib/carrierwave/base64/version.rb
|
204
234
|
- spec/adapter_spec.rb
|
205
235
|
- spec/base64_string_io_spec.rb
|
206
236
|
- spec/fixtures/base64_image.fixture
|
207
|
-
- spec/fixtures/test.
|
237
|
+
- spec/fixtures/test.docx
|
238
|
+
- spec/fixtures/test.jpeg
|
239
|
+
- spec/fixtures/test.mp3
|
240
|
+
- spec/fixtures/test.pdf
|
241
|
+
- spec/fixtures/test.png
|
208
242
|
- spec/spec_helper.rb
|
209
243
|
- spec/support/custom_expectations/warn_expectation.rb
|
210
244
|
- spec/support/models.rb
|
@@ -213,7 +247,7 @@ homepage: https://github.com/lebedev-yury/carrierwave-base64
|
|
213
247
|
licenses:
|
214
248
|
- MIT
|
215
249
|
metadata: {}
|
216
|
-
post_install_message:
|
250
|
+
post_install_message:
|
217
251
|
rdoc_options: []
|
218
252
|
require_paths:
|
219
253
|
- lib
|
@@ -228,16 +262,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
228
262
|
- !ruby/object:Gem::Version
|
229
263
|
version: '0'
|
230
264
|
requirements: []
|
231
|
-
|
232
|
-
|
233
|
-
signing_key:
|
265
|
+
rubygems_version: 3.1.4
|
266
|
+
signing_key:
|
234
267
|
specification_version: 4
|
235
268
|
summary: Upload images encoded as base64 to carrierwave.
|
236
269
|
test_files:
|
237
270
|
- spec/adapter_spec.rb
|
238
271
|
- spec/base64_string_io_spec.rb
|
239
272
|
- spec/fixtures/base64_image.fixture
|
240
|
-
- spec/fixtures/test.
|
273
|
+
- spec/fixtures/test.docx
|
274
|
+
- spec/fixtures/test.jpeg
|
275
|
+
- spec/fixtures/test.mp3
|
276
|
+
- spec/fixtures/test.pdf
|
277
|
+
- spec/fixtures/test.png
|
241
278
|
- spec/spec_helper.rb
|
242
279
|
- spec/support/custom_expectations/warn_expectation.rb
|
243
280
|
- spec/support/models.rb
|