activestorage-delayed 0.2.0.pre.pre → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02b3779f97c4d12be774399968c3ef1b3e0c006bf4c39da9ecf5fd583bdeadf4
4
- data.tar.gz: c9e4338a68b08f564d2b6e8c0c6f608d21dc70feea8688afbe22f1566b88a7ca
3
+ metadata.gz: 23b95115e89560a1c7e3a880f0b89a1d7aaf99fe4b6e54c1b207ce0d435dfda2
4
+ data.tar.gz: 727dd8e29453607c884e4ddf126fd1f41da790bb8c1db89ae30cedc506b02900
5
5
  SHA512:
6
- metadata.gz: 88ebc7439257498bd71450e9958856102fb0a948bc1c3b3d0b26fabc43aae6094599187f3336dac6a125f8c3be320a5648f66c57cc9aa299d2ce540dcf4ec77e
7
- data.tar.gz: cea2b0e318299d762121f285a270e72d11457b742537b6515fb95232de48222c52db8ea7d2af0b5281402d22b905de6fc869333de03daba393519214c88c3743
6
+ metadata.gz: 976b9b1c03bab65f651fb81d2915aeb9561e380fbd35b26069c3263d4f443f029cc4fd22bee015a4e946eb20487b68228a5d66d175978c556f0a76e6979b59b8
7
+ data.tar.gz: 29d8ae5308daa56f5845e4deeb70a709cce48a9f5c48b2759ad4473cea0041c6a84a0f50b50657235177d5d10dd6736696c305a304d38474dc2cca05d5a6de5b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activestorage-delayed (0.2.0.pre.pre)
4
+ activestorage-delayed (0.2.0)
5
5
  activestorage
6
6
  rails
7
7
 
data/README.md CHANGED
@@ -39,7 +39,7 @@ Note: This gem assumes that the app has already configured activestorage.
39
39
  class User < ApplicationRecord
40
40
  include ActivestorageDelayed::DelayedConcern
41
41
 
42
- has_one_attached :photo, required: true, use_filename: true
42
+ has_one_attached :photo, required: true, use_filename: true, variant_info: { resize_to_fit: [400, 400], convert: 'jpg' }
43
43
  delayed_attach :photo
44
44
 
45
45
  has_many_attached :certificates
@@ -50,6 +50,7 @@ end
50
50
  ### `delayed_attach` accepts an optional hash with the following options:
51
51
  - `required`: If set to `true`, the `photo` or the `photo_tmp` will be required before saving.
52
52
  - `use_filename`: If set to `true`, the image filename will be used as the name of uploaded file instead of the hash-key used by `activestorage`
53
+ - `variant_info`: (Hash) Variant information to be performed before uploading the file.
53
54
 
54
55
  ### Examples to upload files in background
55
56
  - Upload a single file
@@ -90,7 +91,7 @@ end
90
91
  User.first.update(certificates_tmp: { clean_before: true, files: [File.open('my_file.png')] })
91
92
  ```
92
93
 
93
- - Upload files with custom names (requires `use_filename: true`)
94
+ - Upload files with custom names (requires `use_filename: true`): `<attr_name>_filename`
94
95
  ```ruby
95
96
  class User < ApplicationRecord
96
97
  def photo_filename(filename)
@@ -101,42 +102,27 @@ end
101
102
  When `<attr_name>_filename` is defined, then it is called to fetch the uploaded file name.
