class MediaOrganizer::Filescanner

Attributes

root_nodes[R]
source_list[RW]

Public Class Methods

new() click to toggle source
# File lib/filescanner.rb, line 16
def initialize
  @root_nodes = []
  @source_list = []
end

Public Instance Methods

add_root(dir_uri) click to toggle source

alternative run mode. Add multiple “root” directories to scan at once

# File lib/filescanner.rb, line 64
def add_root(dir_uri)
  validate_uri(dir_uri)
  @root_nodes << dir_uri
rescue StandardError => e
  puts "Warning: #{e.message}"
  return false
end
multiscan(args = {}) click to toggle source
multiscan(): scans multiple directories added to @root_nodes using the add_root() method.

Inputs

Required

none

Optional

(1) Arguments Hash:
*:mode => (:single, :multiple)
*:music => (true, false) -- if true, music files will be included in the scan.
 Set to false to exclude music files. Defaults to true
*:image => (true, false) -- if true, image files will be included in the scan.
 Set to false to exclude image files. Defaults to true

Outputs

Array of strings, where each string is a file URI for a music or image file.
# File lib/filescanner.rb, line 93
def multiscan(args = {})
  @root_nodes.each do |uri|
    open(uri, args)
  end
  @source_list
end
open(uri = '', args = {}) click to toggle source
Filescanner.open(String, {}): scans directory tree for media files,
starting at String specified in first argument.

Inputs

Required

(1) String: String containing the URI of the top of the directory tree to scan

Optional

(2) Arguments Hash:
*:mode => (:single) -- if set to :single, only the given URI will be scanned.
Subdirectories will be ignored.
*:music => (true, false) -- if true, music files will be included in the scan.
 Set to false to exclude music files. Defaults to true
*:image => (true, false) -- if true, image files will be included in the scan.
 Set to false to exclude image files. Defaults to true

Outputs

Returns array of strings, where each string is a file URI for a music or image file.

Usage Example

Filescanner.open("/absolute/path/for/top/of/directory/tree")
# File lib/filescanner.rb, line 45
def open(uri = '', args = {})
  include_images = true unless args[:image] == false
  include_music = true unless args[:music] == false

  files = load_files(uri, args[:mode])
  files.each do |f|
    if (Music.music?(f) && include_music) || (Image.image?(f) && include_images)
      @source_list << f
    end
  end

  return @source_list

rescue StandardError => e
  puts "Warning: #{e.message}"
  return false
end

Private Instance Methods

load_files(uri, mode) click to toggle source
load_files(): given a URI and optional mode, scans and loads every file in
the specifid folder or tree.

Inputs

Required

(1) String: String containing the URI to validate.

Optional

(1) Arguments Hash:
*:mode => (:single, :multiple) - :single loads a folder, :multiple loads a full tree.

Outputs

Array of strings, where each string is a file URI for a music or image file.
# File lib/filescanner.rb, line 135
def load_files(uri, mode)
  validate_uri(uri)
  if !mode.nil? && mode == :single
    Dir.glob("#{uri}/*")
  else
    Dir.glob("#{uri}/**/*")
  end
end
validate_uri(uri) click to toggle source
validate_uri(): support method for validating a file system URI.

Inputs

Required

(1) String: String containing the URI to validate.

Optional

none

Outputs

Returns true if the URI is valid. Raises a StandardError exception if it is not.
# File lib/filescanner.rb, line 113
def validate_uri(uri)
  unless !uri.nil? && uri.is_a?(String) && (File.directory?(uri) || File.exist?(uri))
    raise StandardError, "Directory given (#{uri}) could not be accessed."
  end
  true
end