lazy_blob_storage 1.2.1 → 2.0.0.pre.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -7
- data/app/models/concerns/lazy_attachable.rb +12 -12
- data/app/models/lazy_blob.rb +2 -2
- data/lib/lazy_blob_storage/engine.rb +0 -8
- data/lib/lazy_blob_storage/version.rb +1 -1
- data/lib/lazy_blob_storage.rb +5 -0
- data/lib/tasks/lazy_blob_storage_tasks.rake +13 -4
- metadata +52 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f76aadde1c8e491a20d9d43ab01b4726ba9039f4a50b0aa60cbcc571493939f
|
4
|
+
data.tar.gz: 0f38de7c7d200daa0208fe363a4ce8a6d7d175121fd1aef55563022e8fe3102b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c37f509e254d0494c019dd60510d07d8ad023ad22d6b3413475424afd82ba60ce138019b0526ca7dcee6908d822dea3b95417d8d490710a0a81a0518558b30c
|
7
|
+
data.tar.gz: d1a8b8c8cf70dd6c8d9e4a7cb634781821908ac08e89a54d46fdc9bd98ec40fa8c1910f9ed8dafad6fdee84cd6fdaece89c187afc73767d453f1586d4710623d
|
data/README.md
CHANGED
@@ -1,26 +1,47 @@
|
|
1
1
|
# LazyBlobStorage
|
2
2
|
|
3
|
-
Useful for attaching small
|
3
|
+
Useful for attaching small database-backed blobs to records. Good
|
4
4
|
when you need small file upload support, but don't want to bother with a cloud
|
5
5
|
service because you know you'll have low traffic/user counts. Uses a SHA256
|
6
6
|
digest to prevent duplicate blob uploads.
|
7
7
|
|
8
|
-
|
8
|
+
Nothing fancy. Just stores files and allows public access using the
|
9
9
|
digest as a key. Respects HTTP Cache headers.
|
10
10
|
|
11
|
+
You probably want to put a CDN/Cache/RateLimit in front of the
|
12
|
+
`"lazy_blobs#show"` route because somebody could DOS your website
|
13
|
+
if all your workers are serving files from the database.
|
14
|
+
|
15
|
+
Basically this is a dumb idea, but setting up AWS buckets and permissions just
|
16
|
+
to upload a dumb logo is :big_sigh_emoji:
|
17
|
+
|
18
|
+
Don't use this. I mean it.
|
19
|
+
|
20
|
+
## Breaking Changes in 2.0.0-beta.1
|
21
|
+
|
22
|
+
- YOLO support for rails > 6 (it's probably fine)
|
23
|
+
- Migrations do not get added to rails paths automatically anymore. They must be copied with the `lazy_blob_storage:install:migrations` rake task.
|
24
|
+
- Prefixed most attachment methods with `lazy_` to avoid conflicts.
|
25
|
+
- The default max blob size is 100kb. It should probably be lower. You can set this to a number of bytes with `LazyBlobStorage.max_blob_size=`
|
26
|
+
- !!! Do not use this for high traffic projects. !!!
|
27
|
+
- !!! Do not use this to store big files !!!
|
28
|
+
- !!! This dumb idea only works for very small files like 10kb logos or icons. !!!
|
29
|
+
- !!! you were warned !!!
|
30
|
+
|
11
31
|
## Note
|
12
32
|
|
13
33
|
If it's not obvious. This is for inclusion in a Rails app.
|
14
34
|
|
15
|
-
|
35
|
+
Built with Rails 6. It could ~probably~ work with Rails 4/5, too.
|
16
36
|
|
17
37
|
## Usage
|
18
38
|
|
19
39
|
1. Add `gem "lazy_blob_storage" to Gemfile`
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
40
|
+
1. Run `rake lazy_blob_storage:install:migrations`
|
41
|
+
1. Run `rake db:migrate`
|
42
|
+
1. Declare `has_lazy_attached :image` in some model
|
43
|
+
1. Add `f.file_field :image_upload` to the form template
|
44
|
+
1. Use `image_tag @thing.lazy_attached_image_path` or `image_tag @thing.lazy_image_url`
|
24
45
|
|
25
46
|
Note: LazyBlobsController respects HTTP cache directives
|
26
47
|
|
@@ -7,7 +7,7 @@ module LazyAttachable
|
|
7
7
|
if (send "#{name}_upload").present? && (send "#{name}_upload_size_ok?")
|
8
8
|
blob = LazyBlob.from_upload(send "#{name}_upload")
|
9
9
|
|
10
|
-
if (attached = (send "
|
10
|
+
if (attached = (send "lazy_attached_#{name}"))
|
11
11
|
attached.lazy_blob = blob
|
12
12
|
else
|
13
13
|
attachment = LazyAttachment.new(
|
@@ -15,7 +15,7 @@ module LazyAttachable
|
|
15
15
|
record: self,
|
16
16
|
lazy_blob: blob
|
17
17
|
)
|
18
|
-
(send "
|
18
|
+
(send "lazy_attached_#{name}=", attachment)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -30,18 +30,18 @@ module LazyAttachable
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
define_method("
|
33
|
+
define_method("lazy_attached_#{name}_path") do
|
34
34
|
if (send "#{name}_attached?")
|
35
|
-
digest = (send "
|
35
|
+
digest = (send "lazy_attached_#{name}").digest
|
36
36
|
Rails.application.routes.url_helpers.lazy_blob_path(digest)
|
37
37
|
else
|
38
38
|
""
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
define_method("
|
42
|
+
define_method("lazy_attached_#{name}_url") do
|
43
43
|
if (send "#{name}_attached?")
|
44
|
-
digest = (send "
|
44
|
+
digest = (send "lazy_attached_#{name}").digest
|
45
45
|
Rails.application.routes.url_helpers.lazy_blob_url(digest)
|
46
46
|
else
|
47
47
|
""
|
@@ -49,12 +49,12 @@ module LazyAttachable
|
|
49
49
|
end
|
50
50
|
|
51
51
|
define_method("#{name}_attached?") do
|
52
|
-
(send "
|
52
|
+
(send "lazy_attached_#{name}").present?
|
53
53
|
end
|
54
54
|
|
55
55
|
define_method("handle_#{name}_digest") do
|
56
56
|
digest = (send "#{name}_digest").presence
|
57
|
-
attached = (send "
|
57
|
+
attached = (send "lazy_attached_#{name}").presence
|
58
58
|
same_as_attached = (attached && digest) && (attached.digest == digest)
|
59
59
|
|
60
60
|
if digest && !same_as_attached
|
@@ -67,7 +67,7 @@ module LazyAttachable
|
|
67
67
|
digest: digest
|
68
68
|
)
|
69
69
|
|
70
|
-
(send "
|
70
|
+
(send "lazy_attached_#{name}=", attachment)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -80,15 +80,15 @@ module LazyAttachable
|
|
80
80
|
attr_accessor "#{name}_upload"
|
81
81
|
attr_accessor "#{name}_digest"
|
82
82
|
|
83
|
-
has_one "
|
83
|
+
has_one "lazy_attached_#{name}".to_sym,
|
84
84
|
-> { where(name: name) },
|
85
85
|
class_name: "LazyAttachment",
|
86
86
|
as: :record,
|
87
87
|
autosave: true,
|
88
88
|
dependent: :destroy
|
89
89
|
|
90
|
-
scope "
|
91
|
-
includes("
|
90
|
+
scope "with_lazy_attached_#{name}".to_sym, -> {
|
91
|
+
includes("lazy_attached_#{name}")
|
92
92
|
}
|
93
93
|
|
94
94
|
validate "validate_size_of_#{name}_upload".to_sym
|
data/app/models/lazy_blob.rb
CHANGED
@@ -7,9 +7,9 @@ class LazyBlob < ActiveRecord::Base
|
|
7
7
|
validates :content_type, presence: true
|
8
8
|
validates :content_length,
|
9
9
|
presence: true,
|
10
|
-
numericality: { greater_than: 0, less_than:
|
10
|
+
numericality: { greater_than: 0, less_than: LazyBlobStorage.max_blob_size || LazyBlobStorage.default_max_blob_size }
|
11
11
|
validates :digest, presence: true, uniqueness: true
|
12
|
-
validates :content, presence: true, length: { maximum:
|
12
|
+
validates :content, presence: true, length: { maximum: LazyBlobStorage.max_blob_size || LazyBlobStorage.default_max_blob_size }
|
13
13
|
|
14
14
|
def self.from_upload(uploaded_file)
|
15
15
|
file = File.open(uploaded_file.tempfile.path, 'rb')
|
@@ -1,13 +1,5 @@
|
|
1
1
|
module LazyBlobStorage
|
2
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
3
|
initializer 'mixin_lazy_attachable' do
|
12
4
|
ActiveSupport.on_load(:active_record) do
|
13
5
|
self.include LazyAttachable
|
data/lib/lazy_blob_storage.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
|
2
|
-
# task
|
3
|
-
|
4
|
-
|
1
|
+
namespace :lazy_blob_storage do
|
2
|
+
# Prevent migration installation task from showing up twice.
|
3
|
+
Rake::Task["install:migrations"].clear_comments
|
4
|
+
|
5
|
+
desc "Copy over the migration needed to the application"
|
6
|
+
task install: :environment do
|
7
|
+
if Rake::Task.task_defined?("lazy_blob_storage:install:migrations")
|
8
|
+
Rake::Task["lazy_blob_storage:install:migrations"].invoke
|
9
|
+
else
|
10
|
+
Rake::Task["app:lazy_blob_storage:install:migrations"].invoke
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,35 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_blob_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.pre.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Corey Smedstad
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
20
31
|
- - ">="
|
21
32
|
- !ruby/object:Gem::Version
|
22
|
-
version: 6
|
33
|
+
version: '6'
|
23
34
|
type: :runtime
|
24
35
|
prerelease: false
|
25
36
|
version_requirements: !ruby/object:Gem::Requirement
|
26
37
|
requirements:
|
27
|
-
- - "
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
|
-
version: 6
|
40
|
+
version: '6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
30
45
|
- - ">="
|
31
46
|
- !ruby/object:Gem::Version
|
32
|
-
version: 6
|
47
|
+
version: '6'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6'
|
33
55
|
- !ruby/object:Gem::Dependency
|
34
56
|
name: pg
|
35
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,7 +66,21 @@ dependencies:
|
|
44
66
|
- - ">="
|
45
67
|
- !ruby/object:Gem::Version
|
46
68
|
version: '0'
|
47
|
-
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '6'
|
83
|
+
description:
|
48
84
|
email:
|
49
85
|
- csmedstad@mreach.com
|
50
86
|
executables: []
|
@@ -72,7 +108,7 @@ licenses:
|
|
72
108
|
- MIT
|
73
109
|
metadata:
|
74
110
|
allowed_push_host: https://rubygems.org
|
75
|
-
post_install_message:
|
111
|
+
post_install_message:
|
76
112
|
rdoc_options: []
|
77
113
|
require_paths:
|
78
114
|
- lib
|
@@ -83,12 +119,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
119
|
version: '0'
|
84
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
121
|
requirements:
|
86
|
-
- - "
|
122
|
+
- - ">"
|
87
123
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
124
|
+
version: 1.3.1
|
89
125
|
requirements: []
|
90
|
-
rubygems_version: 3.
|
91
|
-
signing_key:
|
126
|
+
rubygems_version: 3.2.33
|
127
|
+
signing_key:
|
92
128
|
specification_version: 4
|
93
129
|
summary: Low traffic site? Small file upload needs? Don't want to setup a cloud service?
|
94
130
|
Lazy Blob Storage is for you!
|