Class: FileStore::WebDAV::WebDAVStore

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/filestore/webdav_store.rb

Overview

Main library class implementing a WebDAV file store used for storing and managing arbitrary files

Instance Attribute Summary collapse

Attributes included from Logger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(host, port, user, password, root_path = '/', logger = StdoutLogger) ⇒ WebDAVStore

Initializes a new instance of SimpleFileStore

Arguments: root_path: The path where the file store resides

logger: The logging facility


25
26
27
28
29
30
31
32
33
# File 'lib/filestore/webdav_store.rb', line 25

def initialize(host, port, user, password, root_path = '/', logger = StdoutLogger)
  @host = host
  @port = port
  @user = user
  @password = password
  @root_path = root_path
  
  initialize_connection
end

Instance Attribute Details

#connectionObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def connection
  @connection
end

#hostObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def host
  @host
end

#passwordObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def password
  @password
end

#portObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def port
  @port
end

#root_pathObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def root_path
  @root_path
end

#userObject (readonly)

Accessors for important properties



17
18
19
# File 'lib/filestore/webdav_store.rb', line 17

def user
  @user
end

Instance Method Details

#add(file) ⇒ Object

Adds a file to the store

Arguments: file: The file to be stored

Returns:

The file path or nil

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/filestore/webdav_store.rb', line 43

def add(file)
  raise FileStoreException, "File #{file} not found" if not File.exists?(file)
  raise FileStoreException, "File #{file} isn't readable" if not File.readable?(file)
  
  f_path = File.basename file
  remote_path = File.join @root_path, f_path
  
  begin
    result = @connection.put remote_path, file

    raise "Couldn't upload file, response code isn't of type 2xx" unless (result[:header].status.to_s =~ /2\d{2}/)
    
    inform ObserverAction.new(:type => ObserverAction::TYPE_DAV_ADD, 
        :objects => { :file => remote_path, :meta => meta }, :msg => "Added file to remote file store") if self.is_a?(ObservedSubject)
  rescue Exception => e
    raise FileStoreException, "Couldn't put file '#{file}' to remote store URL '#{remote_path}'.\n#{e.backtrace.join("\n")}"
  end
  
  return remote_path
end

#get(file) ⇒ Object

Retrieves a file identified by it’s path

Arguments: file: The file path to get from the remote store

Returns: A hash of file object (:path) and corresponding meta data (:data) representing the file in the store

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/filestore/webdav_store.rb', line 73

def get(file)
  raise FileStoreException, "No file given" if file.nil? or file == ''
  
  # Retrieve data from the store
  result = @connection.get file
  
  unless result[:body].nil?
      inform ObserverAction.new(:type => ObserverAction::TYPE_DAV_GET, 
        :objects => [file], :msg => "Returning file from the remote store")  if self.is_a?(ObservedSubject)
        
      return { :path => file, :data => result[:body] }
  else
    return {}
  end
  
end

#remove(file) ⇒ Object

Deletes a file from the remote store

Arguments: file: The remote file path

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/filestore/webdav_store.rb', line 95

def remove(file)
  raise FileStoreException, "No file given for removal" if (file == '' or file.nil?)
  
  begin
    result = @connection.delete file
    
    if result[:header].status.to_s =~ /2\d{2}/
        inform ObserverAction.new(:type => ObserverAction::TYPE_DAV_REMOVE, 
          :objects => [id], :msg => "Deleted file from remote store") if self.is_a?(ObservedSubject)
      else
        raise "Response code isn't of class 2xx"
      end
  rescue Exception => e
    raise FileStoreException, "Couldn't remove file '#{file}' from remote store.\n#{e.message}"
  end
end

#shutdownObject

Shuts down the file store



114
115
116
117
118
119
# File 'lib/filestore/webdav_store.rb', line 114

def shutdown
  @connection.close
  
  inform ObserverAction.new(:type => ObserverAction::TYPE_DAV_SHUTDOWN, 
      :msg => "WebDAV store shutdown")  if self.is_a?(ObservedSubject)
end