Class: PackageFileGetter

Inherits:
Object
  • Object
show all
Defined in:
lib/package_file_getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(package, storage: Services.storage) ⇒ PackageFileGetter

Returns a new instance of PackageFileGetter


6
7
8
9
# File 'lib/package_file_getter.rb', line 6

def initialize(package, storage: Services.storage)
  raise "package must be stored" unless package.stored?
  @bag = storage.new(package.storage_location)
end

Instance Method Details

#filesObject


11
12
13
14
# File 'lib/package_file_getter.rb', line 11

def files
  data_path = Pathname.new(bag.data_dir)
  bag.bag_files.map {|f| Pathname.new(f).relative_path_from(data_path) }
end

#sendfile(file) ⇒ Object

Raises:


16
17
18
19
20
21
# File 'lib/package_file_getter.rb', line 16

def sendfile(file)
  raise FileNotFoundError, "No such file #{file} in bag" unless files.include?(Pathname.new(file))

  [File.join(bag.data_dir, file),
   type: Rack::Mime.mime_type(File.extname(file))]
end