carrierwave-base64_image_content 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 792d4946b4a48c18628dcde7e067e7da5f42538f53c1e144eeed1c5d0e6552e2
4
+ data.tar.gz: 88bf0825547250a6de26a187130c4ec3108089c6a0f70e7e14a4b9d07b61331e
5
+ SHA512:
6
+ metadata.gz: f143fc46ff04cecb065d5868c20b6269fbeeb47878596a132387e9766e78635847848f272b371f74ceacd48bd1fb43f5e5fc139015cd7894f738f93007d1620a
7
+ data.tar.gz: f973ee25fc29c9242614abf6c0f006954c1b2038afce0e06fab9bd9e2d266c4d607d0875574af304ceba16a5b873a494cf5cd827cb43c3b5a800b70430253ccc
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /uploads
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - 'bin/**/*'
7
+ - '.git/**/*'
8
+
9
+ Rails:
10
+ Enabled: true
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Layout/MultilineOperationIndentation:
16
+ EnforcedStyle: indented
17
+
18
+ Layout/MultilineMethodCallIndentation:
19
+ EnforcedStyle: indented
20
+
21
+ Metrics/BlockLength:
22
+ ExcludedMethods:
23
+ - context
24
+ - RSpec.describe
25
+ - describe
26
+
27
+ RSpec/FilePath:
28
+ CustomTransform:
29
+ CarrierWave: carrierwave
30
+
31
+ RSpec/ExampleLength:
32
+ Enabled: false
33
+
34
+ RSpec/MultipleExpectations:
35
+ Enabled: false
36
+
37
+ Rails/ApplicationRecord:
38
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.3
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+
5
+ addons:
6
+ postgresql: '9.4'
7
+
8
+ before_script:
9
+ - psql -c 'create database "carrierwave-base64_image_content_test";' -U postgres
10
+
11
+ jobs:
12
+ include:
13
+ - stage: test
14
+ name: Rubocop
15
+ script: bundle exec rubocop -D
16
+ - stage: test
17
+ name: RSpec
18
+ before_script:
19
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
20
+ - chmod +x ./cc-test-reporter
21
+ - ./cc-test-reporter before-build
22
+ script: bundle exec rspec spec
23
+ after_script:
24
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mail@koffeinfrei.org. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in carrierwave-base64_image_content.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,187 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ carrierwave-base64_image_content (0.1.0)
5
+ activerecord (>= 4.2)
6
+ carrierwave (>= 1.2)
7
+ rails (>= 4.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (5.2.2)
13
+ actionpack (= 5.2.2)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailer (5.2.2)
17
+ actionpack (= 5.2.2)
18
+ actionview (= 5.2.2)
19
+ activejob (= 5.2.2)
20
+ mail (~> 2.5, >= 2.5.4)
21
+ rails-dom-testing (~> 2.0)
22
+ actionpack (5.2.2)
23
+ actionview (= 5.2.2)
24
+ activesupport (= 5.2.2)
25
+ rack (~> 2.0)
26
+ rack-test (>= 0.6.3)
27
+ rails-dom-testing (~> 2.0)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ actionview (5.2.2)
30
+ activesupport (= 5.2.2)
31
+ builder (~> 3.1)
32
+ erubi (~> 1.4)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
35
+ activejob (5.2.2)
36
+ activesupport (= 5.2.2)
37
+ globalid (>= 0.3.6)
38
+ activemodel (5.2.2)
39
+ activesupport (= 5.2.2)
40
+ activerecord (5.2.2)
41
+ activemodel (= 5.2.2)
42
+ activesupport (= 5.2.2)
43
+ arel (>= 9.0)
44
+ activestorage (5.2.2)
45
+ actionpack (= 5.2.2)
46
+ activerecord (= 5.2.2)
47
+ marcel (~> 0.3.1)
48
+ activesupport (5.2.2)
49
+ concurrent-ruby (~> 1.0, >= 1.0.2)
50
+ i18n (>= 0.7, < 2)
51
+ minitest (~> 5.1)
52
+ tzinfo (~> 1.1)
53
+ arel (9.0.0)
54
+ ast (2.4.0)
55
+ builder (3.2.3)
56
+ carrierwave (1.2.3)
57
+ activemodel (>= 4.0.0)
58
+ activesupport (>= 4.0.0)
59
+ mime-types (>= 1.16)
60
+ coderay (1.1.2)
61
+ concurrent-ruby (1.1.3)
62
+ crass (1.0.4)
63
+ diff-lcs (1.3)
64
+ docile (1.3.1)
65
+ erubi (1.7.1)
66
+ globalid (0.4.1)
67
+ activesupport (>= 4.2.0)
68
+ i18n (1.2.0)
69
+ concurrent-ruby (~> 1.0)
70
+ jaro_winkler (1.5.1)
71
+ json (2.1.0)
72
+ loofah (2.2.3)
73
+ crass (~> 1.0.2)
74
+ nokogiri (>= 1.5.9)
75
+ mail (2.7.1)
76
+ mini_mime (>= 0.1.1)
77
+ marcel (0.3.3)
78
+ mimemagic (~> 0.3.2)
79
+ method_source (0.9.2)
80
+ mime-types (3.2.2)
81
+ mime-types-data (~> 3.2015)
82
+ mime-types-data (3.2018.0812)
83
+ mimemagic (0.3.2)
84
+ mini_mime (1.0.1)
85
+ mini_portile2 (2.3.0)
86
+ minitest (5.11.3)
87
+ nio4r (2.3.1)
88
+ nokogiri (1.8.5)
89
+ mini_portile2 (~> 2.3.0)
90
+ parallel (1.12.1)
91
+ parser (2.5.3.0)
92
+ ast (~> 2.4.0)
93
+ pg (1.1.3)
94
+ powerpack (0.1.2)
95
+ pry (0.12.2)
96
+ coderay (~> 1.1.0)
97
+ method_source (~> 0.9.0)
98
+ rack (2.0.6)
99
+ rack-test (1.1.0)
100
+ rack (>= 1.0, < 3)
101
+ rails (5.2.2)
102
+ actioncable (= 5.2.2)
103
+ actionmailer (= 5.2.2)
104
+ actionpack (= 5.2.2)
105
+ actionview (= 5.2.2)
106
+ activejob (= 5.2.2)
107
+ activemodel (= 5.2.2)
108
+ activerecord (= 5.2.2)
109
+ activestorage (= 5.2.2)
110
+ activesupport (= 5.2.2)
111
+ bundler (>= 1.3.0)
112
+ railties (= 5.2.2)
113
+ sprockets-rails (>= 2.0.0)
114
+ rails-dom-testing (2.0.3)
115
+ activesupport (>= 4.2.0)
116
+ nokogiri (>= 1.6)
117
+ rails-html-sanitizer (1.0.4)
118
+ loofah (~> 2.2, >= 2.2.2)
119
+ railties (5.2.2)
120
+ actionpack (= 5.2.2)
121
+ activesupport (= 5.2.2)
122
+ method_source
123
+ rake (>= 0.8.7)
124
+ thor (>= 0.19.0, < 2.0)
125
+ rainbow (3.0.0)
126
+ rake (10.5.0)
127
+ rspec (3.8.0)
128
+ rspec-core (~> 3.8.0)
129
+ rspec-expectations (~> 3.8.0)
130
+ rspec-mocks (~> 3.8.0)
131
+ rspec-core (3.8.0)
132
+ rspec-support (~> 3.8.0)
133
+ rspec-expectations (3.8.2)
134
+ diff-lcs (>= 1.2.0, < 2.0)
135
+ rspec-support (~> 3.8.0)
136
+ rspec-mocks (3.8.0)
137
+ diff-lcs (>= 1.2.0, < 2.0)
138
+ rspec-support (~> 3.8.0)
139
+ rspec-support (3.8.0)
140
+ rubocop (0.61.1)
141
+ jaro_winkler (~> 1.5.1)
142
+ parallel (~> 1.10)
143
+ parser (>= 2.5, != 2.5.1.1)
144
+ powerpack (~> 0.1)
145
+ rainbow (>= 2.2.2, < 4.0)
146
+ ruby-progressbar (~> 1.7)
147
+ unicode-display_width (~> 1.4.0)
148
+ rubocop-rspec (1.30.1)
149
+ rubocop (>= 0.60.0)
150
+ ruby-progressbar (1.10.0)
151
+ simplecov (0.16.1)
152
+ docile (~> 1.1)
153
+ json (>= 1.8, < 3)
154
+ simplecov-html (~> 0.10.0)
155
+ simplecov-html (0.10.2)
156
+ sprockets (3.7.2)
157
+ concurrent-ruby (~> 1.0)
158
+ rack (> 1, < 3)
159
+ sprockets-rails (3.2.1)
160
+ actionpack (>= 4.0)
161
+ activesupport (>= 4.0)
162
+ sprockets (>= 3.0.0)
163
+ thor (0.20.3)
164
+ thread_safe (0.3.6)
165
+ tzinfo (1.2.5)
166
+ thread_safe (~> 0.1)
167
+ unicode-display_width (1.4.0)
168
+ websocket-driver (0.7.0)
169
+ websocket-extensions (>= 0.1.0)
170
+ websocket-extensions (0.1.3)
171
+
172
+ PLATFORMS
173
+ ruby
174
+
175
+ DEPENDENCIES
176
+ bundler (~> 1.16)
177
+ carrierwave-base64_image_content!
178
+ pg
179
+ pry
180
+ rake (~> 10.0)
181
+ rspec (~> 3.0)
182
+ rubocop
183
+ rubocop-rspec
184
+ simplecov
185
+
186
+ BUNDLED WITH
187
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ [![Build Status](https://travis-ci.org/panter/carrierwave-base64_image_content.svg?branch=master)](https://travis-ci.org/panter/carrierwave-base64_image_content)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/3dbc2aeb57d7f3dd4cde/maintainability)](https://codeclimate.com/github/panter/carrierwave-base64_image_content/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/3dbc2aeb57d7f3dd4cde/test_coverage)](https://codeclimate.com/github/panter/carrierwave-base64_image_content/test_coverage)
4
+
5
+ # CarrierWave::Base64ImageContent
6
+
7
+ This gem allows storing content with base64 images to a model with CarrierWave
8
+ file uploads. The base64 images are extracted from the content and stored as
9
+ physical files using CarrierWave's configured storage. When reading the content
10
+ the files are converted back to base64 images.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'carrierwave-base64_image_content'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install carrierwave-base64_image_content
27
+
28
+ ## Usage
29
+
30
+ The prerequisite for this gem is a model with an existing [multiple file
31
+ uploads
32
+ field](https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads).
33
+
34
+ 1. Include the module `CarrierWave::Base64ImageContent::Store`
35
+ 1. Call `base64_image_content_store` with the following two parameters:
36
+ * The model's content attribute, which is the actual database field that
37
+ stores the content
38
+ * The images attribute, which uses CarrierWave to store the files
39
+
40
+
41
+ ```ruby
42
+ class Note < ActiveRecord::Base
43
+ include CarrierWave::Base64ImageContent::Store
44
+
45
+ mount_uploaders :images, ImageUploader
46
+
47
+ base64_image_content_store content: :text_content, images: :images
48
+ end
49
+ ```
50
+
51
+ ### Example
52
+
53
+ ```ruby
54
+ # Create a note containing a base64 image in the content
55
+ Note.create!(
56
+ content: 'content1 <img src="data:image/png;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=" />'
57
+ )
58
+
59
+ # The stored content has the base64 image replaced
60
+ note.text_content
61
+ # => content1 <img src="360593ff547c864bd9d16bbed6eb8860d9fad9a407aa74e066039db23b525338" />
62
+
63
+ # The note has one image...
64
+ note.images.count
65
+ # => 1
66
+
67
+ # ... which corresponds to the original base64 image
68
+ note.images.first.filename
69
+ # => "360593ff547c864bd9d16bbed6eb8860d9fad9a407aa74e066039db23b525338.png"
70
+
71
+ # Getting the content contains the base64 image again
72
+ note.content
73
+ # => "content1 <img src=\"data:image/png;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=\" />"
74
+ ```
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/carrierwave-base64_image_content. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the Carrierwave::Base64ImageContent project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/carrierwave-base64_image_content/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "carrierwave/base64_image_content"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'carrierwave/base64_image_content/version'
6
+
7
+ # rubocop:disable Metrics/BlockLength
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'carrierwave-base64_image_content'
10
+ spec.version = CarrierWave::Base64ImageContent::VERSION
11
+ spec.authors = ['Alexis Reigel']
12
+ spec.email = ['mail@koffeinfrei.org']
13
+
14
+ spec.summary = 'Upload content with base64 images to Carrierwave.'
15
+ spec.description = "Allows storing content with base64 images to a model
16
+ with Carrierwave file uploads. The base64 images are
17
+ extracted from the content and stored as physical
18
+ files using Carrierwave's configured storage. When
19
+ reading the content the files are converted back to
20
+ base64 images."
21
+ spec.homepage = 'https://github.com/panter/carrierwave-base64_image_content'
22
+
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`
25
+ .split("\x0")
26
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_runtime_dependency 'activerecord', '>= 4.2'
33
+ spec.add_runtime_dependency 'carrierwave', '>= 1.2'
34
+ spec.add_runtime_dependency 'rails', '>= 4.2'
35
+
36
+ spec.add_development_dependency 'bundler', '~> 1.16'
37
+ spec.add_development_dependency 'pg'
38
+ spec.add_development_dependency 'pry'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
41
+ spec.add_development_dependency 'rubocop'
42
+ spec.add_development_dependency 'rubocop-rspec'
43
+ spec.add_development_dependency 'simplecov'
44
+ end
45
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'active_record'
5
+ require 'carrierwave'
6
+ require 'carrierwave/orm/activerecord'
7
+
8
+ require 'carrierwave/base64_image_content/base64_string_io'
9
+ require 'carrierwave/base64_image_content/base64_file'
10
+ require 'carrierwave/base64_image_content/store'
11
+ require 'carrierwave/base64_image_content/version'
12
+
13
+ module CarrierWave
14
+ module Base64ImageContent
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module Base64ImageContent
5
+ class Base64File
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def filename_without_extension
11
+ File.basename(
12
+ @file.original_filename,
13
+ File.extname(@file.original_filename)
14
+ )
15
+ end
16
+
17
+ def data_url
18
+ base64 = Base64.strict_encode64(@file.read)
19
+ "data:#{@file.content_type};base64,#{base64}"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # initially taken from
4
+ # https://github.com/lebedev-yury/carrierwave-base64/blob/master/lib/carrierwave/base64/base64_string_io.rb
5
+ module CarrierWave
6
+ module Base64ImageContent
7
+ class Base64StringIO < StringIO
8
+ class ArgumentError < StandardError; end
9
+
10
+ attr_accessor :file_format, :file_name
11
+
12
+ # @param [String] encoded_file e.g.
13
+ # "data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABKdhH//2Q=="
14
+ def initialize(encoded_file)
15
+ description, encoded_bytes = encoded_file.split(',')
16
+
17
+ raise ArgumentError unless encoded_bytes
18
+ raise ArgumentError if encoded_bytes == '(null)'
19
+
20
+ @file_name = Digest::SHA256.hexdigest(encoded_file)
21
+ @file_format = get_file_format(description)
22
+ bytes = Base64.strict_decode64(encoded_bytes)
23
+
24
+ super(bytes)
25
+ end
26
+
27
+ def original_filename
28
+ File.basename("#{@file_name}.#{@file_format}")
29
+ end
30
+
31
+ private
32
+
33
+ def get_file_format(description)
34
+ regex = /([a-z0-9]+);base64\z/
35
+ regex.match(description).try(:[], 1)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module Base64ImageContent
5
+ module Store
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ attr_reader :base64_image_content_content_attribute
10
+ attr_reader :base64_image_content_images_attribute
11
+
12
+ def base64_image_content_store(content: :text_content, images: :images)
13
+ @base64_image_content_content_attribute = content
14
+ @base64_image_content_images_attribute = images
15
+ end
16
+ end
17
+
18
+ def content=(content)
19
+ content, new_image_files = extract_image_files_from_content(content)
20
+
21
+ remove_obsolete_files!(new_image_files)
22
+ add_new_files!(new_image_files)
23
+
24
+ # FIXME: https://github.com/carrierwaveuploader/carrierwave/issues/1990
25
+ self[self.class.base64_image_content_images_attribute] =
26
+ read_images_attribute.map(&:file).map(&:original_filename)
27
+
28
+ write_content_attribute(content)
29
+ end
30
+
31
+ def content
32
+ return unless read_content_attribute
33
+
34
+ content = read_content_attribute.dup
35
+
36
+ read_images_attribute.map do |image|
37
+ base64_file = Base64File.new(image.file)
38
+ content = replace_placeholder_by_data(content, base64_file)
39
+ end
40
+
41
+ content
42
+ end
43
+
44
+ private
45
+
46
+ def replace_placeholder_by_data(content, base64_file)
47
+ content.gsub(
48
+ /
49
+ (<img.*? src=['"])
50
+ (#{base64_file.filename_without_extension})
51
+ (['"].*?>)
52
+ /x,
53
+ "\\1#{base64_file.data_url}\\3"
54
+ )
55
+ end
56
+
57
+ def extract_image_files_from_content(content)
58
+ matches = content
59
+ .scan(%r{<img.*? src=['"](data:image/[^;]+;base64,[^'"]+)['"].*?>})
60
+ .flatten
61
+
62
+ image_files = matches.uniq.map do |match|
63
+ Base64StringIO.new(match).tap do |file|
64
+ content = content.sub(match, file.file_name)
65
+ end
66
+ end
67
+
68
+ [content, image_files]
69
+ end
70
+
71
+ def remove_obsolete_files!(new_image_files)
72
+ new_names = new_image_files.map(&:original_filename)
73
+
74
+ read_images_attribute
75
+ .reject { |image| new_names.include?(image.file.original_filename) }
76
+ .each do |image|
77
+ read_images_attribute.delete(image)
78
+ image.remove!
79
+ end
80
+ end
81
+
82
+ def add_new_files!(new_image_files)
83
+ old_file_names =
84
+ read_images_attribute.map(&:file).map(&:original_filename)
85
+
86
+ new_image_files
87
+ .reject { |image| old_file_names.include?(image.original_filename) }
88
+ .each do |image|
89
+ write_images_attribute(read_images_attribute + [image])
90
+ end
91
+ end
92
+
93
+ def read_content_attribute
94
+ self[self.class.base64_image_content_content_attribute]
95
+ end
96
+
97
+ def write_content_attribute(content)
98
+ self[self.class.base64_image_content_content_attribute] = content
99
+ end
100
+
101
+ def read_images_attribute
102
+ public_send(self.class.base64_image_content_images_attribute)
103
+ end
104
+
105
+ def write_images_attribute(images)
106
+ public_send(
107
+ "#{self.class.base64_image_content_images_attribute}=",
108
+ images
109
+ )
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CarrierWave
4
+ module Base64ImageContent
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,222 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-base64_image_content
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Reigel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: carrierwave
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
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'
167
+ description: |-
168
+ Allows storing content with base64 images to a model
169
+ with Carrierwave file uploads. The base64 images are
170
+ extracted from the content and stored as physical
171
+ files using Carrierwave's configured storage. When
172
+ reading the content the files are converted back to
173
+ base64 images.
174
+ email:
175
+ - mail@koffeinfrei.org
176
+ executables: []
177
+ extensions: []
178
+ extra_rdoc_files: []
179
+ files:
180
+ - ".gitignore"
181
+ - ".rspec"
182
+ - ".rubocop.yml"
183
+ - ".ruby-version"
184
+ - ".travis.yml"
185
+ - CODE_OF_CONDUCT.md
186
+ - Gemfile
187
+ - Gemfile.lock
188
+ - LICENSE
189
+ - README.md
190
+ - Rakefile
191
+ - bin/console
192
+ - bin/setup
193
+ - carrierwave-base64_image_content.gemspec
194
+ - lib/carrierwave/base64_image_content.rb
195
+ - lib/carrierwave/base64_image_content/base64_file.rb
196
+ - lib/carrierwave/base64_image_content/base64_string_io.rb
197
+ - lib/carrierwave/base64_image_content/store.rb
198
+ - lib/carrierwave/base64_image_content/version.rb
199
+ homepage: https://github.com/panter/carrierwave-base64_image_content
200
+ licenses: []
201
+ metadata: {}
202
+ post_install_message:
203
+ rdoc_options: []
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubyforge_project:
218
+ rubygems_version: 2.7.6
219
+ signing_key:
220
+ specification_version: 4
221
+ summary: Upload content with base64 images to Carrierwave.
222
+ test_files: []