class MediaOrganizer::Filescanner

FileScanner: scans a file system for images and/or music files, providing a list of file URIs. Output of FileScanner.open() returns a list of paths that can then be consumed by MediaOrganizer::Renamer.

Example

Filescanner.open(“/path/to/files/”, {:image => true, :music => true})

This will return a list of all music and image files contained in /path/to/files/ and it’s

Attributes

root_nodes[R]
source_list[RW]

Public Class Methods

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

Public Instance Methods

addRoot(dir_uri) click to toggle source

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

# File lib/filescanner.rb, line 81
def addRoot(dir_uri)
    unless !dir_uri.nil? && dir_uri.is_a?(String) && File.directory?(dir_uri)   
      raise FileNotFoundError, "Directory given (#{dir_uri}) could not be accessed."
    end
    @root_nodes << dir_uri
rescue FileNotFoundError => e
  puts e.message
  puts e.backtrace.inspect
  return false
end
multiscan(args = {}) click to toggle source

Filescanner:multiscan(): scans multiple directories added to @root_nodes using the addRoot() method.

Inputs

*Optional 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 104
def multiscan(args = {})
  @root_nodes.each do |uri|
    open(uri, args)
  end
  return @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

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

*2: Optional 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.

Example

Filescanner.open(“/absolute/path/for/top/of/directory/tree”)

# File lib/filescanner.rb, line 51
def open(uri = "", args = {})
  unless !uri.nil? && uri.is_a?(String) && (File.directory?(uri) || File.exists?(uri))   
    raise FileNotFoundError, "Directory given (#{uri}) could not be accessed."
  end
  
  include_images = true unless args[:image] == false     
  include_music = true unless args[:music] == false     
  files = []
  if args[:mode] == :single
    files = Dir.glob("#{uri}/*")
  else
    files = Dir.glob("#{uri}/**/*")    
  end
  
  #add all files found to @source_list, if they are music files
  files.each do |f|
    if (Music.is_music?(f) && include_music) || (Image.is_image?(f) && include_images)
      @source_list << f
    end
  end

  return @source_list

rescue FileNotFoundError => e
  puts e.message
  puts e.backtrace.inspect
  return false
end