#!/bin/env ruby
require 'json'
+require 'fileutils'
IMG_TYPES = %w[jpg jpeg png gif]
VID_TYPES = %w[mp4 ogg webm]
end
ARGV.each do |root|
- # Generate the index
index = []
+ existing = {}
+ index_path = "#{root}/index.json"
+
+ # Find all valid references in the existing index
+ if File.exist? index_path
+ JSON.load_file(index_path).each do |e|
+ if File.exist? "#{root}/#{e["path"]}"
+ index << e
+ existing[e["path"]] = true
+ end
+ end
+ end
+
+ # Generate the index
Dir.glob("#{root}/**/*.#{TYPES}").each do |path|
ext = path.sub(/.*\.([^.]+)$/, '\1').downcase
short_path = path.sub("#{root}/", '')
+
+ # Only process files that are not already in the index
+ next if existing[short_path]
puts short_path
+ # OK, let's figure out what it is and add it
if (VID_TYPES.include?(ext))
duration = `#{CMD} \"#{path}\"`.chomp.to_f
index << {
end
# Save the index to disc
- File.open("#{root}/index.json", "wb") do |f|
+ File.open("#{index_path}.tmp", "wb") do |f|
f.write JSON.dump(index)
end
+ FileUtils.mv("#{index_path}.tmp", index_path)
end