Sha256: aced413e038b841f1438bc82e5086726399782d99450b544331c32e022295b9d

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

#!/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"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jchris-couchrest-0.7.99 script/couchdir