Class: Saviour::LocalStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/local_storage.rb

Constant Summary collapse

MissingPublicUrlPrefix =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ LocalStorage

Returns a new instance of LocalStorage.



7
8
9
10
11
# File 'lib/saviour/local_storage.rb', line 7

def initialize(opts = {})
  @local_prefix = opts[:local_prefix]
  @public_url_prefix = opts[:public_url_prefix]
  @permissions = opts.fetch(:permissions, 0644)
end

Instance Method Details

#cp(source_path, destination_path) ⇒ Object



54
55
56
57
58
# File 'lib/saviour/local_storage.rb', line 54

def cp(source_path, destination_path)
  FileUtils.cp(real_path(source_path), real_path(destination_path))
rescue Errno::ENOENT
  raise FileNotPresent, "Trying to cp an unexisting path: #{source_path}"
end

#delete(path) ⇒ Object



38
39
40
41
42
43
# File 'lib/saviour/local_storage.rb', line 38

def delete(path)
  ::File.delete(real_path(path))
  ensure_removed_empty_dir(path)
rescue Errno::ENOENT
  raise FileNotPresent, "Trying to delete an unexisting path: #{path}"
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/saviour/local_storage.rb', line 45

def exists?(path)
  ::File.file?(real_path(path))
end

#mv(source_path, destination_path) ⇒ Object



60
61
62
63
64
# File 'lib/saviour/local_storage.rb', line 60

def mv(source_path, destination_path)
  FileUtils.mv(real_path(source_path), real_path(destination_path))
rescue Errno::ENOENT
  raise FileNotPresent, "Trying to mv an unexisting path: #{source_path}"
end

#public_url(path) ⇒ Object



49
50
51
52
# File 'lib/saviour/local_storage.rb', line 49

def public_url(path)
  raise(MissingPublicUrlPrefix, "You must provide a `public_url_prefix`") unless public_url_prefix
  ::File.join(public_url_prefix, path)
end

#read(path) ⇒ Object



32
33
34
35
36
# File 'lib/saviour/local_storage.rb', line 32

def read(path)
  ::File.open(real_path(path)).read
rescue Errno::ENOENT
  raise FileNotPresent, "Trying to read an unexisting path: #{path}"
end

#read_to_file(path, dest_file) ⇒ Object



28
29
30
# File 'lib/saviour/local_storage.rb', line 28

def read_to_file(path, dest_file)
  FileUtils.cp real_path(path), dest_file.path
end

#write(contents, path) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/saviour/local_storage.rb', line 13

def write(contents, path)
  ensure_dir!(path)

  ::File.write real_path(path), contents, mode: "wb"

  ensure_file_permissions!(path)
end

#write_from_file(file, path) ⇒ Object



21
22
23
24
25
26
# File 'lib/saviour/local_storage.rb', line 21

def write_from_file(file, path)
  ensure_dir!(path)

  FileUtils.cp file.path, real_path(path)
  ensure_file_permissions!(path)
end