Class: Imagesorter::FileSystemProcessor

Inherits:
Object
  • Object
show all
Includes:
R18n::Helpers
Defined in:
lib/imagesorter/file_system_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destination:, destination_fmt: '%Y/%m/%d/%<name>s.%<extension>s', copy_mode: :copy, test: false) ⇒ FileSystemProcessor

Returns a new instance of FileSystemProcessor.



9
10
11
12
13
14
15
16
17
# File 'lib/imagesorter/file_system_processor.rb', line 9

def initialize(destination:,
               destination_fmt: '%Y/%m/%d/%<name>s.%<extension>s',
               copy_mode: :copy,
               test: false)
  @destination      = destination
  @test             = test
  @copy_mode        = copy_mode
  @destination_fmt  = destination_fmt
end

Instance Attribute Details

#copy_modeObject (readonly)

Returns the value of attribute copy_mode.



7
8
9
# File 'lib/imagesorter/file_system_processor.rb', line 7

def copy_mode
  @copy_mode
end

Instance Method Details

#format_destination(file, destination_params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/imagesorter/file_system_processor.rb', line 46

def format_destination(file, destination_params)
  File.join(@destination, format(l(file.time, @destination_fmt), file.to_h.merge(destination_params)))
rescue KeyError => e
  Imagesorter.logger.warn e.message

  key = /^key<(.*)> not found$/.match(e.message)[1]

  raise if key.nil?

  destination_params[key.to_sym] = ''
  retry
end

#handle_duplicate(source, dest) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/imagesorter/file_system_processor.rb', line 59

def handle_duplicate(source, dest)
  return nil if dest.nil?
  return dest unless File.exist?(dest)

  return nil if FileUtils.identical?(source, dest) # skip if identical

  basename = File.basename(dest, '.*')
  extname  = File.extname(dest)
  dirname  = File.dirname(dest)

  sequence = 1

  handle_duplicate(source, File.join(dirname, "#{basename}_#{sequence}#{extname}"))
end

#process(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/imagesorter/file_system_processor.rb', line 23

def process(file)
  source = file.file.path

  dest = handle_duplicate(source, format_destination(file, full_name: File.basename(source),
                                                           name: File.basename(source, '.*'),
                                                           extension: File.extname(source).delete('.')))

  return if dest.nil?

  Imagesorter.logger.info "#{step_name} #{source} to #{dest}"

  return if @test

  dest_dir = File.dirname(dest)
  FileUtils.mkdir_p(dest_dir) unless File.directory?(dest_dir)

  if @copy_mode == :move
    FileUtils.mv(source, dest)
  else
    FileUtils.cp(source, dest, preserve: true)
  end
end

#step_nameObject



19
20
21
# File 'lib/imagesorter/file_system_processor.rb', line 19

def step_name
  @copy_mode == :move ? 'Moving' : 'Copying'
end