Skip to content

Commit 12a41c4

Browse files
author
Mattia Roccoberton
committed
Version 0.1.0
First version of the component base on ActiveStorage DiskService. Main points: - Works with MySQL and PostgreSQL - All service methods implemented - RSpec tests
1 parent f4072d7 commit 12a41c4

101 files changed

Lines changed: 872 additions & 206 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
.bundle/
22
log/*.log
33
pkg/
4-
test/dummy/db/*.sqlite3
5-
test/dummy/db/*.sqlite3-journal
6-
test/dummy/db/*.sqlite3-*
7-
test/dummy/log/*.log
8-
test/dummy/storage/
9-
test/dummy/tmp/
4+
5+
/spec/dummy/db/*.sqlite3
6+
/spec/dummy/db/*.sqlite3-journal
7+
/spec/dummy/db/*.sqlite3-*
8+
/spec/dummy/log/*
9+
/spec/dummy/storage/
10+
/spec/dummy/tmp/
11+
12+
/.rubocop-*
13+
/coverage/
14+
/Gemfile.lock

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--color
2+
--format documentation
3+
--require rails_helper

.rubocop.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
inherit_from:
2+
- https://relaxed.ruby.style/rubocop.yml
3+
4+
AllCops:
5+
TargetRubyVersion: 2.6
6+
Exclude:
7+
- bin/*
8+
- db/schema.rb
9+
- spec/dummy/**/*
10+
- vendor/**/*
11+
12+
Gemspec/RequiredRubyVersion:
13+
Enabled: false
14+
15+
Layout/LineLength:
16+
Enabled: true
17+
Max: 120

Gemfile

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
35

4-
# Declare your gem's dependencies in active_storage_db.gemspec.
5-
# Bundler will treat runtime dependencies like base dependencies, and
6-
# development dependencies will be added by default to the :development group.
76
gemspec
8-
9-
# Declare any dependencies that are still in development here instead of in
10-
# your gemspec. These might include edge Rails or gems from your path or
11-
# Git. Remember to move these dependencies to your gemspec before releasing
12-
# your gem to rubygems.org.
13-
14-
# To use a debugger
15-
# gem 'byebug', group: [:development, :test]

README.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
# ActiveStorageDB
2-
Short description and motivation.
1+
# Active Storage DB
2+
An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database.
33

4-
## Usage
5-
How to use my plugin.
4+
Main features:
5+
- all service methods implemented;
6+
- data is saved using a binary field (or blob);
7+
- RSpec tests.
68

