lazy_blob_storage 2.0.0.pre.beta.8 → 2.0.0.pre.beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bd41b24bf9723782e729a80453c891b7997a1a254ec3020c966420fcf971fd5
4
- data.tar.gz: 5936757ccfb7262de3caa1d3e1b9b5910e97561286d3895769d3b9cb9f5716dc
3
+ metadata.gz: 244494e5f5ac513ae81c18a14cd2045d07bf297a270c61f8479c3b52b67e3682
4
+ data.tar.gz: d71622461c4614194c5b42d49fbb9076ecd727686a338dcf28cbe3ef42c6e724
5
5
  SHA512:
6
- metadata.gz: 4d02e19f73babc258462411c376b78368b46e742dc0ba1f5b944f27d3d585bd21b309a6ab825b7ab75ab79e36620abbc39f62b5ffedbf957d56601962eb5c8e1
7
- data.tar.gz: 2d6618d774f70847c975cc4eb1d58fbc75bf893d3a843928032a4672facd8de3b5f20a90c050ff0426763b4650b6c6606428fe0ea9a08786282ff7843544475f
6
+ metadata.gz: c2ceed800ee8417bb91f71be8a4021942db532bf8ea0a341d587d110ca1b335ba12480db89fbed36cd26188301e78a64542312703927fce11feec5cb54cb59a5
7
+ data.tar.gz: fb24529827e90bffd573c22ba460a168f2291e9fe643579edf4d448846e10483e6fee8b515f22851986ac6c0c2cb07ad3791e66bf9d8600c60386b0d3c6e1b79
@@ -21,7 +21,7 @@ module LazyAttachable
21
21
  end
22
22
 
23
23
  define_method("#{name}_upload_size_ok?") do
24
- (send "#{name}_upload").tempfile.size <= 1.megabytes
24
+ (send "#{name}_upload").tempfile.size <= LazyBlobStorage._max_blob_size
25
25
  end
26
26
 
27
27
  define_method("validate_size_of_#{name}_upload") do
@@ -1,4 +1,6 @@
1
1
  class LazyAttachment < ActiveRecord::Base
2
+ Result = Struct.new(:ok?, :error)
3
+
2
4
  belongs_to :lazy_blob, autosave: true
3
5
  belongs_to :record, polymorphic: true
4
6
  has_many :lazy_variants, dependent: :destroy
@@ -42,15 +44,54 @@ class LazyAttachment < ActiveRecord::Base
42
44
 
43
45
  blob = LazyBlob.from_processed_variant(processed, name, format, lazy_blob)
44
46
 
45
- variant = LazyVariant.new(
46
- name: name,
47
- lazy_blob: blob,
48
- lazy_attachment: self
47
+ variant = lazy_variants.find_or_initialize_by(
48
+ name: name
49
49
  )
50
+ variant.lazy_attachment = self
51
+ variant.lazy_blob = blob
50
52
 
51
53
  variant.save!
52
54
  end
53
55
 
56
+ def ensure_variant(name, x_size, y_size, format = "png")
57
+ unless lazy_blob.content_type.start_with?("image/")
58
+ return Result.new(false, "Not an image")
59
+ end
60
+
61
+ content_file = Tempfile.new("lazy_blob_content")
62
+ content_file.binmode
63
+ content_file.write(lazy_blob.content)
64
+ content_file.close
65
+
66
+ processed = ImageProcessing::MiniMagick
67
+ .source(content_file.path)
68
+ .resize_to_limit(x_size, y_size)
69
+ .convert(format)
70
+ .call
71
+
72
+ blob = LazyBlob.from_processed_variant(processed, name, format, lazy_blob)
73
+
74
+ variant = lazy_variants.find_or_initialize_by(
75
+ name: name
76
+ )
77
+ variant.lazy_attachment_id = id
78
+ variant.lazy_blob = blob
79
+
80
+ if variant.save
81
+ Result.new(true)
82
+ else
83
+ Result.new(false, errors.full_messages.join(", "))
84
+ end
85
+ rescue => e
86
+ Result.new(false, e.message)
87
+ end
88
+
89
+ def destroy_all_variants
90
+ blob_ids = lazy_variants.pluck(:lazy_blob_id)
91
+ lazy_variants.destroy_all
92
+ LazyBlob.where(id: blob_ids).destroy_all
93
+ end
94
+
54
95
  def variant(name)
55
96
  lazy_variants.find_by(name: name)
56
97
  end
@@ -6,11 +6,14 @@ class LazyBlob < ActiveRecord::Base
6
6
 
7
7
  validates :filename, presence: true
8
8
  validates :content_type, presence: true
9
- validates :content_length,
10
- presence: true,
11
- numericality: {greater_than: 0, less_than: LazyBlobStorage._max_blob_size}
9
+
10
+ # validates :content_length,
11
+ # presence: true,
12
+ # numericality: {greater_than: 0, less_than: 50.megabytes}
13
+ validate :content_length_reqs
12
14
  validates :digest, presence: true, uniqueness: true
13
- validates :content, presence: true, length: {maximum: LazyBlobStorage._max_blob_size}
15
+ # validates :content, presence: true, length: {maximum: 50.megabytes}
16
+ validate :content_reqs
14
17
 
15
18
  def self.from_upload(uploaded_file)
16
19
  file = File.open(uploaded_file.tempfile.path, "rb")
@@ -27,7 +30,7 @@ class LazyBlob < ActiveRecord::Base
27
30
 
28
31
  blob
29
32
  ensure
30
- file.close
33
+ file&.close
31
34
  end
32
35
 
33
36
  def self.from_processed_variant(tempfile, variant_name, variant_format, original_blob)
@@ -51,4 +54,26 @@ class LazyBlob < ActiveRecord::Base
51
54
  def data_uri
52
55
  "data:#{content_type};base64,#{Base64.encode64(content)}"
53
56
  end
57
+
58
+ private
59
+
60
+ def content_length_reqs
61
+ unless content_length.present? && content_length > 0
62
+ errors.add(:content_length, "must be present")
63
+ end
64
+
65
+ if content_length.present? && content_length > LazyBlobStorage._max_blob_size
66
+ errors.add(:content_length, LazyBlobStorage.default_max_blob_size_error_message)
67
+ end
68
+ end
69
+
70
+ def content_reqs
71
+ unless content.present? && content.length > 0
72
+ errors.add(:content, "must be present")
73
+ end
74
+
75
+ if content.present? && content.length > LazyBlobStorage._max_blob_size
76
+ errors.add(:content, LazyBlobStorage.default_max_blob_size_error_message)
77
+ end
78
+ end
54
79
  end
@@ -1,3 +1,3 @@
1
1
  module LazyBlobStorage
2
- VERSION = "2.0.0-beta.8"
2
+ VERSION = "2.0.0-beta.10"
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.8
4
+ version: 2.0.0.pre.beta.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Smedstad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-14 00:00:00.000000000 Z
11
+ date: 2025-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -150,11 +150,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
150
  version: '0'
151
151
  required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  requirements:
153
- - - ">"
153
+ - - ">="
154
154
  - !ruby/object:Gem::Version
155
- version: 1.3.1
155
+ version: '0'
156
156
  requirements: []
157
- rubygems_version: 3.4.10
157
+ rubygems_version: 3.5.22
158
158
  signing_key:
159
159
  specification_version: 4
160
160
  summary: Low traffic site? Small file upload needs? Don't want to setup a cloud service?