Class: MediaOrganizer::Filescanner

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

Overview

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/”, => true, :music => true)

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

Defined Under Namespace

Classes: FileNotValidError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilescanner

Returns a new instance of Filescanner.



28
29
30
31
# File 'lib/filescanner.rb', line 28

def initialize()
  @root_nodes = []
  @source_list = []
end

Instance Attribute Details

#root_nodesObject (readonly)

Returns the value of attribute root_nodes.



24
25
26
# File 'lib/filescanner.rb', line 24

def root_nodes
  @root_nodes
end

#source_listObject

Returns the value of attribute source_list.



25
26
27
# File 'lib/filescanner.rb', line 25

def source_list
  @source_list
end

Instance Method Details

#addRoot(dir_uri) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Object

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.



104
105
106
107
108
109
# 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 = {}) ⇒ Object

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”)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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