Module: RailsCom::AttachmentTransfer

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails_com/active_storage/attachment_transfer.rb

Instance Method Summary collapse

Instance Method Details

#copyObject



10
11
12
13
14
15
16
17
18
# File 'lib/rails_com/active_storage/attachment_transfer.rb', line 10

def copy
  raise 'Only Support mirror service' unless service.is_a?(ActiveStorage::Service::MirrorService)
  blob.open do |io|
    checksum = blob.send(:compute_checksum_in_chunks, io)
    service.mirrors.map do |service|
      service.upload key, io.tap(&:rewind), checksum: checksum
    end
  end
end

#ffmpeg_pathObject



43
44
45
# File 'lib/rails_com/active_storage/attachment_transfer.rb', line 43

def ffmpeg_path
  ActiveStorage.paths[:ffmpeg] || 'ffmpeg'
end

#transfer_faststartObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_com/active_storage/attachment_transfer.rb', line 20

def transfer_faststart
  attach = self.record.send(self.name)
  r = nil
  blob.open do |input|
    Tempfile.open([ 'ActiveStorage', self.filename.extension_with_delimiter ], Dir.tmpdir) do |file|
      file.binmode
      argv = [ffmpeg_path, '-i', input.path, '-codec', 'copy', '-movflags', 'faststart', '-f', 'mp4', '-y', file.path]
      system(*argv)
      file.rewind
      r = attach.attach io: file, filename: self.filename.to_s, content_type: 'video/mp4'
    end
  end
  self.purge

  if attach.is_a?(ActiveStorage::Attached::One)
    r
  elsif attach.ia_a?(ActiveStorage::Attached::Many)
    r[0]
  else
    r
  end
end