attach 1.0.5 → 1.1.0

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
- SHA1:
3
- metadata.gz: 2056ebcc16201f4fe826932ed8db99a711d32e27
4
- data.tar.gz: 06ccf7d7fe3d431341cbebc15492ce557a5daed8
2
+ SHA256:
3
+ metadata.gz: c7bbf0eb92bb9ffe4e7856ab71aed188f446a714508e0c18d2894f2438a81e0f
4
+ data.tar.gz: 9517712508be371342f5e5fb0cfb110b8e628c4935ab0096b13c473c1afe91e7
5
5
  SHA512:
6
- metadata.gz: 2b769025cdcc24a045e155db751f7a0feaa8094d17b5bec19e5c1b104e791e78113b471f400ee80bf792288e4709047eb632ffc1797677b2fc48e346c67fbb55
7
- data.tar.gz: 68071df914675ff62f3c862ff93dc8024b8b480158fe72ca64ebc1632c25cac8fcc224e2ea953eec3d1a1a90497debec58f634635b6e4ae3e974bcfd26de98d5
6
+ metadata.gz: ce2d2f261ab5eefa1583eec1b1180abc138c1ce6017499df74b2c42dc755aba48e6a267f23cf317329a1bfeba531e3ca3c34f807069887cf00d4ed136be0c35c
7
+ data.tar.gz: 604117cd6a9825a8bb8e586c1330daaef67221f080ed343b6397d2a0c76d5a0ad575a0f45f997cd177c7c99d316880eb03cc863a286cea41ac859ffffd04d797
@@ -25,21 +25,21 @@ module Attach
25
25
  validates :digest, :presence => true
26
26
  validates :token, :presence => true, :uniqueness => true
27
27
 
28
- # All attachments should have a token assigned to this
29
- before_validation { self.token = SecureRandom.uuid if self.token.blank? }
30
-
31
28
  # Allow custom data to be stored on the attachment
32
29
  serialize :custom, Hash
33
30
 
34
31
  # Set size and digest
35
32
  before_validation do
36
- self.digest = Digest::SHA1.hexdigest(self.binary)
37
- self.file_size = self.binary.bytesize
33
+ self.token ||= SecureRandom.uuid
34
+ self.digest ||= self.binary.is_a?(String) ? Digest::SHA1.hexdigest(self.binary) : Attach.backend.digest(self.binary)
35
+ self.file_size ||= self.binary.is_a?(String) ? self.binary.bytesize : Attach.backend.bytesize(self.binary)
38
36
  end
39
37
 
40
38
  # Write the binary to the backend storage
41
39
  after_create do
42
- Attach.backend.write(self, self.binary)
40
+ if self.binary
41
+ Attach.backend.write(self, self.binary)
42
+ end
43
43
  end
44
44
 
45
45
  # Remove any old images for this owner/role when this is added
@@ -41,6 +41,34 @@ module Attach
41
41
  end
42
42
  end
43
43
 
44
+ #
45
+ # Return the SHA1 digest of a given binary
46
+ #
47
+ def digest(binary)
48
+ if binary.respond_to?(:path)
49
+ sha1 = Digest::SHA1.new
50
+ binary.binmode
51
+ binary.rewind
52
+ while chunk = binary.read(1024 * 1024)
53
+ sha1.update(chunk)
54
+ end
55
+ sha1.hexdigest
56
+ else
57
+ Digest::SHA1.hexdigest(binary)
58
+ end
59
+ end
60
+
61
+ #
62
+ # Return the bytesize of a given binary
63
+ #
64
+ def bytesize(binary)
65
+ if binary.respond_to?(:path)
66
+ ::File.size(binary.path)
67
+ else
68
+ binary.bytesize
69
+ end
70
+ end
71
+
44
72
  end
45
73
  end
46
74
  end
@@ -12,9 +12,14 @@ module Attach
12
12
  end
13
13
  end
14
14
 
15
- def write(attachment, data)
15
+ def write(attachment, binary)
16
16
  binary = AttachmentBinary.where(:attachment_id => attachment.id).first_or_initialize
17
- binary.data = data
17
+ if binary.respond_to?(:path)
18
+ binary.rewind
19
+ binary.data = binary.read
20
+ else
21
+ binary.data = binary
22
+ end
18
23
  binary.save!
19
24
  end
20
25
 
@@ -12,8 +12,12 @@ module Attach
12
12
  def write(attachment, data)
13
13
  path = path_for_attachment(attachment)
14
14
  FileUtils.mkdir_p(::File.dirname(path))
15
- ::File.open(path, 'wb') do |f|
16
- f.write(data)
15
+ if data.respond_to?(:path)
16
+ FileUtils.mv(data.path, path)
17
+ else
18
+ ::File.open(path, 'wb') do |f|
19
+ f.write(data)
20
+ end
17
21
  end
18
22
  end
19
23
 
@@ -12,6 +12,8 @@ module Attach
12
12
  self.attachments.where(:role => @pending_attachment_deletions).destroy_all
13
13
  end
14
14
 
15
+ @replaced_attachment&.destroy
16
+
15
17
  if @pending_attachments
16
18
  @pending_attachments.values.each(&:save!)
17
19
  @pending_attachments = nil
@@ -24,7 +26,7 @@ module Attach
24
26
  def includes_attachments(*options)
25
27
  manipulate do |records|
26
28
  if records.empty?
27
- # Nothing to do
29
+ # Nothing to do
28
30
  else
29
31
 
30
32
  if options.first.is_a?(Hash)
@@ -141,12 +143,16 @@ module Attach
141
143
 
142
144
  define_method "#{name}=" do |file|
143
145
  if file.is_a?(Attach::Attachment)
146
+ @replaced_attachment = self.try(name)
144
147
  attachment = file
148
+ attachment.owner = self
149
+ attachment.role = name
150
+ attachment.processed = false
145
151
  elsif file
146
152
  attachment = Attachment.new({:owner => self, :role => name}.merge(options))
147
153
  case file
148
154
  when ActionDispatch::Http::UploadedFile
149
- attachment.binary = file.tempfile.read
155
+ attachment.binary = file.tempfile
150
156
  attachment.file_name = file.original_filename
151
157
  attachment.file_type = file.content_type
152
158
  when Attach::File
@@ -1,3 +1,3 @@
1
1
  module Attach
2
- VERSION = '1.0.5'
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attach
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-28 00:00:00.000000000 Z
11
+ date: 2020-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: records_manipulator
@@ -69,8 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubyforge_project:
73
- rubygems_version: 2.5.2.3
72
+ rubygems_version: 3.0.3
74
73
  signing_key:
75
74
  specification_version: 4
76
75
  summary: Attach documents & files to Active Record models