Class: Vera::Safe
- Inherits:
-
Object
- Object
- Vera::Safe
- Defined in:
- lib/vera/safe.rb
Class Method Summary collapse
- .candidates_by_file_size(media_files, backup_media_files) ⇒ Object
- .candidates_by_partial_hash(media_files) ⇒ Object
- .execute(options) ⇒ Object
- .filter_real_hash(hash) ⇒ Object
- .print_folders(path, backup_path) ⇒ Object
- .print_list_of_files(files, backup_dir) ⇒ Object
- .print_number_of_files(media_files, backup_files) ⇒ Object
- .print_separator(length) ⇒ Object
- .safe_to_delete_path?(path, backup_path) ⇒ Boolean
Class Method Details
.candidates_by_file_size(media_files, backup_media_files) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/vera/safe.rb', line 48 def self.candidates_by_file_size(media_files, backup_media_files) media_files.map do |media| { media: media, candidates: backup_media_files.select { |c| c.size == media.size } } end end |
.candidates_by_partial_hash(media_files) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vera/safe.rb', line 57 def self.candidates_by_partial_hash(media_files) media_files.map do |hash| media = hash[:media] file_found = hash[:candidates].find do |m| media.partial_hash == m.partial_hash end { media: media, file_found: file_found, found: !file_found.nil? } end end |
.execute(options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/vera/safe.rb', line 6 def self.execute() unless Exiftool.installed? puts " You need to install exiftool to use vera. You can run the following command to do it: bash <(curl -Ls https://git.io/JtbuH) " return end path = .path path = Dir.pwd if .path.nil? path = File.(path) backup_path = File.(.backup) unless File.directory?(path) puts "#{path} is not a valid directory.".red return end unless File.directory?(backup_path) puts "#{backup_path} is not a valid directory.".red return end safe_to_delete_path? path, backup_path end |
.filter_real_hash(hash) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/vera/safe.rb', line 114 def self.filter_real_hash(hash) { media: hash[:media], candidates: hash[:candidates].find do |m| media.real_hash == m.real_hash end } end |
.print_folders(path, backup_path) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/vera/safe.rb', line 72 def self.print_folders(path, backup_path) = "Trying to find #{path} files in #{backup_path}" print_separator .length puts .yellow print_separator .length end |
.print_list_of_files(files, backup_dir) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/vera/safe.rb', line 94 def self.print_list_of_files(files, backup_dir) = "#{Icons.check} #{files.select { |f| f[:found] }.length } file(s) found" = "#{Icons.cross} #{files.reject { |f| f[:found] }.length } file(s) not found" files.sort_by { |x| [(x[:found] ? 0 : 1), x[:path]] } .each do |f| found = f[:found] found_in = f[:file_found].path.gsub(backup_dir, '') filename = f[:media].filename.ljust(15, ' ') puts "#{Icons.check} #{filename} #{Icons.arrow} #{found_in}" if found puts "#{Icons.cross} #{filename} not found." unless found end print_separator .length puts puts print_separator .length end |
.print_number_of_files(media_files, backup_files) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/vera/safe.rb', line 80 def self.print_number_of_files(media_files, backup_files) = "In a total of #{backup_files.length} media files in your backup folder..." print_separator .length puts "Searching #{media_files.length} media files...".yellow puts .yellow print_separator .length end |
.print_separator(length) ⇒ Object
89 90 91 92 |
# File 'lib/vera/safe.rb', line 89 def self.print_separator length length.times { print "-" } print "\n" end |
.safe_to_delete_path?(path, backup_path) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vera/safe.rb', line 32 def self.safe_to_delete_path?(path, backup_path) backup_media_files = Lister.all_media_files_recursive(backup_path) .map { |f| Media.new(f) } media_files = Lister.all_media_files_recursive(path) .map { |f| Media.new(f) } print_folders(path, backup_path) print_number_of_files(media_files, backup_media_files) select_candidates = candidates_by_file_size(media_files, backup_media_files) found_files = candidates_by_partial_hash(select_candidates) print_list_of_files(found_files, backup_path) end |