Class: Valkyrie::Storage::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/storage/disk.rb

Defined Under Namespace

Classes: BucketedStorage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path:, path_generator: BucketedStorage, file_mover: FileUtils.method(:mv)) ⇒ Disk

Returns a new instance of Disk.


5
6
7
8
9
# File 'lib/valkyrie/storage/disk.rb', line 5

def initialize(base_path:, path_generator: BucketedStorage, file_mover: FileUtils.method(:mv))
  @base_path = Pathname.new(base_path.to_s)
  @path_generator = path_generator.new(base_path: base_path)
  @file_mover = file_mover
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path


4
5
6
# File 'lib/valkyrie/storage/disk.rb', line 4

def base_path
  @base_path
end

#file_moverObject (readonly)

Returns the value of attribute file_mover


4
5
6
# File 'lib/valkyrie/storage/disk.rb', line 4

def file_mover
  @file_mover
end

#path_generatorObject (readonly)

Returns the value of attribute path_generator


4
5
6
# File 'lib/valkyrie/storage/disk.rb', line 4

def path_generator
  @path_generator
end

Instance Method Details

#delete(id:) ⇒ Object

Delete the file on disk associated with the given identifier.

Parameters:


44
45
46
47
# File 'lib/valkyrie/storage/disk.rb', line 44

def delete(id:)
  path = file_path(id)
  FileUtils.rm_rf(path) if File.exist?(path)
end

#file_path(id) ⇒ Object


28
29
30
# File 'lib/valkyrie/storage/disk.rb', line 28

def file_path(id)
  id.to_s.gsub(/^disk:\/\//, '')
end

#find_by(id:) ⇒ Valkyrie::StorageAdapter::File

Return the file associated with the given identifier

Parameters:

Returns:

Raises:

  • Valkyrie::StorageAdapter::FileNotFound if nothing is found


36
37
38
39
40
# File 'lib/valkyrie/storage/disk.rb', line 36

def find_by(id:)
  Valkyrie::StorageAdapter::File.new(id: Valkyrie::ID.new(id.to_s), io: ::File.open(file_path(id), 'rb'))
rescue Errno::ENOENT
  raise Valkyrie::StorageAdapter::FileNotFound
end

#handles?(id:) ⇒ Boolean

Returns true if this adapter can handle this type of identifer.

Parameters:

Returns:

  • (Boolean)

    true if this adapter can handle this type of identifer


24
25
26
# File 'lib/valkyrie/storage/disk.rb', line 24

def handles?(id:)
  id.to_s.start_with?("disk://")
end

#upload(file:, original_filename:, resource: nil) ⇒ Valkyrie::StorageAdapter::File

Parameters:

Returns:


15
16
17
18
19
20
# File 'lib/valkyrie/storage/disk.rb', line 15

def upload(file:, original_filename:, resource: nil)
  new_path = path_generator.generate(resource: resource, file: file, original_filename: original_filename)
  FileUtils.mkdir_p(new_path.parent)
  file_mover.call(file.path, new_path)
  find_by(id: Valkyrie::ID.new("disk://#{new_path}"))
end