Class: Refile::Backend::FileSystem
- Inherits:
-
Object
- Object
- Refile::Backend::FileSystem
- Defined in:
- lib/refile/backend/file_system.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #clear!(confirm = nil) ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
- #get(id) ⇒ Object
-
#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #open(id) ⇒ Object
- #path(id) ⇒ Object
- #read(id) ⇒ Object
- #size(id) ⇒ Object
- #upload(uploadable) ⇒ Object
Constructor Details
#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem
Returns a new instance of FileSystem.
6 7 8 9 10 11 12 |
# File 'lib/refile/backend/file_system.rb', line 6 def initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) @hasher = hasher @directory = directory @max_size = max_size FileUtils.mkdir_p(@directory) end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
4 5 6 |
# File 'lib/refile/backend/file_system.rb', line 4 def directory @directory end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
4 5 6 |
# File 'lib/refile/backend/file_system.rb', line 4 def max_size @max_size end |
Instance Method Details
#clear!(confirm = nil) ⇒ Object
47 48 49 50 51 |
# File 'lib/refile/backend/file_system.rb', line 47 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm FileUtils.rm_rf(@directory) FileUtils.mkdir_p(@directory) end |
#delete(id) ⇒ Object
27 28 29 |
# File 'lib/refile/backend/file_system.rb', line 27 def delete(id) FileUtils.rm(path(id)) if exists?(id) end |
#exists?(id) ⇒ Boolean
43 44 45 |
# File 'lib/refile/backend/file_system.rb', line 43 def exists?(id) ::File.exist?(path(id)) end |
#get(id) ⇒ Object
23 24 25 |
# File 'lib/refile/backend/file_system.rb', line 23 def get(id) Refile::File.new(self, id) end |
#open(id) ⇒ Object
31 32 33 |
# File 'lib/refile/backend/file_system.rb', line 31 def open(id) ::File.open(path(id), "rb") end |
#path(id) ⇒ Object
53 54 55 |
# File 'lib/refile/backend/file_system.rb', line 53 def path(id) ::File.join(@directory, id) end |
#read(id) ⇒ Object
35 36 37 |
# File 'lib/refile/backend/file_system.rb', line 35 def read(id) ::File.read(path(id)) if exists?(id) end |
#size(id) ⇒ Object
39 40 41 |
# File 'lib/refile/backend/file_system.rb', line 39 def size(id) ::File.size(path(id)) if exists?(id) end |
#upload(uploadable) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/refile/backend/file_system.rb', line 14 def upload(uploadable) Refile.verify_uploadable(uploadable, @max_size) id = @hasher.hash(uploadable) IO.copy_stream(uploadable, path(id)) Refile::File.new(self, id) end |