-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version of the component base on ActiveStorage DiskService. Main points: - Works with MySQL and PostgreSQL - All service methods implemented - RSpec tests
- Loading branch information
Mattia Roccoberton
committed
Aug 11, 2020
1 parent
f4072d7
commit 12a41c4
Showing
101 changed files
with
872 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
.bundle/ | ||
log/*.log | ||
pkg/ | ||
test/dummy/db/*.sqlite3 | ||
test/dummy/db/*.sqlite3-journal | ||
test/dummy/db/*.sqlite3-* | ||
test/dummy/log/*.log | ||
test/dummy/storage/ | ||
test/dummy/tmp/ | ||
|
||
/spec/dummy/db/*.sqlite3 | ||
/spec/dummy/db/*.sqlite3-journal | ||
/spec/dummy/db/*.sqlite3-* | ||
/spec/dummy/log/* | ||
/spec/dummy/storage/ | ||
/spec/dummy/tmp/ | ||
|
||
/.rubocop-* | ||
/coverage/ | ||
/Gemfile.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--color | ||
--format documentation | ||
--require rails_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
inherit_from: | ||
- https://relaxed.ruby.style/rubocop.yml | ||
|
||
AllCops: | ||
TargetRubyVersion: 2.6 | ||
Exclude: | ||
- bin/* | ||
- db/schema.rb | ||
- spec/dummy/**/* | ||
- vendor/**/* | ||
|
||
Gemspec/RequiredRubyVersion: | ||
Enabled: false | ||
|
||
Layout/LineLength: | ||
Enabled: true | ||
Max: 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | ||
|
||
# Declare your gem's dependencies in active_storage_db.gemspec. | ||
# Bundler will treat runtime dependencies like base dependencies, and | ||
# development dependencies will be added by default to the :development group. | ||
gemspec | ||
|
||
# Declare any dependencies that are still in development here instead of in | ||
# your gemspec. These might include edge Rails or gems from your path or | ||
# Git. Remember to move these dependencies to your gemspec before releasing | ||
# your gem to rubygems.org. | ||
|
||
# To use a debugger | ||
# gem 'byebug', group: [:development, :test] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
# ActiveStorageDB | ||
Short description and motivation. | ||
# Active Storage DB | ||
An Active Storage service upload/download plugin that stores files in a PostgreSQL or MySQL database. | ||
|
||
## Usage | ||
How to use my plugin. | ||
Main features: | ||
- all service methods implemented; | ||
- data is saved using a binary field (or blob); | ||
- RSpec tests. | ||
|
||
## Installation | ||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'active_storage_db' | ||
- Setup Active Storage in your Rails application | ||
- Add this line to your Gemfile: `gem 'active_storage_db'` | ||
- And execute: `bundle` | ||
- Install gem migrations: `bin/rails active_storage_db:install:migrations` | ||
- And execute: `bin/rails db:migrate` | ||
- Add to your `config/routes.rb`: `mount ActiveStorageDB::Engine => '/active_storage_db'` | ||
- Change Active Storage service in *config/environments/development.rb* to: `config.active_storage.service = :db` | ||
- Add to *config/storage.yml*: | ||
``` | ||
|
||
And then execute: | ||
```bash | ||
$ bundle | ||
db: | ||
service: DB | ||
``` | ||
|
||
Or install it yourself as: | ||
```bash | ||
$ gem install active_storage_db | ||
``` | ||
## Do you like it? Star it! | ||
If you use this component just star it. A developer is more motivated to improve a project when there is some interest. | ||
|
||
## Contributing | ||
Contribution directions go here. | ||
## Contributors | ||
- [Mattia Roccoberton](https://blocknot.es/): author | ||
- Inspired by [activestorage-database-service](https://github.com/TitovDigital/activestorage-database-service) project | ||
|
||
## License | ||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,43 @@ | ||
$:.push File.expand_path("lib", __dir__) | ||
# frozen_string_literal: true | ||
|
||
$:.push File.expand_path('lib', __dir__) | ||
|
||
# Maintain your gem's version: | ||
require "active_storage_db/version" | ||
require 'active_storage_db/version' | ||
|
||
# Describe your gem and declare its dependencies: | ||
Gem::Specification.new do |spec| | ||
spec.name = "active_storage_db" | ||
spec.name = 'active_storage_db' | ||
spec.version = ActiveStorageDB::VERSION | ||
spec.authors = ["Mattia Roccoberton"] | ||
spec.email = ["mattiaroccoberton@nebulab.it"] | ||
spec.homepage = "TODO" | ||
spec.summary = "TODO: Summary of ActiveStorageDB." | ||
spec.description = "TODO: Description of ActiveStorageDB." | ||
spec.license = "MIT" | ||
spec.authors = ['Mattia Roccoberton'] | ||
spec.email = ['mattiaroccoberton@nebulab.it'] | ||
spec.homepage = 'https://blocknot.es' | ||
spec.summary = 'ActiveStorage DB Service' | ||
spec.description = 'An ActiveStorage service plugin to store files in database.' | ||
spec.license = 'MIT' | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
# to allow pushing to a single host or delete this section to allow pushing to any host. | ||
if spec.respond_to?(:metadata) | ||
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" | ||
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against " \ | ||
"public gem pushes." | ||
raise 'RubyGems 2.0 or newer is required to protect against ' \ | ||
'public gem pushes.' | ||
end | ||
|
||
spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] | ||
spec.files = Dir["{app,config,db,lib}/**/*", 'MIT-LICENSE', 'Rakefile', 'README.md'] | ||
|
||
spec.add_dependency "rails", "~> 6.0.3", ">= 6.0.3.2" | ||
spec.add_dependency 'activestorage', '~> 6.0.0' | ||
spec.add_dependency 'rails', '~> 6.0.0' | ||
|
||
spec.add_development_dependency "sqlite3" | ||
spec.add_development_dependency 'capybara', '~> 3.33.0' | ||
spec.add_development_dependency 'database_cleaner-active_record', '~> 1.8.0' | ||
spec.add_development_dependency 'factory_bot_rails', '~> 6.1.0' | ||
spec.add_development_dependency 'mysql2', '~> 0.5.3' | ||
spec.add_development_dependency 'pg', '~> 1.2.3' | ||
spec.add_development_dependency 'pry', '~> 0.13.1' | ||
spec.add_development_dependency 'rspec-rails', '~> 4.0.1' | ||
spec.add_development_dependency 'rubocop', '~> 0.89.0' | ||
spec.add_development_dependency 'selenium-webdriver', '~> 3.142.7' | ||
spec.add_development_dependency 'simplecov', '~> 0.18.5' | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorageDB | ||
class FilesController < ActiveStorage::BaseController | ||
skip_forgery_protection | ||
|
||
def show | ||
if (key = decode_verified_key) | ||
serve_file(key[:key], content_type: key[:content_type], disposition: key[:disposition]) | ||
else | ||
head :not_found | ||
end | ||
rescue ActiveStorage::FileNotFoundError | ||
head :not_found | ||
end | ||
|
||
def update | ||
if (token = decode_verified_token) | ||
if acceptable_content?(token) | ||
db_service.upload(token[:key], request.body, checksum: token[:checksum]) | ||
else | ||
head :unprocessable_entity | ||
end | ||
else | ||
head :not_found | ||
end | ||
rescue ActiveStorage::IntegrityError | ||
head :unprocessable_entity | ||
end | ||
|
||
private | ||
|
||
def db_service | ||
ActiveStorage::Blob.service | ||
end | ||
|
||
def decode_verified_key | ||
ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key) | ||
end | ||
|
||
def serve_file(key, content_type:, disposition:) | ||
options = { | ||
type: content_type || DEFAULT_SEND_FILE_TYPE, | ||
disposition: disposition || DEFAULT_SEND_FILE_DISPOSITION | ||
} | ||
send_data db_service.download(key), options | ||
end | ||
|
||
def decode_verified_token | ||
ActiveStorage.verifier.verified(params[:encoded_token], purpose: :blob_token) | ||
end | ||
|
||
def acceptable_content?(token) | ||
token[:content_type] == request.content_mime_type && token[:content_length] == request.content_length | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorageDB | ||
class File < ApplicationRecord | ||
validates :ref, | ||
presence: true, | ||
allow_blank: false, | ||
uniqueness: { case_sensitive: false } | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
ActiveSupport::Inflector.inflections(:en) do |inflect| | ||
inflect.acronym 'DB' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
ActiveStorageDB::Engine.routes.draw do | ||
get '/files/:encoded_key/*filename', to: 'files#show', as: :service | ||
put '/files/:encoded_token', to: 'files#update', as: :update_service | ||
end |
13 changes: 13 additions & 0 deletions
13
db/migrate/20200702202022_create_active_storage_db_files.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateActiveStorageDBFiles < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :active_storage_db_files do |t| | ||
t.string :ref, null: false | ||
t.binary :data, null: false | ||
t.datetime :created_at, null: false | ||
|
||
t.index [:ref], unique: true | ||
end | ||
end | ||
end |
Oops, something went wrong.