Class: ActiveStorage::Service::DiscService

Inherits:
DiskService
  • Object
show all
Defined in:
lib/active_storage/service/disc_service.rb

Instance Method Summary collapse

Instance Method Details

#download(key, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_storage/service/disc_service.rb', line 15

def download(key, &block)
  path = ActiveStorage::Blob.find_by(key: key)&.filename.to_s

  if block_given?
    instrument :streaming_download, key: key do
      stream path, &block
    end
  else
    instrument :download, key: key do
      File.binread path_for(path)
    rescue Errno::ENOENT
      raise ActiveStorage::FileNotFoundError
    end
  end
end

#make_path_for(path) ⇒ Object



31
32
33
# File 'lib/active_storage/service/disc_service.rb', line 31

def make_path_for(path)
  path_for(path).tap { |p| FileUtils.mkdir_p File.dirname(p) }
end

#path_for(path) ⇒ Object



35
36
37
# File 'lib/active_storage/service/disc_service.rb', line 35

def path_for(path)
  File.join root, path
end

#upload(key, io, checksum: nil, filename: nil) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/active_storage/service/disc_service.rb', line 6

def upload(key, io, checksum: nil, filename: nil, **)
  path = filename.to_s

  instrument :upload, key: key, checksum: checksum do
    IO.copy_stream(io, make_path_for(path)) if path.present?
    ensure_integrity_of(path, checksum) if checksum
  end
end