Skip to content

Instantly share code, notes, and snippets.

@jamesottaway
Last active August 29, 2015 14:08
Show Gist options
  • Save jamesottaway/806be64342146b91d272 to your computer and use it in GitHub Desktop.
Save jamesottaway/806be64342146b91d272 to your computer and use it in GitHub Desktop.
module ArrayRefinements
refine Array do
def head
self.first
end
def tail
self[1..-1]
end
end
end
require 'photo'
require 'array_refinements'
using ArrayRefinements
dir = '/Volumes/HFS+'
extension = 'CR2'
Dir.glob(File.join(dir, '**', "*.#{extension}"))
.map { |path| Photo.new(path) }.group_by(&:name)
.select { |name, group| group.count > 1 }
.values.each { |group|
group.head.keep!
}.each { |group|
group.tail.reject { |photo| photo == group.head }.map(&:keep!)
}.flatten.map(&:to_s)
.tap { |commands| STDOUT.puts commands }
require 'shellwords'
require 'openssl'
class Photo < Struct.new :path
def name
File.split(path).last
end
def fingerprint
@fingerprint ||= OpenSSL::Digest::SHA256.new(data)
end
def ==(other)
self.fingerprint == other.fingerprint
end
def keep!
@keep = true
end
def keep?
@keep
end
def to_s
"#{action} #{Shellwords.escape(path)}"
end
private
def data
File.read(path)
end
def action
keep? ? 'touch' : ' rm'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment