Permalink
Browse files

adding attachments works.

modified how i generate the include_path in the example
  • Loading branch information...
1 parent 4b941ba commit ff5b0713cb320a3dd3ad9ac488cb5adb12307835 Arin Sarkissian committed May 17, 2009
@@ -1,5 +1,7 @@
<?php
+require_once 'Duckk/CouchDB/Util.php';
+
class Duckk_CouchDB_Document
{
/**
@@ -24,21 +26,57 @@ class Duckk_CouchDB_Document
public $_attachments = array();
/**
- * Add an attachment to the document
+ * Add an attachment to the document by file system or HTTP path (URI)
+ *
+ * We'll try to figure out the name and content type of the attachment for you
+ * but you can optionally provide that info.
+ *
+ * NOTE: I tried installing the Fileinfo module via PECL but it wasn't cooperating.
+ * So, I make a decent attempt to grab the right content type etc... but it can be
+ * faulty. So if this gives you any trouble use $this->addAttachment() instead.
*
- * @param string $pathToFile
- * @param string $attachmentName
+ * @param string $pathToFile Path to the file you want to attach
+ * @param string $contentType Content-Type of the attachment
+ * @param string $name The name of the attachment (optional)
*
- * @return bool
+ * @return array The attachment being sent to CouchDB
*/
- public function addAttachment($pathToFile, $attachmentName = null)
+ public function addAttachmentByPath($pathToFile, $contentType = null, $name = null)
{
+ $info = Duckk_CouchDB_Util::getAttachmentInfo($pathToFile);
+
+ if ($contentType !== null) {
+ $info['content_type'] = $contentType;
+ }
+
+ // if $attachmentName is given then use that as opposed to $info['basename']
+ if ($name === null) {
+ $name = $info['basename'];
+ }
+ return $this->addAttachment($name, $info['data'], $info['content_type']);
}
- public function getAttchment($attachmentName)
+ /**
+ * Manually attach a file to the document
+ *
+ * @param string $name The name of the attachment
+ * @param string $data The attachment's data
+ * @param string $contentType The content type
+ * @param bool $alreadyEncoded Whether $data is already base64 encoded or not
+ *
+ * @return array The attachment being sent to CouchDB
+ */
+ public function addAttachment($name, $data, $contentType, $alreadyEncoded = false)
{
+ $attachment = array(
+ 'data' => (($alreadyEncoded) ? $data : base64_encode($data)),
+ 'content_type' => addslashes($contentType)
+ );
+
+ $this->_attachments[$name] = $attachment;
+ return $this->_attachments[$name];
}
}
@@ -48,39 +48,46 @@ static public function makeDatabaseURI($database)
}
/**
- * Get the info we need
+ * Get the info we need for a document attachment
+ *
+ * Please NOTE: I couldn't get the FileInfo module to install. Dunno know why PECL
+ * wasn't cooperating... probabaly cuz it's jank. So, this is a decent attempt at
+ * getting the right file info till I figure that shit out. So, getting the info
+ * for file system paths may or may not work for you. It will not work on Windows
+ * cuz I'm using a system call hack as a stop-gap for now (verified on OS X).
+ *
+ * @param string $pathToFile File system path or HTTP URI to a file
+ *
+ * @return array The necessary file info in an associative array
*/
- static public function getAttachmentInfo($pathToFile, $useCurl = false)
+ static public function getAttachmentInfo($pathToFile)
{
$fileInfo = array(
'content_type' => null,
'data' => null,
'basename' => basename($pathToFile)
);
- if ($useCurl || strpos($pathToFile, 'http') === 0) {
+ if (strpos($pathToFile, 'http') === 0) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pathToFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // TODO throw an exception upon curl errors
-
$fileInfo['data'] = curl_exec($ch);
$fileInfo['content_type'] = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
} else {
- $arg = escapeshellarg($pathToFile);
+ $cmd = 'file --mime -b ' . escapeshellarg($pathToFile);
- $fileInfo['content_type'] = `file -I -b $arg`;
+ $fileInfo['content_type'] = trim(`$cmd`);
$fileInfo['data'] = file_get_contents($pathToFile);
}
-
- $fileInfo['data'] = base64_encode($fileInfo['data']);
-
if (strstr($fileInfo['content_type'], ';')) {
list($fileInfo['content_type'], ) = explode(';', $fileInfo['content_type']);
}
+ // TODO handle errors for http and fs
+
return $fileInfo;
}
}
View
@@ -5,21 +5,22 @@
*
* Eventually this will be split into multiple files... each hilighting a different feature
*/
-require_once('./inc.php');
+require_once realpath(dirname(__FILE__)) . '/inc.php';
require_once('Duckk/CouchDB.php');
require_once('Duckk/CouchDB/Util.php');
-print_r(Duckk_CouchDB_Util::getAttachmentInfo('/Users/arin/Downloads/SolrUpdate.php'));
-exit;
-
$couchdb = new Duckk_CouchDB();
$doc = new Duckk_CouchDB_Document();
$doc->_id = 'abcdddd' . md5(time());
$doc->_rev = md5(time() . 'arin');
$doc->name = 'arin';
+
+$doc->addAttachmentByPath(EXAMPLE_PATH . '/peanut.jpg');
print_r($couchdb->postDocument('arin', $doc, true));
+print_r($doc);
+
exit;
View
@@ -3,9 +3,11 @@
error_reporting(E_ALL);
ini_set('display_errors', '1');
+define('EXAMPLE_PATH', dirname(__FILE__));
+
$parts = array(
- realpath('../Duckk_CouchDB'),
- realpath('../../Duckk_SimpleHTTP/Duckk_SimpleHTTP'),
+ realpath(EXAMPLE_PATH . '/../Duckk_CouchDB'),
+ realpath(EXAMPLE_PATH . '/../../Duckk_SimpleHTTP/Duckk_SimpleHTTP'),
);
set_include_path(implode(PATH_SEPARATOR, $parts));
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ff5b071

Please sign in to comment.