Skip to content

Instantly share code, notes, and snippets.

@rgm
Created February 15, 2013 19:36
Show Gist options
  • Save rgm/4962820 to your computer and use it in GitHub Desktop.
Save rgm/4962820 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# run with ARGV[0] pointed at an iPhone backup directory to re-tag the files
# with extensions.
#
# written under the influence of learning haskell / clojure / frp so also quite
# possibly the worst ruby ever written.
class String
def iphone_extension
types = {
/JPEG/i => 'jpg',
/PNG/i => 'png',
/Apple binary/i => 'plist',
/ASCII/i => 'txt',
/QuickTime/i => 'mov',
/SGML/i => 'html',
/HTML/i => 'html',
/data/i => 'raw',
/XML/i => 'xml',
/MPEG ADTS/ => 'mp3',
/vCard/i => 'vcard',
/iTunes AAC/i => 'm4a',
/PDF/ => 'pdf',
/CoreAudio/ => 'caf',
}
@iphone_file_type ||= types.keys.select {|t| self =~ t}.map {|match| types[match]}.first
end
def detect_file_type
`file #{self}`.strip.split(": ")
end
end
class Array
def rename_using_type
`mv #{self[0]}{,.#{self[1].iphone_extension}}` if self[1].iphone_extension
end
end
Dir[File.join(ARGV[0],'*')]
.map(&:detect_file_type)
.each(&:rename_using_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment