Class: Vidibus::WatchFolder::Base

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Uuid::Mongoid
Defined in:
lib/vidibus/watch_folder/base.rb

Defined Under Namespace

Classes: ConfigError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callback(method, options = {}) ⇒ Object

Define callbacks to perform when files change.

Add filter :when to define events to watch. Supported event types are :added, :modified, an :removed

Add filter :delay to perform callback later. Execution will then be delayed until the watched file will not have been changed for given period of time. This is useful for waiting until an upload is completed. If no delay has been configured, execution will be asynchronous nonetheless.

Set filter :ignore to exclude file names matching given regex.

Provide :folders to limit this callback to certain folders.



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

def callback(method, options = {})
  config[:callback] ||= {}
  opts = {:method => method}
  if events = events_options(options)
    opts[:when] = events
  end
  if delay = delay_options(options)
    opts[:delay] = delay
  end
  if ignore = ignore_options(options)
    opts[:ignore] = ignore
  end
  folders_options(options).each do |folder|
    config[:callback][folder] ||= []
    config[:callback][folder] << opts
  end
end

.configObject

Inheritable getter for config.



114
115
116
# File 'lib/vidibus/watch_folder/base.rb', line 114

def config
  @config ||= {}
end

.config=(value) ⇒ Object

Inheritable setter for config.



119
120
121
# File 'lib/vidibus/watch_folder/base.rb', line 119

def config=(value)
  @config = value
end

.find_by_uuid(uuid) ⇒ Object

Find an instance of this kind of watch folder by its UUID. Will raise an exception if no instance can be found.



125
126
127
128
129
# File 'lib/vidibus/watch_folder/base.rb', line 125

def find_by_uuid(uuid)
  found = where(:uuid => uuid).first || begin
    raise(Mongoid::Errors::DocumentNotFound.new(self, :uuid => uuid))
  end
end

.folders(*args) ⇒ Object

Define folders to create automatically when an instance of this kind of watch folder is created.

Raises:



76
77
78
79
# File 'lib/vidibus/watch_folder/base.rb', line 76

def folders(*args)
  raise ConfigError, 'Define folders' unless args.any?
  config[:folders] = string_list(args)
end

.root(path) ⇒ Object

Set root path of this kind of watch folder.



63
64
65
66
67
68
69
70
71
72
# File 'lib/vidibus/watch_folder/base.rb', line 63

def root(path)
  path = File.expand_path(path)
  unless Util::Directory.valid?(path)
    raise ConfigError, "Given root '#{path}' must be a read and writable folder"
  end
  unless Vidibus::WatchFolder.roots.include?(path)
    Vidibus::WatchFolder.roots << path
  end
  config[:root] = path
end

Instance Method Details

#filesObject

Return a list of file paths within this watch folder.



27
28
29
30
31
# File 'lib/vidibus/watch_folder/base.rb', line 27

def files
  Dir["#{path}/**/*"].reject do |entry|
    File.directory?(entry)
  end
end

#handle(event, file_path, last_checksum = nil) ⇒ Object

Handle event for given file path. Unless a checksum is provided, a asynchronous job will be started. If provided checksum matches the current one (or if execution shall not be delayed), appropriate callbacks will be performed.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vidibus/watch_folder/base.rb', line 37

def handle(event, file_path, last_checksum = nil)
  return unless File.exist?(file_path) && !File.directory?(file_path)
  callbacks = self.class.config[:callback]
  callbacks.each do |folder, handlers|
    unless folder == :any
      pattern = %r(^#{path}/#{folder}/.+$)
      next unless file_path[pattern]
    end
    matching = handlers.select { |c| c[:when].include?(event) }
    matching.each do |handler|
      next if handler[:ignore] && file_path.match(handler[:ignore])
      checksum ||= Vidibus::WatchFolder.checksum(file_path)
      delay = handler[:delay]
      if checksum == last_checksum || (last_checksum && !delay)
        send(handler[:method], event, file_path)
      else
        Job.delete_all(uuid, event, file_path)
        Job.create(uuid, event, file_path, checksum, delay)
      end
    end
  end
end

#pathObject

Return the absolute path to this watch folder.



22
23
24
# File 'lib/vidibus/watch_folder/base.rb', line 22

def path
  File.join(root, uuid) if uuid
end

#rootObject

Return the configured root path



17
18
19
# File 'lib/vidibus/watch_folder/base.rb', line 17

def root
  self.class.config[:root] || raise(ConfigError, 'No root configured')
end