Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 101 changed files with 872 additions and 206 deletions.
17 changes: 11 additions & 6 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--format documentation
--require rails_helper
17 changes: 17 additions & 0 deletions .rubocop.yml
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
13 changes: 2 additions & 11 deletions Gemfile
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]
39 changes: 21 additions & 18 deletions README.md
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).
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -14,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
load 'rails/tasks/engine.rake'

load 'rails/tasks/statistics.rake'
Expand All @@ -24,8 +26,8 @@ require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.libs << 'spec'
t.pattern = 'spec/**/*_spec.rb'
t.verbose = false
end

Expand Down
42 changes: 27 additions & 15 deletions active_storage_db.gemspec
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
1 change: 0 additions & 1 deletion app/assets/config/active_storage_db_manifest.js

This file was deleted.

5 changes: 0 additions & 5 deletions app/controllers/active_storage_db/application_controller.rb

This file was deleted.

57 changes: 57 additions & 0 deletions app/controllers/active_storage_db/files_controller.rb
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
4 changes: 0 additions & 4 deletions app/helpers/active_storage_db/application_helper.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/jobs/active_storage_db/application_job.rb

This file was deleted.

6 changes: 0 additions & 6 deletions app/mailers/active_storage_db/application_mailer.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/active_storage_db/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActiveStorageDB
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
Expand Down
10 changes: 10 additions & 0 deletions app/models/active_storage_db/file.rb
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
15 changes: 0 additions & 15 deletions app/views/layouts/active_storage_db/application.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
require 'rails/engine/commands'

5 changes: 5 additions & 0 deletions config/initializers/inflections.rb
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
4 changes: 4 additions & 0 deletions config/routes.rb
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 db/migrate/20200702202022_create_active_storage_db_files.rb
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
Loading

0 comments on commit 12a41c4

Please sign in to comment.