Class: Plexify::MediaOrganizer

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

Overview

Class MediaOrganizer It manage source and destination folders

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination) ⇒ MediaOrganizer

Initialize



9
10
11
12
# File 'lib/plexify/media_organizer.rb', line 9

def initialize(source, destination)
  @source = source
  @destination = destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



6
7
8
# File 'lib/plexify/media_organizer.rb', line 6

def destination
  @destination
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/plexify/media_organizer.rb', line 5

def source
  @source
end

Instance Method Details

#move_to_destination(file, media) ⇒ Object

Move a file the his appropriate destination folder



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/plexify/media_organizer.rb', line 15

def move_to_destination(file, media)
  # Consider media is a movie
  destination = @destination.movies_folder.path

  # Create an appropriate media folder
  media_folder_name = "#{media.data.name} (#{Date.parse(media.data.released).year})"
  media_folder_path = File.join(destination, media_folder_name)
  file_basename = File.basename(file.path)

  # Create the movie folder
  FileUtils.mkdir(media_folder_path) unless File.exists?(media_folder_path)
  media_folder = File.open(media_folder_path)

  if media_folder
    renamed_file_basename = media_folder_name + File.extname(file_basename)
    renamed_file_path = File.join(media_folder.path, renamed_file_basename)

    # Copy the file with a progress bar
    Logger.info("Copying file to '#{renamed_file_path}'")
    copy_with_progressbar(file.path, renamed_file_path)
    Logger.success("File '#{renamed_file_path}' copied") if File.exists?(renamed_file_path)
  end

  Logger.info("\nDo you want to remove the source file '#{file_basename}' ? (y/n) ")
  resp = STDIN.gets.chomp

  if resp.eql?("y")
    FileUtils.rm file.path
    Logger.success("File '#{file_basename}' removed")
  end
end