carrierwave-base64_image_content 0.1.0 → 1.0.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
  SHA256:
3
- metadata.gz: 792d4946b4a48c18628dcde7e067e7da5f42538f53c1e144eeed1c5d0e6552e2
4
- data.tar.gz: 88bf0825547250a6de26a187130c4ec3108089c6a0f70e7e14a4b9d07b61331e
3
+ metadata.gz: 7329dc9fc5e24b1fc41ce692f773eb4e8395134b5aec9f00beed563ebadfff52
4
+ data.tar.gz: 8ea03a38e8472e3b57d8d3aeb7854d0fe73795b1af48d9b13f68ee907e08d33e
5
5
  SHA512:
6
- metadata.gz: f143fc46ff04cecb065d5868c20b6269fbeeb47878596a132387e9766e78635847848f272b371f74ceacd48bd1fb43f5e5fc139015cd7894f738f93007d1620a
7
- data.tar.gz: f973ee25fc29c9242614abf6c0f006954c1b2038afce0e06fab9bd9e2d266c4d607d0875574af304ceba16a5b873a494cf5cd827cb43c3b5a800b70430253ccc
6
+ metadata.gz: 222b615740c10a8e0024dc97ba4797e2064936171dc52638027106587c18e58dfff9c18df86c087a97587a62d8f7de86cf63b2626436fbbabdf4fe2349d25ac1
7
+ data.tar.gz: 44f4b66d3d1810ec27511501c52e97aab85cdffafb24d0e0af158dfc17d70d2b283528df21116ef2558988d82c19580e360f9b8f3d01420d91a325cac8b08e72
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- carrierwave-base64_image_content (0.1.0)
4
+ carrierwave-base64_image_content (1.0.0)
5
5
  activerecord (>= 4.2)
6
6
  carrierwave (>= 1.2)
7
7
  rails (>= 4.2)
@@ -58,7 +58,7 @@ GEM
58
58
  activesupport (>= 4.0.0)
59
59
  mime-types (>= 1.16)
60
60
  coderay (1.1.2)
61
- concurrent-ruby (1.1.3)
61
+ concurrent-ruby (1.1.4)
62
62
  crass (1.0.4)
63
63
  diff-lcs (1.3)
64
64
  docile (1.3.1)
data/README.md CHANGED
@@ -31,20 +31,20 @@ The prerequisite for this gem is a model with an existing [multiple file
31
31
  uploads
32
32
  field](https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads).
33
33
 
34
- 1. Include the module `CarrierWave::Base64ImageContent::Store`
35
- 1. Call `base64_image_content_store` with the following two parameters:
34
+ 1. Include the module `CarrierWave::Base64ImageContent`
35
+ 1. Call `base64_image_content` with the following two parameters:
36
36
  * The model's content attribute, which is the actual database field that
37
37
  stores the content
38
- * The images attribute, which uses CarrierWave to store the files
38
+ * The model's images attribute, which uses CarrierWave to store the files
39
39
 
40
40
 
41
41
  ```ruby
42
42
  class Note < ActiveRecord::Base
43
- include CarrierWave::Base64ImageContent::Store
43
+ include CarrierWave::Base64ImageContent
44
44
 
45
45
  mount_uploaders :images, ImageUploader
46
46
 
47
- base64_image_content_store content: :text_content, images: :images
47
+ base64_image_content content: :text_content, images: :images
48
48
  end
49
49
  ```
50
50
 
@@ -7,10 +7,114 @@ require 'carrierwave/orm/activerecord'
7
7
 
8
8
  require 'carrierwave/base64_image_content/base64_string_io'
9
9
  require 'carrierwave/base64_image_content/base64_file'
10
- require 'carrierwave/base64_image_content/store'
11
10
  require 'carrierwave/base64_image_content/version'
12
11
 
13
12
  module CarrierWave
14
13
  module Base64ImageContent
