// Read file.
//
// @return void
// @panics
func (this *DocumentAttachment) ReadFile(encode bool) {
	if this.File == "" {
		panic("Attachment file is empty!")
	}

	// get file info
	info, err := util.FileInfo(this.File)
	if err != nil {
		panic(err)
	}
	this.ContentType = util.String(info["mime"])

	// get file contents
	data, err := util.FileGetContents(this.File)
	if err != nil {
		panic(err)
	}

	this.Data = data
	// convert to base64 if encode=true
	if encode {
		this.Data = util.Base64Encode(data)
	}

	this.DataLength = int64(len(data))
}
Example #2
0
// Constructor.
//
// @param  config map[string]interface{}
// @return *couch.http.Request
func NewRequest(config map[string]interface{}) *Request {
	this := &Request{
		Stream: *NewStream(TYPE_REQUEST, "1.0"),
		Config: config,
	}

	// add default headers
	this.Headers["Host"] = _fmt.Sprintf("%s:%v", config["Host"], config["Port"])
	this.Headers["Connection"] = "close"
	this.Headers["Accept"] = "application/json"
	this.Headers["Content-Type"] = "application/json"
	this.Headers["User-Agent"] = _fmt.Sprintf("%s/v%s (+http://github.com/yay-couch/couch-go)",
		config["Couch.NAME"], config["Couch.VERSION"])

	// add auth header
	if config["Username"] != "" && config["Password"] != "" {
		this.Headers["Authorization"] = "Basic " +
			util.Base64Encode(_fmt.Sprintf("%s:%s", config["Username"], config["Username"]))
	}

	return this
}