Class: Refile::Backend::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/backend/file_system.rb

Overview

A backend which stores uploaded files in the local filesystem

Examples:

backend = Refile::Backend::FileSystem.new("some/path")
file = backend.upload(StringIO.new("hello"))
backend.read(file.id) # => "hello"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new) ⇒ FileSystem

Creates the given directory if it doesn't exist.

Parameters:

  • directory (String)

    The path to a directory where files should be stored

  • max_size (Integer, nil) (defaults to: nil)

    The maximum size of an uploaded file

  • hasher (#hash) (defaults to: Refile::RandomHasher.new)

    A hasher which is used to generate ids from files


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

#directoryString (readonly)

Returns the directory where files are stored.

Returns:

  • (String)

    the directory where files are stored


13
14
15
# File 'lib/refile/backend/file_system.rb', line 13

def directory
  @directory
end

#max_sizeString (readonly)

Returns the maximum size of files stored in this backend.

Returns:

  • (String)

    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)

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.

Examples:

backend.clear!(:confirm)

Parameters:

  • confirm (:confirm) (defaults to: nil)

    Pass the symbol :confirm to confirm deletion.

Raises:

  • (Refile::Confirm)

    Unless the :confirm symbol has been passed.


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

This method returns an undefined value.

Delete a file from this backend

Parameters:

  • id (Sring)

    The id of the file


58
59
60
# File 'lib/refile/backend/file_system.rb', line 58

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.

Parameters:

  • id (Sring)

    The id of the file

Returns:

  • (Boolean)

91
92
93
# File 'lib/refile/backend/file_system.rb', line 91

verify_id def exists?(id)
  ::File.exist?(path(id))
end

#getRefile::File

Get a file from this backend.

Note that this method will always return a File object, even if a file with the given id does not exist in this backend. Use #exists? to check if the file actually exists.

Parameters:

  • id (Sring)

    The id of the file

Returns:


50
51
52
# File 'lib/refile/backend/file_system.rb', line 50

verify_id def get(id)
  Refile::File.new(self, id)
end

#openIO

Return an IO object for the uploaded file which can be used to read its content.

Parameters:

  • id (Sring)

    The id of the file

Returns:

  • (IO)

    An IO object containing the file contents


67
68
69
# File 'lib/refile/backend/file_system.rb', line 67

verify_id def open(id)
  ::File.open(path(id), "rb")
end

#pathString

Return the full path of the uploaded file with the given id.

Parameters:

  • id (Sring)

    The id of the file

Returns:

  • (String)

113
114
115
# File 'lib/refile/backend/file_system.rb', line 113

verify_id def path(id)
  ::File.join(@directory, id)
end

#readString

Return the entire contents of the uploaded file as a String.

Parameters:

  • id (Sring)

    The id of the file

Returns:

  • (String)

    The file's contents


75
76
77
# File 'lib/refile/backend/file_system.rb', line 75

verify_id def read(id)
  ::File.read(path(id)) if exists?(id)
end

#sizeInteger

Return the size in bytes of the uploaded file.

Parameters:

  • id (Sring)

    The id of the file

Returns:

  • (Integer)

    The file's size


83
84
85
# File 'lib/refile/backend/file_system.rb', line 83

verify_id def size(id)
  ::File.size(path(id)) if exists?(id)
end

#uploadRefile::File

Upload a file into this backend

Parameters:

  • uploadable (IO)

    An uploadable IO-like object.

Returns:


35
36
37
38
39
40
# 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)
end