79
## Installation
8-
Add this line to your application's Gemfile:
9-
10-
```ruby
11-
gem 'active_storage_db'
10+
- 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`
15+
- Add to your `config/routes.rb`: `mount ActiveStorageDB::Engine => '/active_storage_db'`
16+
- Change Active Storage service in *config/environments/development.rb* to: `config.active_storage.service = :db`
17+
- Add to *config/storage.yml*:
1218
```
13-
14-
And then execute:
15-
```bash
16-
$ bundle
19+
db:
20+
service: DB
1721
```
1822

19-
Or install it yourself as:
20-
```bash
21-
$ gem install active_storage_db
22-
```
23+
## Do you like it? Star it!
24+
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
2325

24-
## Contributing
25-
Contribution directions go here.
26+
## Contributors
27+
- [Mattia Roccoberton](https://blocknot.es/): author
28+
- Inspired by [activestorage-database-service](https://github.com/TitovDigital/activestorage-database-service) project
2629

2730
## License
2831
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
begin
24
require 'bundler/setup'
35
rescue LoadError
@@ -14,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
1416
rdoc.rdoc_files.include('lib/**/*.rb')
1517
end
1618

17-
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
19+
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
1820
load 'rails/tasks/engine.rake'
1921

2022
load 'rails/tasks/statistics.rake'
@@ -24,8 +26,8 @@ require 'bundler/gem_tasks'
2426
require 'rake/testtask'
2527

2628
Rake::TestTask.new(:test) do |t|
27-
t.libs << 'test'
28-
t.pattern = 'test/**/*_test.rb'
29+
t.libs << 'spec'
30+
t.pattern = 'spec/**/*_spec.rb'
2931
t.verbose = false
3032
end
3133

active_storage_db.gemspec

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
$:.push File.expand_path("lib", __dir__)
1+
# frozen_string_literal: true
2+
3+
$:.push File.expand_path('lib', __dir__)
24

35
# Maintain your gem's version:
4-
require "active_storage_db/version"
6+
require 'active_storage_db/version'
57

68
# Describe your gem and declare its dependencies:
79
Gem::Specification.new do |spec|
8-
spec.name = "active_storage_db"
10+
spec.name = 'active_storage_db'
911
spec.version = ActiveStorageDB::VERSION
10-
spec.authors = ["Mattia Roccoberton"]
11-
spec.email = ["mattiaroccoberton@nebulab.it"]
12-
spec.homepage = "TODO"
13-
spec.summary = "TODO: Summary of ActiveStorageDB."
14-
spec.description = "TODO: Description of ActiveStorageDB."
15-
spec.license = "MIT"
12+
spec.authors = ['Mattia Roccoberton']
13+
spec.email = ['mattiaroccoberton@nebulab.it']
14+
spec.homepage = 'https://blocknot.es'
15+
spec.summary = 'ActiveStorage DB Service'
16+
spec.description = 'An ActiveStorage service plugin to store files in database.'
17+
spec.license = 'MIT'
1618

1719
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
1820
# to allow pushing to a single host or delete this section to allow pushing to any host.
1921
if spec.respond_to?(:metadata)
20-
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
2123
else
22-
raise "RubyGems 2.0 or newer is required to protect against " \
23-
"public gem pushes."
24+
raise 'RubyGems 2.0 or newer is required to protect against ' \
25+
'public gem pushes.'
2426
end
2527

26-
spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
28+
spec.files = Dir["{app,config,db,lib}/**/*", 'MIT-LICENSE', 'Rakefile', 'README.md']
2729

28-
spec.add_dependency "rails", "~> 6.0.3", ">= 6.0.3.2"
30+
spec.add_dependency 'activestorage', '~> 6.0.0'
31+
spec.add_dependency 'rails', '~> 6.0.0'
2932

30-
spec.add_development_dependency "sqlite3"
33+
spec.add_development_dependency 'capybara', '~> 3.33.0'
34+
spec.add_development_dependency 'database_cleaner-active_record', '~> 1.8.0'
35+
spec.add_development_dependency 'factory_bot_rails', '~> 6.1.0'
36+
spec.add_development_dependency 'mysql2', '~> 0.5.3'
37+
spec.add_development_dependency 'pg', '~> 1.2.3'
38+
spec.add_development_dependency 'pry', '~> 0.13.1'
39+
spec.add_development_dependency 'rspec-rails', '~> 4.0.1'
40+
spec.add_development_dependency 'rubocop', '~> 0.89.0'
41+
spec.add_development_dependency 'selenium-webdriver', '~> 3.142.7'
42+
spec.add_development_dependency 'simplecov', '~> 0.18.5'
3143
end

app/assets/config/active_storage_db_manifest.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/controllers/active_storage_db/application_controller.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorageDB
4+
class FilesController < ActiveStorage::BaseController
5+
skip_forgery_protection
6+
7+
def show
8+
if (key = decode_verified_key)
9+
serve_file(key[:key], content_type: key[:content_type], disposition: key[:disposition])
10+
else
11+
head :not_found
12+
end
13+
rescue ActiveStorage::FileNotFoundError
14+
head :not_found
15+
end
16+
17+
def update
18+
if (token = decode_verified_token)
19+
if acceptable_content?(token)
20+
db_service.upload(token[:key], request.body, checksum: token[:checksum])
21+
else
22+
head :unprocessable_entity
23+
end
24+
else
25+
head :not_found
26+
end
27+
rescue ActiveStorage::IntegrityError
28+
head :unprocessable_entity
29+
end
30+
31+
private
32+
33+
def db_service
34+
ActiveStorage::Blob.service
35+
end
36+
37+
def decode_verified_key
38+
ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key)
39+
end
40+
41+
def serve_file(key, content_type:, disposition:)
42+
options = {
43+
type: content_type || DEFAULT_SEND_FILE_TYPE,
44+
disposition: disposition || DEFAULT_SEND_FILE_DISPOSITION
45+
}
46+
send_data db_service.download(key), options
47+
end
48+
49+
def decode_verified_token
50+
ActiveStorage.verifier.verified(params[:encoded_token], purpose: :blob_token)
51+
end
52+
53+
def acceptable_content?(token)
54+
token[:content_type] == request.content_mime_type && token[:content_length] == request.content_length
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)