Class: Refile::Backend::FileSystem
- Inherits:
-
Object
- Object
- Refile::Backend::FileSystem
- Defined in:
- lib/refile/backend/file_system.rb
Overview
A backend which stores uploaded files in the local filesystem
Instance Attribute Summary collapse
-
#directory ⇒ String
readonly
The directory where files are stored.
-
#max_size ⇒ String
readonly
The maximum size of files stored in this backend.
Instance Method Summary collapse
-
#clear!(confirm = nil)
Remove all files in this backend.
-
#delete(id)
Delete a file from this backend.
-
#exists?(id) ⇒ Boolean
Return whether the file with the given id exists in this backend.
-
#get(id) ⇒ Refile::File
Get a file from this backend.
-
#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem
constructor
Creates the given directory if it doesn't exist.
-
#open(id) ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
-
#path(id) ⇒ String
Return the full path of the uploaded file with the given id.
-
#read(id) ⇒ String
Return the entire contents of the uploaded file as a String.
-
#size(id) ⇒ Integer
Return the size in bytes of the uploaded file.
-
#upload(uploadable) ⇒ Refile::File
Upload a file into this backend.
Constructor Details
#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem
Creates the given directory if it doesn't exist.
21 22 23 24 25 26 27 |
# File 'lib/refile/backend/file_system.rb', line 21 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 ⇒ String (readonly)
Returns the directory where files are stored.
11 12 13 |
# File 'lib/refile/backend/file_system.rb', line 11 def directory @directory end |
#max_size ⇒ String (readonly)
Returns the maximum size of files stored in this backend.
14 15 16 |
# File 'lib/refile/backend/file_system.rb', line 14 def max_size @max_size end |
Instance Method Details
#clear!(confirm = nil)
This method returns an undefined value.
Remove all files in this backend. You must confirm the deletion by
passing the symbol :confirm
as an argument to this method.
103 104 105 106 107 |
# File 'lib/refile/backend/file_system.rb', line 103 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm FileUtils.rm_rf(@directory) FileUtils.mkdir_p(@directory) end |
#delete(id)
This method returns an undefined value.
Delete a file from this backend
58 59 60 |
# File 'lib/refile/backend/file_system.rb', line 58 def delete(id) FileUtils.rm(path(id)) if exists?(id) end |
#exists?(id) ⇒ Boolean
Return whether the file with the given id exists in this backend.
91 92 93 |
# File 'lib/refile/backend/file_system.rb', line 91 def exists?(id) ::File.exist?(path(id)) end |
#get(id) ⇒ Refile::File
50 51 52 |
# File 'lib/refile/backend/file_system.rb', line 50 def get(id) Refile::File.new(self, id) end |
#open(id) ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
67 68 69 |
# File 'lib/refile/backend/file_system.rb', line 67 def open(id) ::File.open(path(id), "rb") end |
#path(id) ⇒ String
Return the full path of the uploaded file with the given id.
113 114 115 |
# File 'lib/refile/backend/file_system.rb', line 113 def path(id) ::File.join(@directory, id) end |
#read(id) ⇒ String
Return the entire contents of the uploaded file as a String.
75 76 77 |
# File 'lib/refile/backend/file_system.rb', line 75 def read(id) ::File.read(path(id)) if exists?(id) end |
#size(id) ⇒ Integer
Return the size in bytes of the uploaded file.
83 84 85 |
# File 'lib/refile/backend/file_system.rb', line 83 def size(id) ::File.size(path(id)) if exists?(id) end |
#upload(uploadable) ⇒ Refile::File
Upload a file into this backend
33 34 35 36 37 38 39 40 |
# File 'lib/refile/backend/file_system.rb', line 33 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 |