Top Level Namespace

Defined Under Namespace

Modules: Imagebackup Classes: FileTypes

Instance Method Summary collapse

Instance Method Details

#build_paths(dest, file, date) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/methods/build_paths.rb', line 3

def build_paths(dest, file, date)
  date = get_dates(file)
  destpath = "#{dest}/#{date}"
  destpath = destpath.gsub('//', '/')
  outfile =  "#{destpath}/#{File.basename(file)}"
  [outfile, destpath]
end

#copy_pic(file, outfile, destpath, dryrun = nil, file_op = 'cp') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/methods/copy_pic.rb', line 13

def copy_pic(file, outfile, destpath, dryrun = nil, file_op = 'cp')
  if File.exist?(outfile)
    puts "\"#{outfile}\" #{"already exists. Skipping...".colorize(:light_blue)}."
  else
    unless dryrun
      unless File.exist?(destpath)
        puts "creating folder \"#{destpath}\""
        FileUtils.mkdir_p(destpath)
      end
    end
    process_file(file, outfile, dryrun, file_op)
  end
end

#display_help(err = nil, color = nil) ⇒ Object



1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/methods/display_help.rb', line 1

def display_help(err=nil,color=nil)
  print "    ===== ImageBackup =====\n\n    Keep your photos and videos organized by date.\n\n    A simple terminal app to crawl a folder (usually a camera card's DCIM folder) for pictures and videos, and pop them in dated folders to your destination folder of choice. Uses exif data when available, creation_time, or the file's own creation date if nothing better is present.\n\n    Will also copy over any xmp sidecar files found, not overwriting.\n\n    Options:\n\n    -n, --dry-run                            Run without actually doing anything. Good for making sure\n                                             things are working properly. This will also give you console\n                                             output which helps identify unreadable files.\n\n    -a, --add-filetype <extension> <type>    This will add a custom file type to the list of files it's\n                                             looking for. If you have an arcane digital camera (we\n                                             currently support canon, sony, pentax but new stuff comes out\n                                             all the time), this will allow you to add your raw files.\n                                             You can also access *filetypes.csv* and add them manually,\n                                             but ensure there's a blank line at the end or this program\n                                             may behave badly.\n\n    -m, --move                               Will move (deleting the original), which is probably not a\n                                             good idea in most cases but still useful at times.\n    -l, --link                               Create symbolic links instead of copying or moving\n                                             actual files. Useful for large videos.\n\n    Usage:\n\n    - To backup a camera card:\n\n    $ cd /media/username/EOS_DIGITAL/DCIM\n    $ imagebackup.rb ~/Photos/raw\n\n    This will search all files within the DCIM folder, check them with either exiv2 (for stills) or ffprobe (for videos) and retrieve their creation dates.\n    It will then copy them to a folder of the form ~/Photos/raw/<yyyy>-<mm>-<dd>\n    If it's unable to find metadata in a file it will look at the file's creation time attribute, which is less reliable but usually ok.\n\n    - To register a new file type:\n\n    $ imagebackup.rb --add-filetype orf pic\n    or\n    $ imagebackup.rb -a orf pic\n\n    This will work with *.ORF, orf, \".orf\" as it will strip off any unnecessary characters. It is case-insensitive.\n\n    - To do a dry run, checking each file and destination but not actually copying:\n\n    $ imagebackup.rb -n ~/Photos/raw\n    or\n    $ imagebackup.rb ~/Photos/raw --dry-run\n\n    - To move files instead of copying them (careful!):\n\n    $ imagebackup.rb -m ~/Photos/raw\n    or\n    $ imagebackup.rb --move ~/Photos/raw\n\n    - To make symbolic links instead of copying files:\n\n    $ imagebackup.rb -l ~/Photos/raw\n    or\n    $ imagebackup.rb --link ~/Photos/raw\n\n    This mode can be useful if you want to operate on the files in one place but keep them on their media. Particularly useful for large movie files.\n  HELPFILE\n  puts err.to_s.colorize(color)\n  exit(0)\nend\n"

#get_dates(file) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/methods/get_dates.rb', line 1

def get_dates(file)
  begin
    image = Exiv2::ImageFactory.open(file)
    image.
    date = image.exif_data.find { |v| v[0] == 'Exif.Image.DateTime' }
    date = date[1].split[0].gsub(':', '-')
  rescue StandardError
    begin
      probe = Ffprober::Parser.from_file(file)
      date = probe.format.tags[:creation_time].split(/[T ]/)[0]
    rescue StandardError
      fileobj = File.new(file)
      date = "#{"%04d" % fileobj.stat.ctime.year}-#{"%02d" % fileobj.stat.ctime.month}-#{"%02d" % fileobj.stat.ctime.day}"
    end
  end
  date
end

#main_loop(dest, dryrun = true, file_op = 'copy') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/imagebackup.rb', line 17

def main_loop(dest, dryrun = true, file_op = 'copy')
  file_types = FileTypes.list
  Dir.glob(file_types).reverse_each do |f|
    file = "#{Dir.pwd}/#{f}"

    parms = build_paths(dest, file, get_dates(file))
    outfile = parms[0]
    destpath = parms[1]

    copy_pic(file, outfile, destpath, dryrun, file_op)
  end
  puts "\nFinished!\n".colorize(:light_green)
end

#process_file(file, outfile, dryrun = nil, file_op = 'cp') ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
# File 'lib/methods/copy_pic.rb', line 1

def process_file(file, outfile, dryrun = nil, file_op = 'cp')
  if dryrun
    puts "#{"pretending to ".colorize(:light_green)}#{file_op} \"#{file}\"#{" to ".colorize(:light_green)}\"#{outfile}\""
  else
    puts "#{file_op}-ing \"#{file} to #{outfile}\"..."
    FileUtils.public_send(file_op, file, outfile)
    if File.exist?("#{file}.xmp")
      FileUtils.public_send(file_op, "#{file}.xmp", "#{outfile}.xmp")
    end
  end
end