Module: Shrine::Plugins::Mirroring::FileMethods

Defined in:
lib/shrine/plugins/mirroring.rb

Instance Method Summary collapse

Instance Method Details

#delete(mirror: true) ⇒ Object

Mirrors delete to other mirror storages.


98
99
100
101
102
# File 'lib/shrine/plugins/mirroring.rb', line 98

def delete(mirror: true)
  result = super()
  trigger_mirror_delete if mirror
  result
end

#mirror_deleteObject

Deletes the file from each mirror storage.


124
125
126
127
128
# File 'lib/shrine/plugins/mirroring.rb', line 124

def mirror_delete
  each_mirror do |mirror|
    self.class.new(id: id, storage: mirror).delete
  end
end

#mirror_delete_backgroundObject

Calls mirror delete block.


117
118
119
120
121
# File 'lib/shrine/plugins/mirroring.rb', line 117

def mirror_delete_background
  fail Error, "mirror delete block is not registered" unless shrine_class.mirror_delete_block

  shrine_class.mirror_delete_block.call(self)
end

#mirror_upload(**options) ⇒ Object

Uploads the file to each mirror storage.


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shrine/plugins/mirroring.rb', line 82

def mirror_upload(**options)
  previously_opened = opened?

  each_mirror do |mirror|
    rewind if opened?

    shrine_class.upload(self, mirror, **options, location: id, close: false, action: :mirror)
  end
ensure
  if opened? && !previously_opened
    close
    @io = nil
  end
end

#mirror_upload_background(**options) ⇒ Object

Calls mirror upload block.


75
76
77
78
79
# File 'lib/shrine/plugins/mirroring.rb', line 75

def mirror_upload_background(**options)
  fail Error, "mirror upload block is not registered" unless shrine_class.mirror_upload_block

  shrine_class.mirror_upload_block.call(self, **options)
end

#trigger_mirror_deleteObject

Mirrors delete if mirrors are defined. Calls mirror block if registered, otherwise mirrors synchronously.


106
107
108
109
110
111
112
113
114
# File 'lib/shrine/plugins/mirroring.rb', line 106

def trigger_mirror_delete
  return unless shrine_class.mirrors[storage_key] && shrine_class.mirror_delete?

  if shrine_class.mirror_delete_block
    mirror_delete_background
  else
    mirror_delete
  end
end

#trigger_mirror_upload(**options) ⇒ Object

Mirrors upload if mirrors are defined. Calls mirror block if registered, otherwise mirrors synchronously.


64
65
66
67
68
69
70
71
72
# File 'lib/shrine/plugins/mirroring.rb', line 64

def trigger_mirror_upload(**options)
  return unless shrine_class.mirrors[storage_key] && shrine_class.mirror_upload?

  if shrine_class.mirror_upload_block
    mirror_upload_background(**options)
  else
    mirror_upload(**options)
  end
end