lib/tlapse/util.rb in tlapse-0.6.2 vs lib/tlapse/util.rb in tlapse-0.6.3

- old
+ new

@@ -1,19 +1,30 @@ require "fileutils" module Tlapse class Util - def self.normalize!(dry_run: false) + def self.organize!(dry_run: false) Dir["*.jpg"].each do |filename| - normalized = normalize_filename(filename) - if filename != normalized - puts "Rename #{filename} to #{normalized}" - FileUtils.mv(filename, normalized) unless dry_run - end + normalize_path! filename, dry_run: dry_run end end - def self.normalize_filename(file) - File.mtime(file).strftime(Tlapse::Capture::CAPTURE_FILENAME) + def self.normalize_path!(file, dry_run: false) + dirname = normalized_dirname(file) + FileUtils.mkdir dirname unless File.exist? dirname || dry_run + + filename = normalized_filename(file) + path = File.join dirname, filename + + puts "Rename #{file} to #{path}" + FileUtils.mv file, path unless dry_run + end + + def self.normalized_dirname(file) + File.mtime(file).strftime(Tlapse::Capture.capture_dirname) + end + + def self.normalized_filename(file) + File.mtime(file).strftime(Tlapse::Capture.capture_filename) end end end