Skip to content

Instantly share code, notes, and snippets.

@stephenhandley
Created July 17, 2014 21:25
Show Gist options
  • Save stephenhandley/d4ef0b807ce44e94ed88 to your computer and use it in GitHub Desktop.
Save stephenhandley/d4ef0b807ce44e94ed88 to your computer and use it in GitHub Desktop.
Fs = require('fs')
Path = require('path')
ChildProcess = require('child_process')
TYPES = {
"PNG image data" : 'png'
"JPEG image data" : 'jpg'
"GIF image data" : 'gif'
"TIFF image data" : 'tiff'
"Adaptive Multi-Rate Codec (GSM telephony)" : 'amr'
"CoreAudio Format audio file version 1" : 'caf'
"Zip archive data" : 'zip'
"ISO Media" : 'iso'
#
# TYPES WHAT I DONT LIKE
#
#
#"ASCII text" : 'txt'
#"ASCII English text" : 'txt'
# "Apple binary property list"
# "data"
# "Little-endian UTF-16 Unicode c program text"
# "empty"
# "SQLite 3.x database"
# "gzip compressed data"
# "XML document text"
# "HTML document text"
# "UTF-8 Unicode text"
# "RIFF (little-endian) data"
# "vCard visiting card"
# "UTF-8 Unicode English text"
# "XWD X Window Dump image data"
# "UTF-8 Unicode C++ program text"
# "IFF data"
# "exported SGML document text"
# "ASCII C++ program text"
# "UTF-8 Unicode c program text"
}
getMediaFromIPhoneBackup = (opts)->
source = opts.source
destination = opts.destination
callback = opts.callback
Fs.readdir(source, (error, filenames)->
if error
return callback("OOPZ: #{error}")
num_files = filenames.length
index = 0
getType = ()->
filename = filenames[index]
unless filename
return callback('done')
filepath = Path.join(source, filename)
file_cmd = """ file "#{filepath}" """
ChildProcess.exec(file_cmd, (error, stdout, stderr)->
if error
return callback(error)
if (index >= num_files)
callback('done')
else
[path, description] = stdout.split(':')
type = description.trim().split(',')[0]
next = (copied)->
char = if copied then '.' else '-'
process.stdout.write(char)
index += 1
getType()
if type of TYPES
extension = TYPES[type]
destination_filepath = Path.join(destination, "#{filename}.#{extension}")
cp_cmd = """cp "#{filepath}" "#{destination_filepath}" """
ChildProcess.exec(cp_cmd, (error, stdout, stderr)->
if error
return callback(error)
else
next(true)
)
else
next(false)
)
getType()
)
unless (process.argv.length is 4)
throw new Error("pass directory of media plz as arg and other arg where to go k user friendly i know")
getMediaFromIPhoneBackup(
source : process.argv[2]
destination : process.argv[3]
callback : (message)->
console.log(message)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment