Class: Valkyrie::Storage::Disk
- Inherits:
-
Object
- Object
- Valkyrie::Storage::Disk
- Defined in:
- lib/valkyrie/storage/disk.rb
Overview
Implements the DataMapper Pattern to store binary data on disk
Defined Under Namespace
Classes: BucketedStorage, LazyFile
Constant Summary collapse
- PROTOCOL =
'disk://'
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#file_mover ⇒ Object
readonly
Returns the value of attribute file_mover.
-
#path_generator ⇒ Object
readonly
Returns the value of attribute path_generator.
Instance Method Summary collapse
-
#delete(id:) ⇒ Object
Delete the file on disk associated with the given identifier.
- #file_path(id) ⇒ Object
-
#find_by(id:) ⇒ Valkyrie::StorageAdapter::File
Return the file associated with the given identifier.
-
#handles?(id:) ⇒ Boolean
True if this adapter can handle this type of identifer.
-
#initialize(base_path:, path_generator: BucketedStorage, file_mover: FileUtils.method(:mv)) ⇒ Disk
constructor
A new instance of Disk.
-
#protocol ⇒ String
Identifier prefix.
-
#supports?(_feature) ⇒ Boolean
True if the adapter supports the given feature.
- #upload(file:, original_filename:, resource: nil, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::File
Constructor Details
#initialize(base_path:, path_generator: BucketedStorage, file_mover: FileUtils.method(:mv)) ⇒ Disk
Returns a new instance of Disk.
8 9 10 11 12 |
# File 'lib/valkyrie/storage/disk.rb', line 8 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_path ⇒ Object (readonly)
Returns the value of attribute base_path.
5 6 7 |
# File 'lib/valkyrie/storage/disk.rb', line 5 def base_path @base_path end |
#file_mover ⇒ Object (readonly)
Returns the value of attribute file_mover.
5 6 7 |
# File 'lib/valkyrie/storage/disk.rb', line 5 def file_mover @file_mover end |
#path_generator ⇒ Object (readonly)
Returns the value of attribute path_generator.
5 6 7 |
# File 'lib/valkyrie/storage/disk.rb', line 5 def path_generator @path_generator end |
Instance Method Details
#delete(id:) ⇒ Object
Delete the file on disk associated with the given identifier.
82 83 84 85 |
# File 'lib/valkyrie/storage/disk.rb', line 82 def delete(id:) path = file_path(id) FileUtils.rm_rf(path) if File.exist?(path) end |
#file_path(id) ⇒ Object
43 44 45 |
# File 'lib/valkyrie/storage/disk.rb', line 43 def file_path(id) id.to_s.gsub(/^#{Regexp.escape(protocol)}/, '') end |
#find_by(id:) ⇒ Valkyrie::StorageAdapter::File
Return the file associated with the given identifier
51 52 53 54 55 |
# File 'lib/valkyrie/storage/disk.rb', line 51 def find_by(id:) Valkyrie::StorageAdapter::File.new(id: Valkyrie::ID.new(id.to_s), io: LazyFile.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.
28 29 30 |
# File 'lib/valkyrie/storage/disk.rb', line 28 def handles?(id:) id.to_s.start_with?("#{protocol}#{base_path}") end |
#protocol ⇒ String
Returns identifier prefix.
39 40 41 |
# File 'lib/valkyrie/storage/disk.rb', line 39 def protocol PROTOCOL end |
#supports?(_feature) ⇒ Boolean
Returns true if the adapter supports the given feature.
34 35 36 |
# File 'lib/valkyrie/storage/disk.rb', line 34 def supports?(_feature) false end |
#upload(file:, original_filename:, resource: nil, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::File
19 20 21 22 23 24 |
# File 'lib/valkyrie/storage/disk.rb', line 19 def upload(file:, original_filename:, resource: nil, **_extra_arguments) 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("#{protocol}#{new_path}")) end |