Class: FilesystemStorage

Inherits:
Object
  • Object
show all
Defined in:
app/models/filesystem_storage.rb

Overview

A storage for file on the local filesystem under a specific path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, public_path = nil) ⇒ FilesystemStorage

Creates a new storage located at the given path. If this path happens to be publically available via HTTP you can provide that path as well so that access the files does not have to go through rails.


11
12
13
14
# File 'app/models/filesystem_storage.rb', line 11

def initialize(path, public_path=nil)
  @path = Pathname.new path
  @public_path = Pathname.new public_path if public_path
end

Instance Attribute Details

#pathObject

The root path of the current FileSystem storage


5
6
7
# File 'app/models/filesystem_storage.rb', line 5

def path
  @path
end

Instance Method Details

#[](key) ⇒ Object

Will retreive the UploadedFile object for the given key


17
18
19
20
21
# File 'app/models/filesystem_storage.rb', line 17

def [](key)
  path = @path.join key
  public_path = @public_path.join key if @public_path
  UploadedFile.new(key, nil, public_path) {File.open path} if path.file?
end

#[]=(key, file) ⇒ Object

Will store the given file under the given key. If the file responds to :read then we assume a stream. Otherwise we convert to string and assume a file path


26
27
28
29
30
31
32
# File 'app/models/filesystem_storage.rb', line 26

def []=(key, file)
  file = File.open(file.to_s) unless file.respond_to? :read
  path = @path.join key
  FileUtils.mkpath path.dirname
  File.open(path, 'wb') {|f| f << file.read}
  self[key]
end

#destroy(key) ⇒ Object

Will remove a specific key from the database. Can point to an UploadedFile or just a directory. If called on a file that does not exist then this is a no-op.


65
66
67
68
# File 'app/models/filesystem_storage.rb', line 65

def destroy(key)
  path = @path.join key
  FileUtils.rm_rf path if path.exist?
end

#dirs(pattern) ⇒ Object

Works like glob but will only return directories that match.

Note that the storage device does not really need to support directories. It could simulate it by quering the keys which is why this is handled somewhat differently. It allows the backend to optimize the directory pull (v.s. have the frontend query based on keys which could be inefficient).


49
50
51
52
53
# File 'app/models/filesystem_storage.rb', line 49

def dirs(pattern)
  Pathname.glob(@path.join(pattern)).sort.collect do |match|
    path2key match if match.directory?
  end.compact
end

#glob(pattern) ⇒ Object

Will file all UploadFile objects that match the given glob pattern


56
57
58
59
60
# File 'app/models/filesystem_storage.rb', line 56

def glob(pattern)
  Pathname.glob(@path.join(pattern)).sort.collect do |match|
    self[path2key match]
  end.compact
end

#mkdir(path) ⇒ Object

Like []= only creates a directory instead of a file. In stores that don't really support directories it might need to create a dummy hidden file to simulate a directory.


37
38
39
40
# File 'app/models/filesystem_storage.rb', line 37

def mkdir(path)
  path = @path.join path
  FileUtils.mkpath path
end