Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
require_once('uuid.php');
function generateDocumentId()
{
return 'rec-' . str_replace('-', '', UUID::mint()->string);
}
function getTemplate($id, $createdDate) {
return array(
'_id' => $id,
'dateCreated' => $createdDate,
'dateModified' => $createdDate,
'dbID' => 'db-c1da6869786341efbd8dbe2021a8df72',
'form' => 'frm-900c80d629cd41ffb9bf45f091381753',
'type' => 'frm-900c80d629cd41ffb9bf45f091381753',
'values' => array(
'fld-dc8a71d49f1e4f10836166b61e431116' => array(array(
'dimensions' => '{}',
'filename' => '',
'mimetype' => ''
))
),
'_attachments' => array()
);
}
while(!feof(STDIN))
{
$filename = rtrim(fgets(STDIN));
sleep(5);
if (!file_exists($filename))
{
continue;
}
echo $filename . "\n";
$base = basename($filename);
if (strpos($base, '.jpg') === FALSE)
{
echo "Not a .jpg file: $filename\n";
continue;
}
$createdDate = date('c');
$template = getTemplate(generateDocumentId(), $createdDate);
$imageDetails = getimagesize($filename);
print_r($imageDetails);
$template['values']['fld-dc8a71d49f1e4f10836166b61e431116'][0]['dimensions'] = sprintf("{%d, %d}", $imageDetails[0], $imageDetails[1]);
$template['values']['fld-dc8a71d49f1e4f10836166b61e431116'][0]['filename'] = $base;
$template['values']['fld-dc8a71d49f1e4f10836166b61e431116'][0]['mimetype'] = $imageDetails['mime'];
$template['_attachments'][$base] = array(
'content_type' => $imageDetails['mime'],
'data' => base64_encode(file_get_contents($filename))
);
$data_json = json_encode($template);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.100:5984/db-c1da6869786341efbd8dbe2021a8df72/' . $template['_id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment