couchrest_model/script/couchdir

62 lines
1.1 KiB
Text
Raw Normal View History

2008-06-09 23:08:46 +02:00
#!/usr/bin/env ruby
2008-06-15 20:43:05 +02:00
unless ARGV.length >= 2
puts "usage: couchdir path/to/directory db-name"
exit
end
2008-06-09 23:08:46 +02:00
dirname = ARGV[0].sub(/\/$/,'')
dbname = ARGV[1]
2008-06-15 20:43:05 +02:00
2008-06-09 23:08:46 +02:00
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
2008-07-05 01:56:09 +02:00
begin
doc = @db.get(dirname)
rescue RestClient::Request::RequestFailed
doc = nil
end
2008-06-09 23:08:46 +02:00
2008-06-15 20:43:05 +02:00
# puts "get: #{doc.inspect}"
2008-06-09 23:08:46 +02:00
if doc
doc["_attachments"] = attachments
else
doc = {
"_id" => dirname,
"_attachments" => attachments
}
end
2008-06-15 20:43:05 +02:00
# puts "saving: #{doc.inspect}"
2008-06-09 23:08:46 +02:00
@db.save(doc)
2008-06-15 20:43:05 +02:00
puts "saved"