couchrest_model/script/couchdir
Chris Anderson a12ad9c4ea restart etc
2008-06-15 11:43:05 -07:00

58 lines
No EOL
1.1 KiB
Ruby
Executable file

#!/usr/bin/env ruby
unless ARGV.length >= 2
puts "usage: couchdir path/to/directory db-name"
exit
end
dirname = ARGV[0].sub(/\/$/,'')
dbname = ARGV[1]
puts "Shoving #{dirname} into #{dbname}."
require File.expand_path(File.dirname(__FILE__)) + '/../couchrest'
require 'fileutils'
cr = CouchRest.new("http://localhost:5984")
@db = cr.database(dbname)
@content_types = {
"html" => "text/html",
"htm" => "text/html",
"png" => "image/png",
"css" => "text/css"
}
files = Dir.glob(File.join(dirname,"**","*"))
attachments = {}
files.each do |filename|
content = open(filename).read
aname = filename.split('/')
aname.shift
aname = aname.join('/')
attachments[aname] = {
"data" => content,
"content_type" => @content_types[aname.split('.').last]
}
end
puts attachments.keys.inspect
doc = @db.get(dirname) rescue nil
# puts "get: #{doc.inspect}"
if doc
doc["_attachments"] = attachments
else
doc = {
"_id" => dirname,
"_attachments" => attachments
}
end
# puts "saving: #{doc.inspect}"
@db.save(doc)
puts "saved"