14
+ extend ActiveSupport::Concern
15
+
16
+ module ClassMethods
17
+ attr_reader :base64_image_content_content_attribute
18
+ attr_reader :base64_image_content_images_attribute
19
+
20
+ def base64_image_content(content: :text_content, images: :images)
21
+ @base64_image_content_content_attribute = content
22
+ @base64_image_content_images_attribute = images
23
+ end
24
+ end
25
+
26
+ def content=(content)
27
+ content, new_image_files = extract_image_files_from_content(content)
28
+
29
+ remove_obsolete_files!(new_image_files)
30
+ add_new_files!(new_image_files)
31
+
32
+ # FIXME: https://github.com/carrierwaveuploader/carrierwave/issues/1990
33
+ self[self.class.base64_image_content_images_attribute] =
34
+ read_images_attribute.map(&:file).map(&:original_filename)
35
+
36
+ write_content_attribute(content)
37
+ end
38
+
39
+ def content
40
+ return unless read_content_attribute
41
+
42
+ content = read_content_attribute.dup
43
+
44
+ read_images_attribute.map do |image|
45
+ base64_file = Base64File.new(image.file)
46
+ content = replace_placeholder_by_data(content, base64_file)
47
+ end
48
+
49
+ content
50
+ end
51
+
52
+ private
53
+
54
+ def replace_placeholder_by_data(content, base64_file)
55
+ content.gsub(
56
+ /
57
+ (<img.*? src=['"])
58
+ (#{base64_file.filename_without_extension})
59
+ (['"].*?>)
60
+ /x,
61
+ "\\1#{base64_file.data_url}\\3"
62
+ )
63
+ end
64
+
65
+ def extract_image_files_from_content(content)
66
+ matches = content
67
+ .scan(%r{<img.*? src=['"](data:image/[^;]+;base64,[^'"]+)['"].*?>})
68
+ .flatten
69
+
70
+ image_files = matches.uniq.map do |match|
71
+ Base64StringIO.new(match).tap do |file|
72
+ content = content.sub(match, file.file_name)
73
+ end
74
+ end
75
+
76
+ [content, image_files]
77
+ end
78
+
79
+ def remove_obsolete_files!(new_image_files)
80
+ new_names = new_image_files.map(&:original_filename)
81
+
82
+ read_images_attribute
83
+ .reject { |image| new_names.include?(image.file.original_filename) }
84
+ .each do |image|
85
+ read_images_attribute.delete(image)
86
+ image.remove!
87
+ end
88
+ end
89
+
90
+ def add_new_files!(new_image_files)
91
+ old_file_names =
92
+ read_images_attribute.map(&:file).map(&:original_filename)
93
+
94
+ new_image_files
95
+ .reject { |image| old_file_names.include?(image.original_filename) }
96
+ .each do |image|
97
+ write_images_attribute(read_images_attribute + [image])
98
+ end
99
+ end
100
+
101
+ def read_content_attribute
102
+ self[self.class.base64_image_content_content_attribute]
103
+ end
104
+
105
+ def write_content_attribute(content)
106
+ self[self.class.base64_image_content_content_attribute] = content
107
+ end
108
+
109
+ def read_images_attribute
110
+ public_send(self.class.base64_image_content_images_attribute)
111
+ end
112
+
113
+ def write_images_attribute(images)
114
+ public_send(
115
+ "#{self.class.base64_image_content_images_attribute}=",
116
+ images
117
+ )
118
+ end
15
119
  end
16
120
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CarrierWave
4
4
  module Base64ImageContent
5
- VERSION = '0.1.0'
5
+ VERSION = '1.0.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-base64_image_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Reigel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-15 00:00:00.000000000 Z
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -194,7 +194,6 @@ files:
194
194
  - lib/carrierwave/base64_image_content.rb
195
195
  - lib/carrierwave/base64_image_content/base64_file.rb
196
196
  - lib/carrierwave/base64_image_content/base64_string_io.rb
197
- - lib/carrierwave/base64_image_content/store.rb
198
197
  - lib/carrierwave/base64_image_content/version.rb
199
198
  homepage: https://github.com/panter/carrierwave-base64_image_content
200
199
  licenses: []
@@ -1,113 +0,0 @@
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