lazy_blob_storage 1.0.0 → 1.2.1

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: 5b711bdc3d1ef7a715b6f749568e99a3051900d955f205abeab52814bebbfa55
4
- data.tar.gz: 2d743f678cc1974717852be21d7d06c40a09b9e333967f7b51bb721566c94645
3
+ metadata.gz: d9384630bebc59d8659078936519987b004f1e93716e0ac6e9462bc804beb818
4
+ data.tar.gz: d83f8c699ceb0cc1076829d558bfc5d2e83753e5f9589b56bb38accfbf1b9f66
5
5
  SHA512:
6
- metadata.gz: 64b1b3373ab9158adbfeaf9afc00d4c19c7aecda9ac212508776d21cddcefceba59fdeb4033a4099e3d4fc51624b65028854c43295d70d2f410c1176dab8e2df
7
- data.tar.gz: 7563479841938ca4f145aeb0f9fce4317798ed1fcf5b4115f7a7566023b5a73c645ba26ab14782ce9ed0ced1c4245d6f0c78c5022bb9ae05dd03ebec6e725803
6
+ metadata.gz: d98a3911c8e1988e42eae91827b1ae29a04c138231f2b8ed81cfeb4a61fadcb7e25c49170697dd7c986f8e47f7f87d5f20f957ca611809cc2260a8ad3e2cef16
7
+ data.tar.gz: 71a963f730b5ab642575494d639a30e6de24bdb4bf279de504725ffa027d601ac7569ffbeb96a454a9969cbe8e118c380a11466d5ff86d4fda981ac315435fde
@@ -8,4 +8,14 @@ class LazyBlobsController < ActionController::Base
8
8
  send_data(@blob.content, filename: @blob.filename, disposition: :inline)
9
9
  end
10
10
  end
11
+
12
+ def create
13
+ @blob = LazyBlob.from_upload(params[:file])
14
+
15
+ if @blob.save
16
+ render json: { digest: @blob.digest }
17
+ else
18
+ render json: { errors: @blob.errors }, status: :unprocessable_entity
19
+ end
20
+ end
11
21
  end
@@ -21,12 +21,12 @@ module LazyAttachable
21
21
  end
22
22
 
23
23
  define_method("#{name}_upload_size_ok?") do
24
- (send "#{name}_upload").tempfile.size < 3.megabytes
24
+ (send "#{name}_upload").tempfile.size <= 1.megabytes
25
25
  end
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 3 MB)")
29
+ errors.add("#{name}_upload", "file too large (limit 1 MB)")
30
30
  end
31
31
  end
32
32
 
@@ -51,13 +51,34 @@ module LazyAttachable
51
51
  define_method("#{name}_attached?") do
52
52
  (send "attached_#{name}").present?
53
53
  end
54
+
55
+ define_method("handle_#{name}_digest") do
56
+ digest = (send "#{name}_digest").presence
57
+ attached = (send "attached_#{name}").presence
58
+ same_as_attached = (attached && digest) && (attached.digest == digest)
59
+
60
+ if digest && !same_as_attached
61
+ blob = LazyBlob.find_by(digest: digest)
62
+
63
+ if blob
64
+ attachment = LazyAttachment.new(
65
+ lazy_blob: blob,
66
+ name: name,
67
+ digest: digest
68
+ )
69
+
70
+ (send "attached_#{name}=", attachment)
71
+ end
72
+ end
73
+ end
54
74
  end
55
75
  end
56
76
 
57
77
  class_methods do
58
78
  def has_lazy_attached(name)
59
79
  include InstanceMethods.new(name)
60
- attr_accessor "#{name}_upload".to_sym
80
+ attr_accessor "#{name}_upload"
81
+ attr_accessor "#{name}_digest"
61
82
 
62
83
  has_one "attached_#{name}".to_sym,
63
84
  -> { where(name: name) },
@@ -67,11 +88,12 @@ module LazyAttachable
67
88
  dependent: :destroy
68
89
 
69
90
  scope "with_attached_#{name}".to_sym, -> {
70
- includes("attached_#{name}".to_sym)
91
+ includes("attached_#{name}")
71
92
  }
72
93
 
73
94
  validate "validate_size_of_#{name}_upload".to_sym
74
95
  before_validation "handle_#{name}_upload".to_sym
96
+ before_validation "handle_#{name}_digest".to_sym
75
97
  end
76
98
  end
77
99
  end
@@ -7,4 +7,16 @@ class LazyAttachment < ActiveRecord::Base
7
7
  before_validation do
8
8
  self.digest = lazy_blob.digest
9
9
  end
10
+
11
+ def data_uri
12
+ lazy_blob.data_uri
13
+ end
14
+
15
+ def path
16
+ Rails.application.routes.url_helpers.lazy_blob_path(digest)
17
+ end
18
+
19
+ def url
20
+ Rails.application.routes.url_helpers.lazy_blob_url(digest)
21
+ end
10
22
  end
@@ -9,7 +9,7 @@ class LazyBlob < ActiveRecord::Base
9
9
  presence: true,
10
10
  numericality: { greater_than: 0, less_than: 2.gigabytes }
11
11
  validates :digest, presence: true, uniqueness: true
12
- validates :content, presence: true
12
+ validates :content, presence: true, length: { maximum: 1.megabyte }
13
13
 
14
14
  def self.from_upload(uploaded_file)
15
15
  file = File.open(uploaded_file.tempfile.path, 'rb')
@@ -28,4 +28,8 @@ class LazyBlob < ActiveRecord::Base
28
28
  ensure
29
29
  file.close
30
30
  end
31
+
32
+ def data_uri
33
+ "data:#{content_type};base64,#{Base64.encode64(content)}"
34
+ end
31
35
  end
@@ -1,3 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
  get "lbs/file/:digest", to: "lazy_blobs#show", as: :lazy_blob
3
+ post "lbs/file", to: "lazy_blobs#create", as: :lazy_blob_upload
3
4
  end
@@ -1,3 +1,3 @@
1
1
  module LazyBlobStorage
2
- VERSION = "1.0.0"
2
+ VERSION = "1.2.1"
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: 1.0.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Smedstad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-13 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails