Class: Filestorage::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/filestorage/local.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ Local

Returns a new instance of Local.



10
11
12
# File 'lib/filestorage/local.rb', line 10

def initialize(base_dir)
  @base_dir = Pathname.new(base_dir)
end

Instance Method Details

#delete(path) ⇒ Object

Raises:



38
39
40
41
42
43
# File 'lib/filestorage/local.rb', line 38

def delete(path)
  fullpath = @base_dir + path
  raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
  FileUtils.rm(fullpath)
  path
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/filestorage/local.rb', line 45

def exist?(path)
  fullpath = @base_dir + path
  File.exist?(fullpath)
end

#get(path) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/filestorage/local.rb', line 32

def get(path)
  fullpath = @base_dir + path
  raise NotExist.new("Not exist #{path}") unless File.exist?(fullpath)
  File.open(fullpath, "rb")
end

#store(path, file) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/filestorage/local.rb', line 14

def store(path, file)
  fullpath = @base_dir + path
  raise AlreadyExist.new("Already exist #{path}") if File.exist?(fullpath)
  FileUtils.mkdir_p(fullpath.parent)
  if file.instance_of?(Pathname)
    FileUtils.cp(file, fullpath)
  elsif file.instance_of?(File)
    File.open(fullpath, "wb") do |f|
      f.write(file.read)
    end
  else
    File.open(fullpath, "wb") do |f|
      f.write(file)
    end
  end
  path
end