active_storage_db 0.1.2 → 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: 5badec24c31c5731ad428313fb458e6282e3e8006edad239581bc2fd73d3db72
4
- data.tar.gz: df260e3805a1d3ca52d8a105faa1e3bee0adbcc4545f1e61eea44a24a2600abe
3
+ metadata.gz: 733afb3767f4174510cc6e59a72a5c6e137d17479fc6ec733a977ff81f80fbaf
4
+ data.tar.gz: dd7ffb266b99e6fbb2a0537b3d198a3e20200bc01dbdb43fe2407cd01161793d
5
5
  SHA512:
6
- metadata.gz: 776d212df5f29c405074ff0093fb1091cbf0eca5ae8bbce5c639eb2c3fb3346d80860e980e7afe25b4dfb36e37128fe0ed8a08179cd1d573b03f86770165386e
7
- data.tar.gz: 5cd28f3391c22d923b14fc270a334a97bfe209ca18c85d7218f43b79f7480187bf330a6459b32a1be6b1ab3244d494058e01e8200dd562ab36819e8e63f244ca
6
+ metadata.gz: 4ef2822294d193dd86b6bf865602ec14f13e5868a418e2ad3685d9eaade829b302659634c4effebf495f63c63a96252da50a170ad0b2e3cb831243602e0cfdcd
7
+ data.tar.gz: 8b4e73f58bfcc0f3a840812757069be9ef9859585a3a9158721ede94885d014a2b507917fdc8e5a5da7e3f17c0ef90df7146958205aa88a07243471f3b44d118
data/README.md CHANGED
@@ -1,33 +1,51 @@
1
- # Active Storage DB [![Gem Version](https://badge.fury.io/rb/active_storage_db.svg)](https://badge.fury.io/rb/active_storage_db) [![CircleCI](https://circleci.com/gh/blocknotes/active_storage_db.svg?style=svg)](https://circleci.com/gh/blocknotes/active_storage_db)
1
+ # Active Storage DB
2
+
3
+ [![gem version](https://badge.fury.io/rb/active_storage_db.svg)](https://badge.fury.io/rb/active_storage_db)
4
+ [![linters](https://github.com/blocknotes/active_storage_db/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/linters.yml)
5
+ [![specs Postgres](https://github.com/blocknotes/active_storage_db/actions/workflows/postgres.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/postgres.yml)
6
+ [![specs MySQL](https://github.com/blocknotes/active_storage_db/actions/workflows/mysql.yml/badge.svg)](https://github.com/blocknotes/active_storage_db/actions/workflows/mysql.yml)
7
+
2
8
  An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database.
3
9
 
4
10
  Main features:
11
+ - supports Rails 6.0, 6.1 and 7.0;
5
12
  - all service methods implemented;
6
- - data is saved using a binary field (or blob);
7
- - RSpec tests.
13
+ - attachment data stored in a binary field (or blob).
14
+
15
+ Useful also with platforms like Heroku (due to their ephemeral file system).
8
16
 
9
17
  ## Installation
18
+
10
19
  - Setup Active Storage in your Rails application
11
- - Add this line to your Gemfile: `gem 'active_storage_db'`
12
- - And execute: `bundle`
13
- - Install gem migrations: `bin/rails active_storage_db:install:migrations`
14
- - And execute: `bin/rails db:migrate`
20
+ - Add to your Gemfile `gem 'active_storage_db'` (and execute: `bundle`)
21
+ - Install the gem migrations: `bin/rails active_storage_db:install:migrations` (and execute: `bin/rails db:migrate`)
15
22
  - Add to your `config/routes.rb`: `mount ActiveStorageDB::Engine => '/active_storage_db'`
16
23
  - Change Active Storage service in *config/environments/development.rb* to: `config.active_storage.service = :db`
17
24
  - Add to *config/storage.yml*:
25
+
18
26
  ```
19
27
  db:
20
28
  service: DB
21
29
  ```
22
30
 
31
+ ## Misc
32
+
33
+ Some rake tasks are available:
34
+
35
+ - `asdb:list`: list the stored attachments
36
+ - `asdb:get`: download an attachment (ex. `bin/rails "asdb:get[ruby-logo.png,/tmp]"`)
37
+
23
38
  ## Do you like it? Star it!
39
+
24
40
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
25
41
 
26
42
  Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
27
43
 
28
44
  ## Contributors
45
+
29
46
  - [Mattia Roccoberton](https://blocknot.es/): author
30
47
  - Inspired by [activestorage-database-service](https://github.com/TitovDigital/activestorage-database-service) project
31
48
 
32
49
  ## License
50
+
33
51
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -16,19 +16,23 @@ RDoc::Task.new(:rdoc) do |rdoc|
16
16
  rdoc.rdoc_files.include('lib/**/*.rb')
17
17
  end
18
18
 
19
- APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
19
+ dummy_app = ENV['RAILS_7'] ? 'dummy7' : 'dummy'
20
+ APP_RAKEFILE = File.expand_path("spec/#{dummy_app}/Rakefile", __dir__)
20
21
  load 'rails/tasks/engine.rake'
21
22
 
22
23
  load 'rails/tasks/statistics.rake'
23
24
 
24
25
  require 'bundler/gem_tasks'
25
26
 
26
- require 'rake/testtask'
27
+ begin
28
+ require 'rspec/core/rake_task'
27
29
 
28
- Rake::TestTask.new(:test) do |t|
29
- t.libs << 'spec'
30
- t.pattern = 'spec/**/*_spec.rb'
31
- t.verbose = false
32
- end
30
+ RSpec::Core::RakeTask.new(:spec) do |t|
31
+ # t.ruby_opts = %w[-w]
32
+ t.rspec_opts = ['--color', '--format documentation']
33
+ end
33
34
 
34
- task default: :test
35
+ task default: :spec
36
+ rescue LoadError
37
+ puts '! LoadError: no RSpec available'
38
+ end
@@ -83,7 +83,7 @@ module ActiveStorage
83
83
  end
84
84
  end
85
85
 
86
- def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
86
+ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
87
87
  instrument :url, key: key do |payload|
88
88
  verified_token_with_expiration = ActiveStorage.verifier.generate(
89
89
  {
@@ -95,10 +95,7 @@ module ActiveStorage
95
95
  expires_in: expires_in,
96
96
  purpose: :blob_token
97
97
  )
98
- generated_url = url_helpers.update_service_url(
99
- verified_token_with_expiration,
100
- host: current_host
101
- )
98
+ generated_url = url_helpers.update_service_url(verified_token_with_expiration, host: current_host)
102
99
  payload[:url] = generated_url
103
100
 
104
101
  generated_url
@@ -112,7 +109,14 @@ module ActiveStorage
112
109
  private
113
110
 
114
111
  def current_host
115
- ActiveStorage::Current.host
112
+ if ActiveStorage::Current.respond_to? :url_options
113
+ opts = ActiveStorage::Current.url_options || {}
114
+ url = "#{opts[:protocol]}#{opts[:host]}"
115
+ url += ":#{opts[:port]}" if opts[:port]
116
+ url || ''
117
+ else
118
+ ActiveStorage::Current.host
119
+ end
116
120
  end
117
121
 
118
122
  def ensure_integrity_of(key, checksum)
@@ -128,7 +132,7 @@ module ActiveStorage
128
132
  ::ActiveStorageDB::File.select('OCTET_LENGTH(data) AS size').find_by(ref: key)&.size ||
129
133
  raise(ActiveStorage::FileNotFoundError)
130
134
  (size / @chunk_size.to_f).ceil.times.each do |i|
131
- range = (i * @chunk_size..(i + 1) * @chunk_size - 1)
135
+ range = (i * @chunk_size..((i + 1) * @chunk_size) - 1)
132
136
  yield download_chunk(key, range)
133
137
  end
134
138
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageDB
4
- VERSION = '0.1.2'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -1,5 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # desc 'ActiveStorageDB sync'
4
- # task :active_storage_db do
5
- # end
3
+ namespace :asdb do
4
+ desc 'ActiveStorageDB: list attachments'
5
+ task list: [:environment] do |_t, _args|
6
+ ::ActiveStorage::Blob.order(:filename).pluck(:byte_size, :created_at, :filename).each do |size, dt, filename|
7
+ size_k = (size / 1024).to_s.rjust(7)
8
+ date = dt.strftime('%Y-%m-%d %H:%M')
9
+ puts "#{size_k}K #{date} #{filename}"
10
+ end
11
+ end
12
+
13
+ desc 'ActiveStorageDB: download attachment'
14
+ task :get, [:src, :dst] => [:environment] do |_t, args|
15
+ src = args[:src]&.strip
16
+ dst = args[:dst]&.strip
17
+ abort('Required arguments: source file, destination file') if src.blank? || dst.blank?
18
+
19
+ dst = "#{dst}/#{src}" if Dir.exist?(dst)
20
+ dir = File.dirname(dst)
21
+ abort("Can't write on: #{dir}") unless File.writable?(dir)
22
+
23
+ blob = ::ActiveStorage::Blob.order(created_at: :desc).find_by(filename: src)
24
+ abort('Source file not found') unless blob
25
+
26
+ ret = File.binwrite(dst, blob.download)
27
+ puts "#{ret} bytes written"
28
+ rescue StandardError => e
29
+ puts e
30
+ end
31
+ end
metadata CHANGED
@@ -1,71 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-05 00:00:00.000000000 Z
11
+ date: 2022-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '6.0'
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
26
  version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '6.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '6.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: capybara
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '3.33'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '3.33'
55
- - !ruby/object:Gem::Dependency
56
- name: database_cleaner-active_record
42
+ name: appraisal
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '1.8'
47
+ version: '2.4'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '1.8'
54
+ version: '2.4'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: factory_bot_rails
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,135 +66,9 @@ dependencies:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
68
  version: '6.1'
83
- - !ruby/object:Gem::Dependency
84
- name: mysql2
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '0.5'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '0.5'
97
- - !ruby/object:Gem::Dependency
98
- name: pg
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '1.2'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '1.2'
111
- - !ruby/object:Gem::Dependency
112
- name: pry
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '0.13'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '0.13'
125
- - !ruby/object:Gem::Dependency
126
- name: rspec_junit_formatter
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '0.4'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '0.4'
139
- - !ruby/object:Gem::Dependency
140
- name: rspec-rails
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '4.0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '4.0'
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.89'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - "~>"
165
- - !ruby/object:Gem::Version
166
- version: '0.89'
167
- - !ruby/object:Gem::Dependency
168
- name: rubocop-rspec
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - "~>"
172
- - !ruby/object:Gem::Version
173
- version: '1.42'
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - "~>"
179
- - !ruby/object:Gem::Version
180
- version: '1.42'
181
- - !ruby/object:Gem::Dependency
182
- name: selenium-webdriver
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: '3.142'
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: '3.142'
195
- - !ruby/object:Gem::Dependency
196
- name: simplecov
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - "~>"
200
- - !ruby/object:Gem::Version
201
- version: '0.18'
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - "~>"
207
- - !ruby/object:Gem::Version
208
- version: '0.18'
209
69
  description: An ActiveStorage service plugin to store files in database.
210
70
  email:
211
- - mattiaroccoberton@nebulab.it
71
+ - mat@blocknot.es
212
72
  executables: []
213
73
  extensions: []
214
74
  extra_rdoc_files: []
@@ -227,10 +87,13 @@ files:
227
87
  - lib/active_storage_db/engine.rb
228
88
  - lib/active_storage_db/version.rb
229
89
  - lib/tasks/active_storage_db_tasks.rake
230
- homepage: https://blocknot.es
90
+ homepage: https://github.com/blocknotes/active_storage_db
231
91
  licenses:
232
92
  - MIT
233
- metadata: {}
93
+ metadata:
94
+ homepage_uri: https://github.com/blocknotes/active_storage_db
95
+ source_code_uri: https://github.com/blocknotes/active_storage_db
96
+ rubygems_mfa_required: 'true'
234
97
  post_install_message:
235
98
  rdoc_options: []
236
99
  require_paths:
@@ -239,14 +102,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
102
  requirements:
240
103
  - - ">="
241
104
  - !ruby/object:Gem::Version
242
- version: '0'
105
+ version: 2.6.0
243
106
  required_rubygems_version: !ruby/object:Gem::Requirement
244
107
  requirements:
245
108
  - - ">="
246
109
  - !ruby/object:Gem::Version
247
110
  version: '0'
248
111
  requirements: []
249
- rubygems_version: 3.0.3
112
+ rubygems_version: 3.1.6
250
113
  signing_key:
251
114
  specification_version: 4
252
115
  summary: ActiveStorage DB Service