lazy_blob_storage 2.0.0.pre.beta.5 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45ca84928e6e354ad7966a97cf15c4d5ab6f93aa952c507f12ab405a30d12310
4
- data.tar.gz: 0d4f5893ddb9acfa1631b63177f3951dc178492be3c17781ea661d141d887da9
3
+ metadata.gz: 9ff3df420c3918382cd60e6956cbba5839c545b83043e64ec034ca3603da74ee
4
+ data.tar.gz: acb455a52cdee6e72de4eecf22e9aacf0bbd6f406c20941fcc78bacba4fa5149
5
5
  SHA512:
6
- metadata.gz: ad52f8ca8bcb8cb31f9a683b96ac88cd2d7ef92e6176affe55ea30d2a28822de2709c59914719b03a2bcde0e015ec80854be9f61b1d99889f7bcc6d3580a93fe
7
- data.tar.gz: e0da5bf09d25d351e1cda6edac680efd82b2a18e56bf514f3e82e7e6b13c61ef4eb6be3263ca7b28fb1fc9c01a6865e05828363efd520e35664f0e43ba9c4890
6
+ metadata.gz: 3a8f3e606cdf64e5b7b040917585a5f9a59d64b0d22cc2300ad20063b510cf651b0e979253f08379b4ef442b0bec7edcb9d926b5593c64bfe054a5a08857ca81
7
+ data.tar.gz: 3368a9a65aed6487156585dbd54e63b87d4ae33c63e0bf8d13bdbcc451162839ad650fe70c769ac2aa7e016ab119979397e8563bb1a9af8772a912f682ddaa8f
data/Rakefile CHANGED
@@ -1,31 +1,31 @@
1
1
  begin
2
- require 'bundler/setup'
2
+ require "bundler/setup"
3
3
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
5
  end
6
6
 
7
- require 'rdoc/task'
7
+ require "rdoc/task"
8
8
 
9
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')
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 'rails/tasks/engine.rake'
18
+ load "rails/tasks/engine.rake"
19
19
 
20
- load 'rails/tasks/statistics.rake'
20
+ load "rails/tasks/statistics.rake"
21
21
 
22
- require 'bundler/gem_tasks'
22
+ require "bundler/gem_tasks"
23
23
 
24
- require 'rake/testtask'
24
+ require "rake/testtask"
25
25
 
26
26
  Rake::TestTask.new(:test) do |t|
27
- t.libs << 'test'
28
- t.pattern = 'test/**/*_test.rb'
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['Content-Type'] = @blob.content_type
7
- headers['Content-Length'] = @blob.content_length
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: { digest: @blob.digest }
16
+ render json: {digest: @blob.digest}
17
17
  else
18
- render json: { errors: @blob.errors }, status: :unprocessable_entity
18
+ render json: {errors: @blob.errors}, status: :unprocessable_entity
19
19
  end
20
20
  end
21
- end
21
+ end
@@ -5,7 +5,7 @@ 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 "#{name}_upload")
8
+ blob = LazyBlob.from_upload(send("#{name}_upload"))
9
9
 
10
10
  if (attached = (send "attached_#{name}"))
11
11
  attached.lazy_blob = blob
@@ -31,7 +31,7 @@ module LazyAttachable
31
31
  end
32
32
 
33
33
  define_method("attached_#{name}_path") do
34
- if (send "#{name}_attached?")
34
+ if send "#{name}_attached?"
35
35
  digest = (send "attached_#{name}").digest
36
36
  Rails.application.routes.url_helpers.lazy_blob_path(digest)
37
37
  else
@@ -40,7 +40,7 @@ module LazyAttachable
40
40
  end
41
41
 
42
42
  define_method("attached_#{name}_url") do
43
- if (send "#{name}_attached?")
43
+ if send "#{name}_attached?"
44
44
  digest = (send "attached_#{name}").digest
45
45
  Rails.application.routes.url_helpers.lazy_blob_url(digest)
46
46
  else
@@ -81,11 +81,11 @@ module LazyAttachable
81
81
  attr_accessor "#{name}_digest"
82
82
 
83
83
  has_one "attached_#{name}".to_sym,
84
- -> { where(name: name) },
85
- class_name: "LazyAttachment",
86
- as: :record,
87
- autosave: true,
88
- dependent: :destroy
84
+ -> { where(name: name) },
85
+ class_name: "LazyAttachment",
86
+ as: :record,
87
+ autosave: true,
88
+ dependent: :destroy
89
89
 
90
90
  scope "with_attached_#{name}".to_sym, -> {
91
91
  includes("attached_#{name}")
@@ -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
 
@@ -23,4 +25,33 @@ class LazyAttachment < ActiveRecord::Base
23
25
  def filename
24
26
  LazyBlob.select(:id, :filename).find(lazy_blob_id).filename
25
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
26
57
  end
@@ -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
- presence: true,
10
- numericality: { greater_than: 0, less_than: LazyBlobStorage._max_blob_size }
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: { maximum: LazyBlobStorage._max_blob_size }
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, 'rb')
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,7 +1,7 @@
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
4
+ primary_key_type, _foreign_key_type = primary_and_foreign_key_types
5
5
 
6
6
  create_table :lazy_blobs, id: primary_key_type do |t|
7
7
  t.string :filename
@@ -14,6 +14,7 @@ class CreateLazyBlobs < ActiveRecord::Migration[6.0]
14
14
  end
15
15
 
16
16
  private
17
+
17
18
  def primary_and_foreign_key_types
18
19
  config = Rails.configuration.generators
19
20
  setting = config.options[config.orm][:primary_key_type]
@@ -0,0 +1,11 @@
1
+ class CreateLazyVariants < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :lazy_variants do |t|
4
+ t.references :lazy_attachment
5
+ t.references :lazy_blob
6
+ t.string :name
7
+ t.index :name
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class AddDigestToLazyVariants < ActiveRecord::Migration[7.0]
2
+ def change
3
+ add_column :lazy_variants, :digest, :string
4
+ end
5
+ end
@@ -1,8 +1,8 @@
1
1
  module LazyBlobStorage
2
2
  class Engine < ::Rails::Engine
3
- initializer 'mixin_lazy_attachable' do
3
+ initializer "mixin_lazy_attachable" do
4
4
  ActiveSupport.on_load(:active_record) do
5
- self.include LazyAttachable
5
+ include LazyAttachable
6
6
  end
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module LazyBlobStorage
2
- VERSION = "2.0.0-beta.5"
2
+ VERSION = "2.0.0-beta.6"
3
3
  end
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.5
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: 2022-10-28 00:00:00.000000000 Z
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.2.33
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?