Class: StandardStorage::Filesystem

Inherits:
StandardStorage show all
Defined in:
lib/standard_storage/filesystem.rb

Constant Summary

Constants inherited from StandardStorage

VERSION

Instance Attribute Summary collapse

Attributes inherited from StandardStorage

#partition_depth

Instance Method Summary collapse

Methods inherited from StandardStorage

#copy_to_tempfile

Constructor Details

#initialize(configs = {}) ⇒ Filesystem

Returns a new instance of Filesystem.



8
9
10
11
12
13
# File 'lib/standard_storage/filesystem.rb', line 8

def initialize(configs={})
  super
  @host = configs[:host]
  @path = configs[:path]
  @prefix = configs[:prefix]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/standard_storage/filesystem.rb', line 6

def host
  @host
end

#path(key) ⇒ Object (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/standard_storage/filesystem.rb', line 6

def path
  @path
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/standard_storage/filesystem.rb', line 6

def prefix
  @prefix
end

Instance Method Details

#cp(source, destination) ⇒ Object



41
42
43
44
# File 'lib/standard_storage/filesystem.rb', line 41

def cp(source, destination)
  source = destination(source)
  FileUtils.cp(source, destination)
end

#delete(key) ⇒ Object



50
51
52
# File 'lib/standard_storage/filesystem.rb', line 50

def delete(key)
  FileUtils.rm(destination(key), force: true)
end

#destination(key) ⇒ Object



27
28
29
# File 'lib/standard_storage/filesystem.rb', line 27

def destination(key)
  File.join([@path, partition(key)].compact)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/standard_storage/filesystem.rb', line 31

def exists?(key)
  File.exist?(destination(key))
end

#last_modified(key) ⇒ Object



54
55
56
# File 'lib/standard_storage/filesystem.rb', line 54

def last_modified(key)
  File.mtime(destination(key))
end

#local?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/standard_storage/filesystem.rb', line 15

def local?
  true
end

#mime_type(key) ⇒ Object



58
59
60
61
# File 'lib/standard_storage/filesystem.rb', line 58

def mime_type(key)
  command = Terrapin::CommandLine.new("file", '-b --mime-type :file')
  command.run({ file: destination(key) }).strip
end

#read(key, &block) ⇒ Object



46
47
48
# File 'lib/standard_storage/filesystem.rb', line 46

def read(key, &block)
  File.read(destination(key), &block)
end

#url(key) ⇒ Object



19
20
21
# File 'lib/standard_storage/filesystem.rb', line 19

def url(key)
  File.join([@host, path(key)].compact)
end

#write(key, file, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/standard_storage/filesystem.rb', line 35

def write(key, file, options={})
  key = destination(key)
  FileUtils.mkdir_p(File.dirname(key))
  FileUtils.cp(file.path, key)
end