@ -19,22 +19,27 @@ function islandora_datastream_as_attachment($object_id, $dsid) {
try {
try {
$restConnection = new RestConnection($user);
$restConnection = new RestConnection($user);
$fedora_object = new FedoraObject($object_id, $restConnection->repository);
$fedora_object = new FedoraObject($object_id, $restConnection->repository);
// if the object exists but the datastream doesn't
if(!isset($fedora_object[$dsid])) {
return drupal_not_found();
}
header('Content-type: ' . $fedora_object[$dsid]->mimetype);
if($fedora_object[$dsid]->controlGroup == 'M' || $fedora_object[$dsid]->controlGroup == 'X') {
header('Content-length: ' . $fedora_object[$dsid]->size);
}
header("Cache-control: private");
$method = arg(5);
if (isset($method) & & $method == 'download') {
header("Content-Disposition: attachment; filename=\"" . $fedora_object[$dsid]->label);
}
print($fedora_object[$dsid]->content);
exit();
} catch (Exception $e) {
} catch (Exception $e) {
drupal_set_message(t('Error getting Islanndora datastream $d for object %s', array('%s' => $object_id, '%d' => $dsid)), 'error');
return drupal_not_found();
return"";
}
header('Content-type: ' . $fedora_object[$dsid]->mimetype);
if($fedora_object[$dsid]->controlGroup == 'M' || $fedora_object[$dsid]->controlGroup == 'X') {
header('Content-length: ' . $fedora_object[$dsid]->size);
}
}
header("Cache-control: private");
$method = arg(5);
if (isset($method) & & $method == 'download') {
header("Content-Disposition: attachment; filename=\"" . $fedora_object[$dsid]->label);
}
//maybe able to use drupal's file transfer here
print($fedora_object[$dsid]->content);
exit();
}
}
function islandora_get_datastream_parents($islandora_object) {
function islandora_get_datastream_parents($islandora_object) {
@ -211,27 +216,30 @@ function islandora_get_add_datastream_form($object_id, &$form_state) {
*/
*/
function islandora_add_datastream_form_submit($form, & $form_state) {
function islandora_add_datastream_form_submit($form, & $form_state) {
global $base_url;
global $base_url;
module_load_include('inc', 'islandora', 'RestConnection');
if (!empty($form_state['submit']) & & $form_state['submit'] == 'OK') {
if (!empty($form_state['submit']) & & $form_state['submit'] == 'OK') {
$form_state['rebuild'] = TRUE;
$form_state['rebuild'] = TRUE;
return;
return;
}
}
module_load_include('inc', 'islandora', 'includes/MimeClass');
$mimetype = new MimeClass();
$file = $form_state['values']['add-stream-file-location'];
$file = $form_state['values']['add-stream-file-location'];
$file = drupal_realpath($file);
$file = drupal_realpath($file);
$object_id = $form_state['values']['pid'];
$object_id = $form_state['values']['pid'];
$dsid = $form_state['values']['stream_id'];
$dsid = $form_state['values']['stream_id'];
$ds_label = $form_state['values']['stream_label']; // Add the file extention to the end of the label.;
$ds_label = $form_state['values']['stream_label'];
// $dformat = $mimetype->getType($file);
$dformat = $mimetype->getMime Type($file);
$controlGroup = "M";
$controlGroup = "M";
//if ($dformat == 'text/xml') {
// $controlGroup = 'X';
//}
global $user;
try {
try {
$restConnection = new RestConnection($user );
$restConnection = new RestConnection();
$fedora_object = new FedoraObject($object_id, $restConnection->repository);
$fedora_object = $restConnection->repository->getObject($object_id);
$ds = $fedora_object->constructDatastream($dsid, $controlGroup);
$ds = $fedora_object->constructDatastream($dsid, $controlGroup);
$ds->label = $ds_label;
$ds->label = $ds_label;
$ds->mimetype = $dformat;
$ds->setContentFromFile($file);
$ds->setContentFromFile($file);
$fedora_object->ingestDatastream($ds);
$fedora_object->ingestDatastream($ds);
$d_file = file_load($form_state['values']['fid']);
$d_file = file_load($form_state['values']['fid']);
@ -240,7 +248,8 @@ function islandora_add_datastream_form_submit($form, &$form_state) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
return;
return;
}
}
$form_state['rebuild'] = TRUE;
drupal_set_message("Successfully Added Datastream!");
drupal_goto("islandora/object/$object_id");
}
}
/**
/**
@ -252,6 +261,7 @@ function islandora_add_datastream_form_submit($form, &$form_state) {
*/
*/
function islandora_add_datastream_form_validate($form, & $form_state) {
function islandora_add_datastream_form_validate($form, & $form_state) {
module_load_include('inc', 'islandora', 'includes/MimeClass');
module_load_include('inc', 'islandora', 'includes/MimeClass');
module_load_include('inc', 'islandora', 'RestConnection');
$mimetype = new MimeClass();
$mimetype = new MimeClass();
if ($form_state['clicked_button']['#value'] == 'OK') {
if ($form_state['clicked_button']['#value'] == 'OK') {
$form_state['rebuild'] = TRUE;
$form_state['rebuild'] = TRUE;
@ -275,13 +285,32 @@ function islandora_add_datastream_form_validate($form, &$form_state) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
return FALSE;
}
}
$object_id = $form_state['values']['pid'];
$restConnection = new RestConnection();
$fedora_object = $restConnection->repository->getObject($object_id);
if(isset($fedora_object[$dsid])) {
form_set_error('', t('Data stream ID already exists in object.'));
return FALSE;
}
$mimetype = new MimeClass();
$mimetype = new MimeClass();
$unused_dsids = islandora_get_unused_dsids($form_state['values']['pid']);
$unused_dsids = islandora_get_unused_dsids($form_state['values']['pid']);
$types_allowed = $unused_dsids[$dsid];
if(isset($unused_dsids[$dsid])) {
$arr = array();
$types_allowed = $unused_dsids[$dsid];
foreach ($types_allowed as $type) {
$arr = array();
$arr[] = $mimetype->getExtension($type);
foreach ($types_allowed as $type) {
$arr[] = $mimetype->getExtension($type);
}
}
else {
// todo: this is unsafe, should probably be fixed see:
// http://api.drupal.org/api/drupal/includes!file.inc/function/file_save_upload/7
$arr = array();
}
}
$file = file_save_upload('add-stream-file-location', array('file_validate_extensions' => $arr));
$file = file_save_upload('add-stream-file-location', array('file_validate_extensions' => $arr));
if ($file) {
if ($file) {
$form_state['values']['add-stream-file-location'] = $file->uri;
$form_state['values']['add-stream-file-location'] = $file->uri;
@ -340,32 +369,44 @@ function islandora_add_datastream_form($form, &$form_state, $object_id) {
'#value' => t('Add Datastream')
'#value' => t('Add Datastream')
);
);
if (!empty($unused_dsids)) {
$unused_dsids = islandora_get_unused_dsids($object_id);
$dsidsForForm = array();
$dsidsForForm = '';
foreach ($unused_dsids as $key => $value) {
$i = 0;
$dsidsForForm[$key] = $key;
foreach ($unused_dsids as $key => $value) {
if($i++) {
$dsidsForForm .= ", ";
}
}
$form['add_fieldset']['stream_id'] = array(
$dsidsForForm .= "'$key'";
'#type' => 'select',
'#title' => t('Datastream ID'),
'#default_value' => variable_get('feed_item_length', 'teaser'),
'#weight' => '-1',
'#description' => t('Datastream IDs defined by the content model.'),
);
$form['add_fieldset']['stream_id']['#options'] = $dsidsForForm;
}
else {
$form['add_fieldset']['stream_id'] = array(
'#title' => 'Datastream ID',
'#required' => 'TRUE',
'#description' => t('An ID for this stream that is unique to this object. Must start with a letter and contain only alphanumeric characters and dashes and underscores.'),
'#type' => 'textfield',
'#weight' => -1,
);
}
}
$form['add_fieldset']['stream_id'] = array(
'#title' => 'Datastream ID',
'#required' => 'TRUE',
'#description' => t('An ID for this stream that is unique to this object. Must start with a letter and contain only alphanumeric characters and dashes and underscores. Datastreams that are defined by the content model dont currently exist: < b > ' . $dsidsForForm . '< / b > .'),
'#type' => 'textfield',
'#weight' => -1,
'#autocomplete_path' => "islandora/object/$object_id/manage/datastreams/add/autocomplete",
);
return $form;
return $form;
}
}
function islandora_datastream_autocomplete_callback($object_id, $string = '') {
$dsids = islandora_get_unused_dsids($object_id);
$output = array();
foreach($dsids as $id => $ds) {
if(trim($string) == '') {
$output[$id] = $id;
}
else {
$ret = stripos($id, $string);
if($ret !== FALSE) {
$output[$id] = $id;
}
}
}
drupal_json_output($output);
}
function islandora_datastream_get_human_readable_size($ds) {
function islandora_datastream_get_human_readable_size($ds) {
module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('inc', 'islandora', 'includes/utilities');