Class: Refile::Backend::FileSystem
- Inherits:
-
Object
- Object
- Refile::Backend::FileSystem
- Extended by:
- Refile::BackendMacros
- 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) ⇒ void
Remove all files in this backend.
-
#delete ⇒ void
Delete a file from this backend.
-
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
-
#get ⇒ 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 ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
-
#path ⇒ String
Return the full path of the uploaded file with the given id.
-
#read ⇒ String
Return the entire contents of the uploaded file as a String.
-
#size ⇒ Integer
Return the size in bytes of the uploaded file.
-
#upload ⇒ Refile::File
Upload a file into this backend.
Methods included from Refile::BackendMacros
decode_id, valid_id?, verify_id, verify_uploadable
Constructor Details
#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem
Creates the given directory if it doesn’t exist.
23 24 25 26 27 28 29 |
# File 'lib/refile/backend/file_system.rb', line 23 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.
13 14 15 |
# File 'lib/refile/backend/file_system.rb', line 13 def directory @directory end |
#max_size ⇒ String (readonly)
Returns the maximum size of files stored in this backend.
16 17 18 |
# File 'lib/refile/backend/file_system.rb', line 16 def max_size @max_size end |
Instance Method Details
#clear!(confirm = nil) ⇒ void
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.
105 106 107 108 109 |
# File 'lib/refile/backend/file_system.rb', line 105 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm FileUtils.rm_rf(@directory) FileUtils.mkdir_p(@directory) end |
#delete ⇒ void
This method returns an undefined value.
Delete a file from this backend
60 61 62 |
# File 'lib/refile/backend/file_system.rb', line 60 verify_id def delete(id) FileUtils.rm(path(id)) if exists?(id) end |
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
93 94 95 |
# File 'lib/refile/backend/file_system.rb', line 93 verify_id def exists?(id) ::File.exist?(path(id)) end |
#get ⇒ Refile::File
52 53 54 |
# File 'lib/refile/backend/file_system.rb', line 52 verify_id def get(id) Refile::File.new(self, id) end |
#open ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
69 70 71 |
# File 'lib/refile/backend/file_system.rb', line 69 verify_id def open(id) ::File.open(path(id), "rb") end |
#path ⇒ String
Return the full path of the uploaded file with the given id.
115 116 117 |
# File 'lib/refile/backend/file_system.rb', line 115 verify_id def path(id) ::File.join(@directory, id) end |
#read ⇒ String
Return the entire contents of the uploaded file as a String.
77 78 79 |
# File 'lib/refile/backend/file_system.rb', line 77 verify_id def read(id) ::File.read(path(id)) if exists?(id) end |
#size ⇒ Integer
Return the size in bytes of the uploaded file.
85 86 87 |
# File 'lib/refile/backend/file_system.rb', line 85 verify_id def size(id) ::File.size(path(id)) if exists?(id) end |
#upload ⇒ Refile::File
Upload a file into this backend
35 36 37 38 39 40 41 42 |
# File 'lib/refile/backend/file_system.rb', line 35 verify_uploadable def upload(uploadable) id = @hasher.hash(uploadable) IO.copy_stream(uploadable, path(id)) Refile::File.new(self, id) ensure uploadable.close end |