couchrest_model/script/couchdir
2008-07-04 16:56:09 -07:00

62 lines
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
begin
doc = @db.get(dirname)
rescue RestClient::Request::RequestFailed
doc = nil
end
# 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"