102
103
  Note: Check [this](https://gist.github.com/owen2345/33730a452d73b6b292326bb602b0ee6b) if you want to rename an already uploaded file (remote file)
103
104
 
104
- - Capture event when file upload has failed
105
+ - Capture event when file upload has failed: `<attr_name>_error_upload`
105
106
  ```ruby
106
107
  class User < ApplicationRecord
107
- def ast_delayed_on_error(attr_name, error)
108
- puts "Failed #{attr_name} with #{error}"
108
+ def photo_error_upload(error)
109
+ puts "Failed with #{error}"
109
110
  end
110
111
  end
111
112
  ```
112
113
 
113
- - Capture event when file has been uploaded
114
+ - Capture event when file has been uploaded: `<attr_name>_after_upload`
114
115
  ```ruby
115
116
  class User < ApplicationRecord
116
- after_save_commit do
117
- puts 'Photo has been uploaded' if photo.blob.present?
118
- puts 'No pending enqueued photo uploads' unless photo_delayed_uploads.any?
119
- end
120
-
121
- before_save do
122
- puts "current assigned tmp photo: #{photo_tmp.inspect}"
117
+ def photo_after_upload
118
+ puts "Photo has been uploaded"
123
119
  end
124
120
  end
125
121
  ```
122
+
123
+ Note:
126
124
  `<attr_name>_delayed_uploads` is a `has_many` association that contains the list of scheduled uploads for the corresponding attribute.
127
125
 
128
-
129
- ## Preprocessing original files before uploading (Rails 7+)
130
- ```ruby
131
- class User < ApplicationRecord
132
- has_one_attached :photo do |attachable|
133
- attachable.variant :default, strip: true, quality: 70, resize_to_fill: [200, 200]
134
- end
135
- end
136
- ```
137
- `:default` variant will be used to pre-preprocess the original file before uploading (if defined). By this way you have your desired image size and quality as the original image.
138
-
139
-
140
126
 
141
127
  ## Contributing
142
128
  Bug reports and pull requests are welcome on https://github.com/owen2345/activestorage-delayed. 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.
@@ -18,20 +18,27 @@ module ActivestorageDelayed
18
18
 
19
19
  private
20
20
 
21
- # TODO: check the ability to delete io with save or upload method
22
- # file_data['io'].close
23
- def upload_photos # rubocop:disable Metrics/AbcSize
24
- tmp_files_data.each do |file_data|
25
- model.send(attr_name).attach(file_data.transform_keys(&:to_sym))
26
- end
21
+ def upload_photos
22
+ tmp_files_data.each(&method(:upload_photo))
27
23
  model.send("#{attr_name}_after_upload")
28
24
  true
29
25
  rescue => e # rubocop:disable Style/RescueStandardError
30
- Rails.logger.error("********* #{self.class.name} -> Failed uploading files: #{e.message}. #{e.backtrace[0..20]}")
31
- model.send("#{attr_name}_error_upload", e)
26
+ print_failure(e)
32
27
  false
33
28
  end
34
29
 
30
+ def upload_photo(file_data)
31
+ parse_file_io(file_data) do |io|
32
+ file_data['io'] = io
33
+ model.send(attr_name).attach(file_data.transform_keys(&:to_sym))
34
+ end
35
+ end
36
+
37
+ def print_failure(error)
38
+ Rails.logger.error("***#{self.class.name} -> Failed uploading files: #{error.message}. #{error.backtrace[0..20]}")
39
+ model.send("#{attr_name}_error_upload", error)
40
+ end
41
+
35
42
  def save_changes
36
43
  model.save!
37
44
  delayed_upload.destroy!
@@ -42,7 +49,6 @@ module ActivestorageDelayed
42
49
  @tmp_files_data ||= begin
43
50
  files = JSON.parse(delayed_upload.files || '[]')
44
51
  files.each do |file_data|
45
- file_data['io'] = base64_to_file(file_data)
46
52
  if attr_settings[:use_filename]
47
53
  file_data['key'] = filename_for(file_data['filename'])
48
54
  file_data['filename'] = file_data['key']
@@ -51,9 +57,22 @@ module ActivestorageDelayed
51
57
  end
52
58
  end
53
59
 
54
- def base64_to_file(file_data)
55
- io = StringIO.new(Base64.decode64(file_data['io']))
56
- apply_variant(io, attr_settings[:variant_info]) { |io2| return io2 }
60
+ def parse_file_io(file_data, &block)
61
+ tempfile = Tempfile.new(file_data['filename'], binmode: true)
62
+ tempfile.write Base64.decode64(file_data['io'])
63
+ tempfile.rewind
64
+ transform_variation(tempfile, attr_settings[:variant_info]) do |io|
65
+ block.call(io)
66
+ tempfile.close
67
+ end
68
+ end
69
+
70
+ # @param io [StringIO, File]
71
+ # @param variant_info [Hash, Nil] ActiveStorage variant info. Sample: { resize_to_fit: [400, 400], convert: 'jpg' }
72
+ def transform_variation(io, variant_info, &block)
73
+ return block.call(io) unless variant_info
74
+
75
+ ActiveStorage::Variation.wrap(variant_info).transform(io, &block)
57
76
  end
58
77
 
59
78
  def model
@@ -84,13 +103,5 @@ module ActivestorageDelayed
84
103
  def attr_settings
85
104
  model.class.instance_variable_get(:@ast_delayed_settings)[attr_name]
86
105
  end
87
-
88
- # @param io [StringIO, File]
89
- # @param variant_info [Hash, Nil] ActiveStorage variant info. Sample: { resize_to_fit: [400, 400], convert: 'jpg' }
90
- def apply_variant(io, variant_info, &block)
91
- return block.call(io) unless variant_info
92
-
93
- ActiveStorage::Variation.wrap(variant_info).transform(io, &block)
94
- end
95
106
  end
96
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivestorageDelayed
4
- VERSION = '0.2.0-pre'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage-delayed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre.pre
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen Peredo Diaz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-05 00:00:00.000000000 Z
11
+ date: 2022-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage
@@ -88,9 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - ">"
91
+ - - ">="
92
92
  - !ruby/object:Gem::Version
93
- version: 1.3.1
93
+ version: '0'
94
94
  requirements: []
95
95
  rubygems_version: 3.1.2
96
96
  signing_key: