lazy_blob_storage 2.0.0.pre.beta.7 → 2.0.0.pre.beta.9

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: f59d945b4c54444be929225d268159520d5ceefd47cd74f432e82c2f8c05c26c
4
- data.tar.gz: c5092d1b0f3db7974116aa3d7041a75deae86e9c94b3395bd9609ebba38bd0f3
3
+ metadata.gz: f0fbc67607315036a01e23d1d5eee5393fdc1de62d2e28a754a866f766dc1817
4
+ data.tar.gz: 2199b435b39f20882660afbed3f0d16d1fa83a0841fcb1ef228989975c1073f6
5
5
  SHA512:
6
- metadata.gz: a3319925424b037dd4934322b021ef020004a10db8ba768c88a5bc0461e0a88e1a7f62f8d3598f668edbfdd10c7f16c2f2f09b8ffdc357c405a3842daf93c648
7
- data.tar.gz: 4536667ff095869e75fa3b9cee03c59dd78481270c2a42edd3bc0ad11a9f6634a9fb04c816ba206c309343bbdae508ada8a986249579a65aa57afbc8557cab1a
6
+ metadata.gz: 23789bf445266bb7d56bb09602401cd6478ebfd74edbedf01a60c0d87b1395f7ec4a857065b2c919bc1ecfca43c12e54622833bfd549d21a9e36788c5b8a373c
7
+ data.tar.gz: 5c1fc717a604406792dcea3ab1529c55402f22fee86930d984da7061cac44aad359d5f4142037ef127ed18ef6afa7ff02c0096355c9116b9bd38f4f55f9739d6
@@ -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
@@ -1,11 +1,21 @@
1
1
  class CreateLazyVariants < ActiveRecord::Migration[7.0]
2
2
  def change
3
- create_table :lazy_variants do |t|
4
- t.references :lazy_attachment
5
- t.references :lazy_blob
3
+ primary_key_type, foreign_key_type = primary_and_foreign_key_types
4
+
5
+ create_table :lazy_variants, id: primary_key_type do |t|
6
+ t.references :lazy_attachment, type: foreign_key_type
7
+ t.references :lazy_blob, 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
@@ -1,3 +1,3 @@
1
1
  module LazyBlobStorage
2
- VERSION = "2.0.0-beta.7"
2
+ VERSION = "2.0.0-beta.9"
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.7
4
+ version: 2.0.0.pre.beta.9
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-13 00:00:00.000000000 Z
11
+ date: 2023-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -154,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  - !ruby/object:Gem::Version
155
155
  version: 1.3.1
156
156
  requirements: []
157
- rubygems_version: 3.4.10
157
+ rubygems_version: 3.4.18
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?