carrierwave-base64 2.2.0 → 2.3.0
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.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +10 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -1
- data/README.md +11 -1
- data/Rakefile +4 -6
- data/carrierwave-base64.gemspec +20 -18
- data/lib/carrierwave/base64.rb +5 -4
- data/lib/carrierwave/base64/adapter.rb +6 -5
- data/lib/carrierwave/base64/base64_string_io.rb +6 -5
- data/lib/carrierwave/base64/version.rb +1 -1
- data/spec/.#base64_string_io_spec.rb +1 -0
- data/spec/adapter_spec.rb +46 -24
- data/spec/base64_string_io_spec.rb +18 -39
- data/spec/spec_helper.rb +13 -12
- data/spec/support/schema.rb +4 -4
- metadata +21 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cf760a50a5b061dd501f3ef738c93e11deef874
|
4
|
+
data.tar.gz: b934e247e3419fbc7adcbaf7bb66d2f15a1442e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5e14194c1a0b7e76d32c708de7f46c675bdb9f2cd642f6cc2d75956e9c1ea71d9a4e8f7251e29ab978fb2571ccf26fb63bf50b658a16bb0e255aeadc2f85309
|
7
|
+
data.tar.gz: cda44171485b7081c9b005ac372a1a88b0a8cdf7dc0a8fb92392593532353ea1818205f060ec6c379b6614ad70df43e26a4329de21624d769574c189e6295124
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
@@ -2,14 +2,20 @@ language: ruby
|
|
2
2
|
|
3
3
|
rvm:
|
4
4
|
- 2.0.0
|
5
|
-
- 2.1.
|
6
|
-
- 2.2.
|
5
|
+
- 2.1.9
|
6
|
+
- 2.2.5
|
7
|
+
- 2.3.1
|
7
8
|
- ruby-head
|
8
9
|
|
10
|
+
matrix:
|
11
|
+
allow_failures:
|
12
|
+
- rvm: ruby-head
|
13
|
+
|
9
14
|
gemfile:
|
10
15
|
- Gemfile
|
11
16
|
|
12
17
|
sudo: false
|
13
18
|
|
14
|
-
|
15
|
-
|
19
|
+
script:
|
20
|
+
- bundle exec rspec
|
21
|
+
- bundle exec rubocop
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
# carrierwave-base64 changelog
|
2
|
+
|
3
|
+
## 2.3.0
|
4
|
+
|
5
|
+
- Added `:file_name` option for `mount_base64_uploader` method. All base64 uploads for this attribute will use the given filename for the stored file. The `:file_name` option should not contain the file extention (it will be taken from the content type of base64 string). (@HarenBroog, thanks for the idea)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Upload files encoded as base64 to carrierwave.
|
|
7
7
|
|
8
8
|
This small gem can be useful for API's that interact with mobile devices.
|
9
9
|
|
10
|
-
As of version 2.
|
10
|
+
As of version 2.3.0, this gem requires Ruby 2.0 or higher
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
@@ -33,6 +33,16 @@ mount_base64_uploader :image, ImageUploader
|
|
33
33
|
|
34
34
|
Now you can also upload files by passing an encoded base64 string to the attribute.
|
35
35
|
|
36
|
+
## Setting the file name
|
37
|
+
|
38
|
+
To set the file name for the uploaded files, use the `:file_name` option:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
mount_base64_uploader :image, ImageUploader, file_name: 'userpic'
|
42
|
+
```
|
43
|
+
|
44
|
+
This value will be used, when naming stored files. Extention will be taked from the base64 content-type spec.
|
45
|
+
|
36
46
|
## Data format
|
37
47
|
|
38
48
|
The string with the encoded data, should be prefixed with Data URI scheme format:
|
data/Rakefile
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'rspec/core/rake_task'
|
4
4
|
|
5
|
-
desc
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
7
|
-
t.rspec_opts = %w[--color]
|
8
|
-
end
|
5
|
+
desc 'Run all examples'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
7
|
|
10
8
|
task default: [:spec]
|
data/carrierwave-base64.gemspec
CHANGED
@@ -1,31 +1,33 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
|
5
|
-
require
|
5
|
+
require 'carrierwave/base64/version'
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
|
-
spec.name =
|
8
|
+
spec.name = 'carrierwave-base64'
|
9
9
|
spec.version = Carrierwave::Base64::VERSION
|
10
|
-
spec.authors = [
|
11
|
-
spec.email = [
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
|
15
|
-
spec.
|
10
|
+
spec.authors = ['Yury Lebedev']
|
11
|
+
spec.email = ['lebedev.yurii@gmail.com']
|
12
|
+
spec.summary = 'Upload images encoded as base64 to carrierwave.'
|
13
|
+
spec.description = 'This gem can be useful, if you need to upload files '\
|
14
|
+
'to your API from mobile devises.'
|
15
|
+
spec.homepage = 'https://github.com/lebedev-yury/carrierwave-base64'
|
16
|
+
spec.license = 'MIT'
|
16
17
|
|
17
18
|
spec.files = `git ls-files -z`.split("\x0")
|
18
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
-
spec.require_paths = [
|
21
|
+
spec.require_paths = ['lib']
|
21
22
|
|
22
|
-
spec.add_dependency
|
23
|
+
spec.add_dependency 'carrierwave', ['>= 0.8.0', '< 0.12.0']
|
23
24
|
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
25
|
+
spec.add_development_dependency 'rails', '>= 3.2.0'
|
26
|
+
spec.add_development_dependency 'sqlite3'
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
28
|
+
spec.add_development_dependency 'rake', ['~> 10.0']
|
29
|
+
spec.add_development_dependency 'rspec', ['~> 2.14']
|
30
|
+
spec.add_development_dependency 'sham_rack'
|
31
|
+
spec.add_development_dependency 'pry'
|
32
|
+
spec.add_development_dependency 'rubocop'
|
31
33
|
end
|
data/lib/carrierwave/base64.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'carrierwave/base64/version'
|
2
|
+
require 'carrierwave/base64/base64_string_io'
|
3
|
+
require 'carrierwave/base64/adapter'
|
4
4
|
|
5
5
|
module Carrierwave
|
6
6
|
module Base64
|
@@ -10,7 +10,8 @@ module Carrierwave
|
|
10
10
|
end
|
11
11
|
|
12
12
|
ActiveSupport.on_load :mongoid do
|
13
|
-
Mongoid::Document::ClassMethods.send :include,
|
13
|
+
Mongoid::Document::ClassMethods.send :include,
|
14
|
+
Carrierwave::Base64::Adapter
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -7,11 +7,12 @@ module Carrierwave
|
|
7
7
|
define_method "#{attribute}=" do |data|
|
8
8
|
send "#{attribute}_will_change!" if data.present?
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
super(data) unless data.is_a?(String) &&
|
11
|
+
data.strip.start_with?('data')
|
12
|
+
|
13
|
+
super(Carrierwave::Base64::Base64StringIO.new(
|
14
|
+
data.strip, options[:file_name] || 'file'
|
15
|
+
))
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -3,14 +3,15 @@ module Carrierwave
|
|
3
3
|
class Base64StringIO < StringIO
|
4
4
|
class ArgumentError < StandardError; end
|
5
5
|
|
6
|
-
attr_accessor :file_format
|
6
|
+
attr_accessor :file_format, :file_name
|
7
7
|
|
8
|
-
def initialize(encoded_file)
|
9
|
-
description, encoded_bytes = encoded_file.split(
|
8
|
+
def initialize(encoded_file, file_name)
|
9
|
+
description, encoded_bytes = encoded_file.split(',')
|
10
10
|
|
11
11
|
raise ArgumentError unless encoded_bytes
|
12
|
-
raise ArgumentError if encoded_bytes.eql?(
|
12
|
+
raise ArgumentError if encoded_bytes.eql?('(null)')
|
13
13
|
|
14
|
+
@file_name = file_name
|
14
15
|
@file_format = get_file_format description
|
15
16
|
bytes = ::Base64.decode64 encoded_bytes
|
16
17
|
|
@@ -18,7 +19,7 @@ module Carrierwave
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def original_filename
|
21
|
-
File.basename("
|
22
|
+
File.basename("#{@file_name}.#{@file_format}")
|
22
23
|
end
|
23
24
|
|
24
25
|
private
|
@@ -0,0 +1 @@
|
|
1
|
+
lebedev@MacBook-Pro-2.local.4354
|
data/spec/adapter_spec.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
1
|
RSpec.describe Carrierwave::Base64::Adapter do
|
4
|
-
describe
|
2
|
+
describe '.mount_base64_uploader' do
|
5
3
|
let(:uploader) { Class.new CarrierWave::Uploader::Base }
|
6
4
|
|
7
5
|
subject do
|
@@ -9,40 +7,64 @@ RSpec.describe Carrierwave::Base64::Adapter do
|
|
9
7
|
Post.new
|
10
8
|
end
|
11
9
|
|
12
|
-
it
|
10
|
+
it 'mounts the uploader on the image field' do
|
13
11
|
expect(subject.image).to be_an_instance_of(uploader)
|
14
12
|
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
14
|
+
context 'normal file uploads' do
|
15
|
+
before(:each) do
|
16
|
+
sham_rack_app = ShamRack.at('www.example.com').stub
|
17
|
+
sham_rack_app.register_resource(
|
18
|
+
'/test.jpg', file_path('fixtures', 'test.jpg'), 'images/jpg'
|
19
|
+
)
|
20
|
+
subject[:image] = 'test.jpg'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'sets will_change for the attribute' do
|
24
|
+
expect(subject.changed?).to be_truthy
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
it 'saves the file' do
|
28
|
+
subject.save!
|
29
|
+
subject.reload
|
30
|
+
|
31
|
+
expect(
|
32
|
+
subject.image.current_path
|
33
|
+
).to eq file_path('../uploads', 'test.jpg')
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
|
37
|
+
context 'base64 strings' do
|
38
|
+
before(:each) do
|
39
|
+
subject.image = File.read(
|
40
|
+
file_path('fixtures', 'base64_image.fixture')
|
41
|
+
).strip
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'creates a file' do
|
45
|
+
subject.save!
|
46
|
+
subject.reload
|
47
|
+
|
48
|
+
expect(
|
49
|
+
subject.image.current_path
|
50
|
+
).to eq file_path('../uploads', 'file.jpg')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'sets will_change for the attribute' do
|
54
|
+
expect(subject.changed?).to be_truthy
|
55
|
+
end
|
36
56
|
end
|
37
57
|
|
38
|
-
context
|
58
|
+
context 'stored uploads exist for the field' do
|
39
59
|
before :each do
|
40
|
-
subject.image = File.read(
|
60
|
+
subject.image = File.read(
|
61
|
+
file_path('fixtures', 'base64_image.fixture')
|
62
|
+
).strip
|
41
63
|
subject.save!
|
42
64
|
subject.reload
|
43
65
|
end
|
44
66
|
|
45
|
-
it
|
67
|
+
it 'removes files when remove_* is set to true' do
|
46
68
|
subject.remove_image = true
|
47
69
|
subject.save!
|
48
70
|
expect(subject.reload.image.file).to be_nil
|
@@ -1,55 +1,34 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
1
|
RSpec.describe Carrierwave::Base64::Base64StringIO do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
expect(subject.file_format).to eql("jpg")
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should respond to :original_filename" do
|
13
|
-
expect(subject.original_filename).to eql("file.jpg")
|
14
|
-
end
|
15
|
-
end
|
2
|
+
%w(image/jpg application/pdf audio/mp3).each do |content_type|
|
3
|
+
context "correct #{content_type} data" do
|
4
|
+
let(:data) do
|
5
|
+
"data:#{content_type};base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
|
6
|
+
end
|
16
7
|
|
17
|
-
|
18
|
-
let(:image_data) { "data:application/pdf;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q==" }
|
19
|
-
subject { described_class.new image_data }
|
8
|
+
let(:file_format) { content_type.split('/').last }
|
20
9
|
|
21
|
-
|
22
|
-
expect(subject.file_format).to eql("pdf")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should respond to :original_filename" do
|
26
|
-
expect(subject.original_filename).to eql("file.pdf")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context "correct mp3 data" do
|
31
|
-
let(:audio_data) { "data:audio/mp3;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q==" }
|
32
|
-
subject { described_class.new audio_data }
|
10
|
+
subject { described_class.new data, 'file' }
|
33
11
|
|
34
|
-
|
35
|
-
|
36
|
-
|
12
|
+
it 'determines the file format from the Data URI content type' do
|
13
|
+
expect(subject.file_format).to eql(file_format)
|
14
|
+
end
|
37
15
|
|
38
|
-
|
39
|
-
|
16
|
+
it 'should respond to :original_filename' do
|
17
|
+
expect(subject.original_filename).to eql("file.#{file_format}")
|
18
|
+
end
|
40
19
|
end
|
41
20
|
end
|
42
21
|
|
43
|
-
context
|
44
|
-
it
|
22
|
+
context 'invalid image data' do
|
23
|
+
it 'raises an ArgumentError if Data URI content type is missing' do
|
45
24
|
expect do
|
46
|
-
described_class.new(
|
25
|
+
described_class.new('/9j/4AAQSkZJRgABAQEASABIAADKdhH//2Q==', 'file')
|
47
26
|
end.to raise_error(Carrierwave::Base64::Base64StringIO::ArgumentError)
|
48
27
|
end
|
49
28
|
|
50
|
-
it
|
29
|
+
it 'raises ArgumentError if base64 data eql (null)' do
|
51
30
|
expect do
|
52
|
-
described_class.new(
|
31
|
+
described_class.new('data:image/jpg;base64,(null)', 'file')
|
53
32
|
end.to raise_error(Carrierwave::Base64::Base64StringIO::ArgumentError)
|
54
33
|
end
|
55
34
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'pry'
|
5
|
+
require 'sham_rack'
|
6
6
|
|
7
|
-
require
|
8
|
-
require
|
7
|
+
require 'rails'
|
8
|
+
require 'active_record'
|
9
9
|
|
10
|
-
require
|
11
|
-
require
|
10
|
+
require 'carrierwave'
|
11
|
+
require 'carrierwave/orm/activerecord'
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'carrierwave/base64'
|
14
14
|
|
15
|
-
ActiveRecord::Base.
|
15
|
+
ActiveRecord::Base.raise_in_transactional_callbacks = true
|
16
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
16
17
|
|
17
|
-
load
|
18
|
-
require
|
18
|
+
load 'support/schema.rb'
|
19
|
+
require 'support/models'
|
19
20
|
|
20
21
|
def file_path(*paths)
|
21
22
|
File.expand_path(File.join(File.dirname(__FILE__), *paths))
|
data/spec/support/schema.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
ActiveRecord::Schema.define do
|
2
2
|
self.verbose = false
|
3
3
|
|
4
|
-
create_table
|
5
|
-
t.string
|
6
|
-
t.datetime
|
7
|
-
t.datetime
|
4
|
+
create_table 'posts', force: :cascade do |t|
|
5
|
+
t.string 'image'
|
6
|
+
t.datetime 'created_at', null: false
|
7
|
+
t.datetime 'updated_at', null: false
|
8
8
|
end
|
9
9
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Lebedev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carrierwave
|
@@ -128,6 +128,20 @@ dependencies:
|
|
128
128
|
- - ">="
|
129
129
|
- !ruby/object:Gem::Version
|
130
130
|
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: rubocop
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
131
145
|
description: This gem can be useful, if you need to upload files to your API from
|
132
146
|
mobile devises.
|
133
147
|
email:
|
@@ -137,7 +151,10 @@ extensions: []
|
|
137
151
|
extra_rdoc_files: []
|
138
152
|
files:
|
139
153
|
- ".gitignore"
|
154
|
+
- ".rspec"
|
155
|
+
- ".rubocop.yml"
|
140
156
|
- ".travis.yml"
|
157
|
+
- CHANGELOG.md
|
141
158
|
- Gemfile
|
142
159
|
- LICENSE.txt
|
143
160
|
- README.md
|
@@ -147,6 +164,7 @@ files:
|
|
147
164
|
- lib/carrierwave/base64/adapter.rb
|
148
165
|
- lib/carrierwave/base64/base64_string_io.rb
|
149
166
|
- lib/carrierwave/base64/version.rb
|
167
|
+
- spec/.#base64_string_io_spec.rb
|
150
168
|
- spec/adapter_spec.rb
|
151
169
|
- spec/base64_string_io_spec.rb
|
152
170
|
- spec/fixtures/base64_image.fixture
|
@@ -179,6 +197,7 @@ signing_key:
|
|
179
197
|
specification_version: 4
|
180
198
|
summary: Upload images encoded as base64 to carrierwave.
|
181
199
|
test_files:
|
200
|
+
- spec/.#base64_string_io_spec.rb
|
182
201
|
- spec/adapter_spec.rb
|
183
202
|
- spec/base64_string_io_spec.rb
|
184
203
|
- spec/fixtures/base64_image.fixture
|