lazy_blob_storage 2.0.0.pre.beta.3 → 2.0.0.pre.beta.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +14 -14
- data/app/controllers/lazy_blobs_controller.rb +5 -5
- data/app/models/concerns/lazy_attachable.rb +21 -21
- data/app/models/lazy_attachment.rb +36 -1
- data/app/models/lazy_blob.rb +23 -4
- data/app/models/lazy_variant.rb +27 -0
- data/db/migrate/20200213161207_create_lazy_blobs.rb +14 -1
- data/db/migrate/20200213164215_create_lazy_attachments.rb +13 -3
- data/db/migrate/20230309225433_create_lazy_variants.rb +11 -0
- data/db/migrate/20230309235549_add_digest_to_lazy_variants.rb +5 -0
- data/lib/lazy_blob_storage/engine.rb +2 -2
- data/lib/lazy_blob_storage/version.rb +1 -1
- data/lib/lazy_blob_storage.rb +4 -0
- metadata +34 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ff3df420c3918382cd60e6956cbba5839c545b83043e64ec034ca3603da74ee
|
4
|
+
data.tar.gz: acb455a52cdee6e72de4eecf22e9aacf0bbd6f406c20941fcc78bacba4fa5149
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a8f3e606cdf64e5b7b040917585a5f9a59d64b0d22cc2300ad20063b510cf651b0e979253f08379b4ef442b0bec7edcb9d926b5593c64bfe054a5a08857ca81
|
7
|
+
data.tar.gz: 3368a9a65aed6487156585dbd54e63b87d4ae33c63e0bf8d13bdbcc451162839ad650fe70c769ac2aa7e016ab119979397e8563bb1a9af8772a912f682ddaa8f
|
data/README.md
CHANGED
@@ -19,9 +19,9 @@ Don't use this. I mean it.
|
|
19
19
|
|
20
20
|
## Breaking Changes in 2.0.0-beta.1
|
21
21
|
|
22
|
+
- Support UUID key types
|
22
23
|
- YOLO support for rails > 6 (it's probably fine)
|
23
24
|
- 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
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
26
|
- !!! Do not use this for high traffic projects. !!!
|
27
27
|
- !!! Do not use this to store big files !!!
|
data/Rakefile
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
begin
|
2
|
-
require
|
2
|
+
require "bundler/setup"
|
3
3
|
rescue LoadError
|
4
|
-
puts
|
4
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
5
5
|
end
|
6
6
|
|
7
|
-
require
|
7
|
+
require "rdoc/task"
|
8
8
|
|
9
9
|
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir =
|
11
|
-
rdoc.title
|
12
|
-
rdoc.options <<
|
13
|
-
rdoc.rdoc_files.include(
|
14
|
-
rdoc.rdoc_files.include(
|
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
15
|
end
|
16
16
|
|
17
17
|
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
18
|
-
load
|
18
|
+
load "rails/tasks/engine.rake"
|
19
19
|
|
20
|
-
load
|
20
|
+
load "rails/tasks/statistics.rake"
|
21
21
|
|
22
|
-
require
|
22
|
+
require "bundler/gem_tasks"
|
23
23
|
|
24
|
-
require
|
24
|
+
require "rake/testtask"
|
25
25
|
|
26
26
|
Rake::TestTask.new(:test) do |t|
|
27
|
-
t.libs <<
|
28
|
-
t.pattern =
|
27
|
+
t.libs << "test"
|
28
|
+
t.pattern = "test/**/*_test.rb"
|
29
29
|
t.verbose = false
|
30
30
|
end
|
31
31
|
|
@@ -3,8 +3,8 @@ class LazyBlobsController < ActionController::Base
|
|
3
3
|
@blob = LazyBlob.find_by(digest: params[:digest])
|
4
4
|
|
5
5
|
if stale?(@blob, last_modified: @blob.created_at.utc, public: true)
|
6
|
-
headers[
|
7
|
-
headers[
|
6
|
+
headers["Content-Type"] = @blob.content_type
|
7
|
+
headers["Content-Length"] = @blob.content_length
|
8
8
|
send_data(@blob.content, filename: @blob.filename, disposition: :inline)
|
9
9
|
end
|
10
10
|
end
|
@@ -13,9 +13,9 @@ class LazyBlobsController < ActionController::Base
|
|
13
13
|
@blob = LazyBlob.from_upload(params[:file])
|
14
14
|
|
15
15
|
if @blob.save
|
16
|
-
render json: {
|
16
|
+
render json: {digest: @blob.digest}
|
17
17
|
else
|
18
|
-
render json: {
|
18
|
+
render json: {errors: @blob.errors}, status: :unprocessable_entity
|
19
19
|
end
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
@@ -5,9 +5,9 @@ module LazyAttachable
|
|
5
5
|
def initialize(name)
|
6
6
|
define_method("handle_#{name}_upload") do
|
7
7
|
if (send "#{name}_upload").present? && (send "#{name}_upload_size_ok?")
|
8
|
-
blob = LazyBlob.from_upload(send
|
8
|
+
blob = LazyBlob.from_upload(send("#{name}_upload"))
|
9
9
|
|
10
|
-
if (attached = (send "
|
10
|
+
if (attached = (send "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 "attached_#{name}=", attachment)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -26,22 +26,22 @@ module LazyAttachable
|
|
26
26
|
|
27
27
|
define_method("validate_size_of_#{name}_upload") do
|
28
28
|
if (send "#{name}_upload").present? && !(send "#{name}_upload_size_ok?")
|
29
|
-
errors.add("#{name}_upload", "file too large (limit #{
|
29
|
+
errors.add("#{name}_upload", "file too large (limit #{LazyBlobStorage._max_blob_size / 1_000} kb")
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
define_method("
|
34
|
-
if
|
35
|
-
digest = (send "
|
33
|
+
define_method("attached_#{name}_path") do
|
34
|
+
if send "#{name}_attached?"
|
35
|
+
digest = (send "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("
|
43
|
-
if
|
44
|
-
digest = (send "
|
42
|
+
define_method("attached_#{name}_url") do
|
43
|
+
if send "#{name}_attached?"
|
44
|
+
digest = (send "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 "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 "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 "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 "
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
83
|
+
has_one "attached_#{name}".to_sym,
|
84
|
+
-> { where(name: name) },
|
85
|
+
class_name: "LazyAttachment",
|
86
|
+
as: :record,
|
87
|
+
autosave: true,
|
88
|
+
dependent: :destroy
|
89
89
|
|
90
|
-
scope "
|
91
|
-
includes("
|
90
|
+
scope "with_attached_#{name}".to_sym, -> {
|
91
|
+
includes("attached_#{name}")
|
92
92
|
}
|
93
93
|
|
94
94
|
validate "validate_size_of_#{name}_upload".to_sym
|
@@ -1,6 +1,8 @@
|
|
1
1
|
class LazyAttachment < ActiveRecord::Base
|
2
|
-
belongs_to :lazy_blob
|
2
|
+
belongs_to :lazy_blob, autosave: true
|
3
3
|
belongs_to :record, polymorphic: true
|
4
|
+
has_many :lazy_variants, dependent: :destroy
|
5
|
+
|
4
6
|
validates :name, presence: true
|
5
7
|
validates :digest, presence: true
|
6
8
|
|
@@ -19,4 +21,37 @@ class LazyAttachment < ActiveRecord::Base
|
|
19
21
|
def url
|
20
22
|
Rails.application.routes.url_helpers.lazy_blob_url(digest)
|
21
23
|
end
|
24
|
+
|
25
|
+
def filename
|
26
|
+
LazyBlob.select(:id, :filename).find(lazy_blob_id).filename
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_variant(name, x_size, y_size, format = "png")
|
30
|
+
return unless lazy_blob.content_type.start_with?("image/")
|
31
|
+
|
32
|
+
content_file = Tempfile.new("lazy_blob_content")
|
33
|
+
content_file.binmode
|
34
|
+
content_file.write(lazy_blob.content)
|
35
|
+
content_file.close
|
36
|
+
|
37
|
+
processed = ImageProcessing::MiniMagick
|
38
|
+
.source(content_file.path)
|
39
|
+
.resize_to_limit(x_size, y_size)
|
40
|
+
.convert(format)
|
41
|
+
.call
|
42
|
+
|
43
|
+
blob = LazyBlob.from_processed_variant(processed, name, format, lazy_blob)
|
44
|
+
|
45
|
+
variant = LazyVariant.new(
|
46
|
+
name: name,
|
47
|
+
lazy_blob: blob,
|
48
|
+
lazy_attachment: self
|
49
|
+
)
|
50
|
+
|
51
|
+
variant.save!
|
52
|
+
end
|
53
|
+
|
54
|
+
def variant(name)
|
55
|
+
lazy_variants.find_by(name: name)
|
56
|
+
end
|
22
57
|
end
|
data/app/models/lazy_blob.rb
CHANGED
@@ -2,17 +2,18 @@
|
|
2
2
|
|
3
3
|
class LazyBlob < ActiveRecord::Base
|
4
4
|
has_many :lazy_attachments, dependent: :restrict_with_error
|
5
|
+
has_many :lazy_variants, dependent: :restrict_with_error
|
5
6
|
|
6
7
|
validates :filename, presence: true
|
7
8
|
validates :content_type, presence: true
|
8
9
|
validates :content_length,
|
9
|
-
|
10
|
-
|
10
|
+
presence: true,
|
11
|
+
numericality: {greater_than: 0, less_than: LazyBlobStorage._max_blob_size}
|
11
12
|
validates :digest, presence: true, uniqueness: true
|
12
|
-
validates :content, presence: true, length: {
|
13
|
+
validates :content, presence: true, length: {maximum: LazyBlobStorage._max_blob_size}
|
13
14
|
|
14
15
|
def self.from_upload(uploaded_file)
|
15
|
-
file = File.open(uploaded_file.tempfile.path,
|
16
|
+
file = File.open(uploaded_file.tempfile.path, "rb")
|
16
17
|
data = file.read
|
17
18
|
digest = Digest::SHA256.new.update(data).hexdigest
|
18
19
|
blob = find_or_initialize_by(digest: digest)
|
@@ -29,6 +30,24 @@ class LazyBlob < ActiveRecord::Base
|
|
29
30
|
file.close
|
30
31
|
end
|
31
32
|
|
33
|
+
def self.from_processed_variant(tempfile, variant_name, variant_format, original_blob)
|
34
|
+
file = File.open(tempfile.path, "rb")
|
35
|
+
data = file.read
|
36
|
+
digest = Digest::SHA256.new.update(data).hexdigest
|
37
|
+
blob = find_or_initialize_by(digest: digest)
|
38
|
+
|
39
|
+
if blob.new_record?
|
40
|
+
blob.content = data
|
41
|
+
blob.content_length = tempfile.size
|
42
|
+
blob.filename = "#{original_blob.filename.split(".").first}--#{variant_name}.#{variant_format}"
|
43
|
+
blob.content_type = "image/#{variant_format}"
|
44
|
+
end
|
45
|
+
|
46
|
+
blob
|
47
|
+
ensure
|
48
|
+
file.close
|
49
|
+
end
|
50
|
+
|
32
51
|
def data_uri
|
33
52
|
"data:#{content_type};base64,#{Base64.encode64(content)}"
|
34
53
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class LazyVariant < ActiveRecord::Base
|
2
|
+
belongs_to :lazy_blob
|
3
|
+
belongs_to :lazy_attachment
|
4
|
+
validates :name, presence: true
|
5
|
+
validates :name, uniqueness: {scope: :lazy_attachment_id}
|
6
|
+
validates :digest, presence: true
|
7
|
+
|
8
|
+
before_validation do
|
9
|
+
self.digest = lazy_blob.digest
|
10
|
+
end
|
11
|
+
|
12
|
+
def data_uri
|
13
|
+
lazy_blob.data_uri
|
14
|
+
end
|
15
|
+
|
16
|
+
def path
|
17
|
+
Rails.application.routes.url_helpers.lazy_blob_path(digest)
|
18
|
+
end
|
19
|
+
|
20
|
+
def url
|
21
|
+
Rails.application.routes.url_helpers.lazy_blob_url(digest)
|
22
|
+
end
|
23
|
+
|
24
|
+
def filename
|
25
|
+
LazyBlob.select(:id, :filename).find(lazy_blob_id).filename
|
26
|
+
end
|
27
|
+
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
class CreateLazyBlobs < ActiveRecord::Migration[6.0]
|
2
2
|
def change
|
3
|
-
|
3
|
+
# Use Active Record's configured type for primary and foreign keys
|
4
|
+
primary_key_type, _foreign_key_type = primary_and_foreign_key_types
|
5
|
+
|
6
|
+
create_table :lazy_blobs, id: primary_key_type do |t|
|
4
7
|
t.string :filename
|
5
8
|
t.string :content_type
|
6
9
|
t.integer :content_length
|
@@ -9,4 +12,14 @@ class CreateLazyBlobs < ActiveRecord::Migration[6.0]
|
|
9
12
|
t.timestamps
|
10
13
|
end
|
11
14
|
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def primary_and_foreign_key_types
|
19
|
+
config = Rails.configuration.generators
|
20
|
+
setting = config.options[config.orm][:primary_key_type]
|
21
|
+
primary_key_type = setting || :primary_key
|
22
|
+
foreign_key_type = setting || :bigint
|
23
|
+
[primary_key_type, foreign_key_type]
|
24
|
+
end
|
12
25
|
end
|
@@ -1,11 +1,21 @@
|
|
1
1
|
class CreateLazyAttachments < ActiveRecord::Migration[6.0]
|
2
2
|
def change
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
primary_key_type, foreign_key_type = primary_and_foreign_key_types
|
4
|
+
|
5
|
+
create_table :lazy_attachments, id: primary_key_type do |t|
|
6
|
+
t.references :lazy_blob, type: foreign_key_type
|
7
|
+
t.references :record, polymorphic: true, type: foreign_key_type
|
6
8
|
t.string :name
|
7
9
|
t.index :name
|
8
10
|
t.timestamps
|
9
11
|
end
|
10
12
|
end
|
13
|
+
|
14
|
+
def primary_and_foreign_key_types
|
15
|
+
config = Rails.configuration.generators
|
16
|
+
setting = config.options[config.orm][:primary_key_type]
|
17
|
+
primary_key_type = setting || :primary_key
|
18
|
+
foreign_key_type = setting || :bigint
|
19
|
+
[primary_key_type, foreign_key_type]
|
20
|
+
end
|
11
21
|
end
|
data/lib/lazy_blob_storage.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_blob_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.pre.beta.
|
4
|
+
version: 2.0.0.pre.beta.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Corey Smedstad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,34 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: ruby-lsp
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: standard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description:
|
84
112
|
email:
|
85
113
|
- csmedstad@mreach.com
|
@@ -95,10 +123,13 @@ files:
|
|
95
123
|
- app/models/concerns/lazy_attachable.rb
|
96
124
|
- app/models/lazy_attachment.rb
|
97
125
|
- app/models/lazy_blob.rb
|
126
|
+
- app/models/lazy_variant.rb
|
98
127
|
- config/routes.rb
|
99
128
|
- db/migrate/20200213161207_create_lazy_blobs.rb
|
100
129
|
- db/migrate/20200213164215_create_lazy_attachments.rb
|
101
130
|
- db/migrate/20200213212649_add_blob_digest_to_lazy_attachments.rb
|
131
|
+
- db/migrate/20230309225433_create_lazy_variants.rb
|
132
|
+
- db/migrate/20230309235549_add_digest_to_lazy_variants.rb
|
102
133
|
- lib/lazy_blob_storage.rb
|
103
134
|
- lib/lazy_blob_storage/engine.rb
|
104
135
|
- lib/lazy_blob_storage/version.rb
|
@@ -123,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
154
|
- !ruby/object:Gem::Version
|
124
155
|
version: 1.3.1
|
125
156
|
requirements: []
|
126
|
-
rubygems_version: 3.
|
157
|
+
rubygems_version: 3.4.7
|
127
158
|
signing_key:
|
128
159
|
specification_version: 4
|
129
160
|
summary: Low traffic site? Small file upload needs? Don't want to setup a cloud service?
|