lazy_blob_storage 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5b711bdc3d1ef7a715b6f749568e99a3051900d955f205abeab52814bebbfa55
4
+ data.tar.gz: 2d743f678cc1974717852be21d7d06c40a09b9e333967f7b51bb721566c94645
5
+ SHA512:
6
+ metadata.gz: 64b1b3373ab9158adbfeaf9afc00d4c19c7aecda9ac212508776d21cddcefceba59fdeb4033a4099e3d4fc51624b65028854c43295d70d2f410c1176dab8e2df
7
+ data.tar.gz: 7563479841938ca4f145aeb0f9fce4317798ed1fcf5b4115f7a7566023b5a73c645ba26ab14782ce9ed0ced1c4245d6f0c78c5022bb9ae05dd03ebec6e725803
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Corey Smedstad
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # LazyBlobStorage
2
+
3
+ Useful for attaching small (size < 3 MB) database-backed blobs to records. Good
4
+ when you need small file upload support, but don't want to bother with a cloud
5
+ service because you know you'll have low traffic/user counts. Uses a SHA256
6
+ digest to prevent duplicate blob uploads.
7
+
8
+ Doesn't do anything fancy. Just stores files and allows public access using the
9
+ digest as a key. Respects HTTP Cache headers.
10
+
11
+ ## Note
12
+
13
+ If it's not obvious. This is for inclusion in a Rails app.
14
+
15
+ Build with Rails 6. It'll ~probably~ work with Rails 4/5, too.
16
+
17
+ ## Usage
18
+
19
+ 1. Add `gem "lazy_blob_storage" to Gemfile`
20
+ 2. Run `rake db:migrate`
21
+ 3. Declare `has_lazy_attached :image` in some model
22
+ 4. Add `f.file_field :image_upload` to the form template
23
+ 5. Use `image_tag @thing.attached_image_path` or `image_tag @thing.image_url`
24
+
25
+ Note: LazyBlobsController respects HTTP cache directives
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'LazyBlobStorage'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
File without changes
@@ -0,0 +1,11 @@
1
+ class LazyBlobsController < ActionController::Base
2
+ def show
3
+ @blob = LazyBlob.find_by(digest: params[:digest])
4
+
5
+ if stale?(@blob, last_modified: @blob.created_at.utc, public: true)
6
+ headers['Content-Type'] = @blob.content_type
7
+ headers['Content-Length'] = @blob.content_length
8
+ send_data(@blob.content, filename: @blob.filename, disposition: :inline)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,77 @@
1
+ module LazyAttachable
2
+ extend ActiveSupport::Concern
3
+
4
+ class InstanceMethods < Module
5
+ def initialize(name)
6
+ define_method("handle_#{name}_upload") do
7
+ if (send "#{name}_upload").present? && (send "#{name}_upload_size_ok?")
8
+ blob = LazyBlob.from_upload(send "#{name}_upload")
9
+
10
+ if (attached = (send "attached_#{name}"))
11
+ attached.lazy_blob = blob
12
+ else
13
+ attachment = LazyAttachment.new(
14
+ name: name,
15
+ record: self,
16
+ lazy_blob: blob
17
+ )
18
+ (send "attached_#{name}=", attachment)
19
+ end
20
+ end
21
+ end
22
+
23
+ define_method("#{name}_upload_size_ok?") do
24
+ (send "#{name}_upload").tempfile.size < 3.megabytes
25
+ end
26
+
27
+ define_method("validate_size_of_#{name}_upload") do
28
+ if (send "#{name}_upload").present? && !(send "#{name}_upload_size_ok?")
29
+ errors.add("#{name}_upload", "file too large (limit 3 MB)")
30
+ end
31
+ end
32
+
33
+ define_method("attached_#{name}_path") do
34
+ if (send "#{name}_attached?")
35
+ digest = (send "attached_#{name}").digest
36
+ Rails.application.routes.url_helpers.lazy_blob_path(digest)
37
+ else
38
+ ""
39
+ end
40
+ end
41
+
42
+ define_method("attached_#{name}_url") do
43
+ if (send "#{name}_attached?")
44
+ digest = (send "attached_#{name}").digest
45
+ Rails.application.routes.url_helpers.lazy_blob_url(digest)
46
+ else
47
+ ""
48
+ end
49
+ end
50
+
51
+ define_method("#{name}_attached?") do
52
+ (send "attached_#{name}").present?
53
+ end
54
+ end
55
+ end
56
+
57
+ class_methods do
58
+ def has_lazy_attached(name)
59
+ include InstanceMethods.new(name)
60
+ attr_accessor "#{name}_upload".to_sym
61
+
62
+ has_one "attached_#{name}".to_sym,
63
+ -> { where(name: name) },
64
+ class_name: "LazyAttachment",
65
+ as: :record,
66
+ autosave: true,
67
+ dependent: :destroy
68
+
69
+ scope "with_attached_#{name}".to_sym, -> {
70
+ includes("attached_#{name}".to_sym)
71
+ }
72
+
73
+ validate "validate_size_of_#{name}_upload".to_sym
74
+ before_validation "handle_#{name}_upload".to_sym
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,10 @@
1
+ class LazyAttachment < ActiveRecord::Base
2
+ belongs_to :lazy_blob
3
+ belongs_to :record, polymorphic: true
4
+ validates :name, presence: true
5
+ validates :digest, presence: true
6
+
7
+ before_validation do
8
+ self.digest = lazy_blob.digest
9
+ end
10
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LazyBlob < ActiveRecord::Base
4
+ has_many :lazy_attachments, dependent: :restrict_with_error
5
+
6
+ validates :filename, presence: true
7
+ validates :content_type, presence: true
8
+ validates :content_length,
9
+ presence: true,
10
+ numericality: { greater_than: 0, less_than: 2.gigabytes }
11
+ validates :digest, presence: true, uniqueness: true
12
+ validates :content, presence: true
13
+
14
+ def self.from_upload(uploaded_file)
15
+ file = File.open(uploaded_file.tempfile.path, 'rb')
16
+ data = file.read
17
+ digest = Digest::SHA256.new.update(data).hexdigest
18
+ blob = find_or_initialize_by(digest: digest)
19
+
20
+ if blob.new_record?
21
+ blob.content = data
22
+ blob.content_length = uploaded_file.tempfile.size
23
+ blob.filename = uploaded_file.original_filename
24
+ blob.content_type = uploaded_file.content_type
25
+ end
26
+
27
+ blob
28
+ ensure
29
+ file.close
30
+ end
31
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ get "lbs/file/:digest", to: "lazy_blobs#show", as: :lazy_blob
3
+ end
@@ -0,0 +1,12 @@
1
+ class CreateLazyBlobs < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :lazy_blobs do |t|
4
+ t.string :filename
5
+ t.string :content_type
6
+ t.integer :content_length
7
+ t.string :digest
8
+ t.binary :content
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateLazyAttachments < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :lazy_attachments do |t|
4
+ t.references :lazy_blob
5
+ t.references :record, polymorphic: true
6
+ t.string :name
7
+ t.index :name
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class AddBlobDigestToLazyAttachments < ActiveRecord::Migration[6.0]
2
+ def change
3
+ add_column :lazy_attachments, :digest, :string
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module LazyBlobStorage
2
+ class Engine < ::Rails::Engine
3
+ initializer :append_migrations do |app|
4
+ unless app.root.to_s.match root.to_s
5
+ config.paths["db/migrate"].expanded.each do |expanded_path|
6
+ app.config.paths["db/migrate"] << expanded_path
7
+ end
8
+ end
9
+ end
10
+
11
+ initializer 'mixin_lazy_attachable' do
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include LazyAttachable
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module LazyBlobStorage
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "lazy_blob_storage/engine"
2
+
3
+ module LazyBlobStorage
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :lazy_blob_storage do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_blob_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Corey Smedstad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.0.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: pg
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description:
48
+ email:
49
+ - csmedstad@mreach.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/assets/config/lazy_blob_storage_manifest.js
58
+ - app/controllers/lazy_blobs_controller.rb
59
+ - app/models/concerns/lazy_attachable.rb
60
+ - app/models/lazy_attachment.rb
61
+ - app/models/lazy_blob.rb
62
+ - config/routes.rb
63
+ - db/migrate/20200213161207_create_lazy_blobs.rb
64
+ - db/migrate/20200213164215_create_lazy_attachments.rb
65
+ - db/migrate/20200213212649_add_blob_digest_to_lazy_attachments.rb
66
+ - lib/lazy_blob_storage.rb
67
+ - lib/lazy_blob_storage/engine.rb
68
+ - lib/lazy_blob_storage/version.rb
69
+ - lib/tasks/lazy_blob_storage_tasks.rake
70
+ homepage: https://git.mreach.com/csmedstad/lazy_blob_storage
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ allowed_push_host: https://rubygems.org
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Low traffic site? Small file upload needs? Don't want to setup a cloud service?
94
+ Lazy Blob Storage is for you!
95
+ test_files: []