Skip to content

Commit

Permalink
Merge pull request #16 from MorganDawe/7.x
Browse files Browse the repository at this point in the history
Cleaned up code to be compliant with code standards/file permissions.
  • Loading branch information
nigelgbanks committed Apr 3, 2013
2 parents f2c56ad + 668c375 commit 1c200ba
Showing 1 changed file with 56 additions and 31 deletions.
87 changes: 56 additions & 31 deletions includes/utilities.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @file
* Miscellaneous helper functions for creating/ingesting FITS documents.
*/

/**
Expand All @@ -10,7 +11,7 @@
* @param FedoraObject $object
* The object that will be used to generate/store the derivatives.
*
* @return boolean
* @return bool
* TRUE if the techmd datastream was created, FALSE otherwise.
*/
function islandora_fits_create_techmd(FedoraObject $object) {
Expand All @@ -22,23 +23,20 @@ function islandora_fits_create_techmd(FedoraObject $object) {
}
$mime_detect = new MimeDetect();
$ext = $mime_detect->getExtension($object['OBJ']->mimeType);
$file_name = str_replace(":", "-", $object->id);
$file_name = str_replace(':', '-', $object->id);
$out_file = drupal_realpath("temporary://{$file_name}.OBJ.{$ext}");
$object['OBJ']->getContent($out_file);

$fits_file = islandora_fits_create_fits($out_file);
if ($fits_file === FALSE) {
watchdog("islandora_fits",
t("Failed to create technical metadata with FITS script."));
watchdog('islandora_fits', 'Failed to create technical metadata with FITS script.');
}
else {
islandora_fits_add_datastream($object,
variable_get('islandora_fits_techmd_dsid', 'TECHMD'), $fits_file);
drupal_unlink($fits_file); // just in case
islandora_fits_add_datastream($object, variable_get('islandora_fits_techmd_dsid', 'TECHMD'), $fits_file);
drupal_unlink($fits_file);
}

// add any more processors here - there probably won't be any

// Add any more processors here - there probably won't be any.
drupal_unlink($out_file);

// If fits_file resolved then we succeeded, otherwise we failed.
Expand All @@ -48,45 +46,72 @@ function islandora_fits_create_techmd(FedoraObject $object) {
/**
* Creates the technical metadata derivative from the given file.
*
* @param string $file_uri
* @param string $file
* The URI to the file from which the derivative will be generated.
*
* @return string
* A URI to the generated derivative if successful, FALSE otherwise.
*/
function islandora_fits_create_fits($file) {
$output = array();
$outfile = $file . ".tech.xml";
// compose fits command
$command = variable_get("islandora_fits_executable_path", "fits.sh") . " -i "
. $file . " -xc -o " . $outfile;
$outfile = "{$file}.tech.xml";
$fits = variable_get('islandora_fits_executable_path', 'fits.sh');
$command = "$fits -i $file -xc -o $outfile";
exec($command, $output, $ret);
if ($ret == "0") {
return $outfile;
if ($ret == '0') {
if (verify_fits_xml($outfile)) {
return $outfile;
}
}
// if it failed, lets try a simpler command (-x instead of -xc)
$command = variable_get("islandora_fits_executable_path", "fits.sh") . " -i "
. $file . " -x -o " . $outfile;
$output = array();
// It failed, lets try a simpler command (-x instead of -xc).
$command = "$fits -i $file -x -o $outfile";
exec($command, $output, $ret);
if ($ret == "0") {
return $outfile;
if ($ret == '0') {
if (verify_fits_xml($outfile)) {
return $outfile;
}
}
// in case of disaster, fall back to the simplest possibly command line
$command = variable_get("islandora_fits_executable_path", "fits.sh") . " -i "
. $file . " -o " . $outfile;
$output = array();
// In case of disaster, fall back to the simplest possibly command line.
$command = "$fits -i $file -o $outfile";
exec($command, $output, $ret);
if ($ret == "0") {
return $outfile;
if ($ret == '0') {
if (verify_fits_xml($outfile)) {
return $outfile;
}
}
return FALSE;
}

/**
* Adds the given file as a datastream to the given object using the given
* datastream id to identify it.
* Helper to verify the fits xml file size is greater then 0.
*
* This function does not verify the contents of the provided xml file.
* WARNING: This function will delete said file if its file size
* is equel to 0.
*
* @param String $xml_file
* The path to the created xml file to validate
*/
function verify_fits_xml($xml_file) {
if (filesize($xml_file) > 0) {
return TRUE;
}
else {
// Fits wont write to a file if it already exists.
if (file_exists($xml_file)) {
drupal_unlink($xml_file);
}
return FALSE;
}
}

/**
* Adds the given file as a datastream to the given object.
*
* @param FedoraObject $object
* The object to add the datasteam to.
* The object to add the datasteam to.
* @param string $datastream_id
* The datastream id of the added datastream.
* @param string $file_uri
Expand All @@ -95,13 +120,13 @@ function islandora_fits_create_fits($file) {
function islandora_fits_add_datastream($object, $datastream_id, $file_uri) {
try {
$mime_detector = new MimeDetect();
$ds = $object->constructDatastream($datastream_id, "M");
$ds = $object->constructDatastream($datastream_id, 'M');
$ds->label = $datastream_id;
$ds->mimetype = $mime_detector->getMimetype($file_uri);
$ds->setContentFromFile(drupal_realpath($file_uri));
$object->ingestDatastream($ds);
}
catch (exception $e) {
drupal_set_message(t("@message", array("@message" => $e->getMessage())));
drupal_set_message(t('@message', array('@message' => $e->getMessage())));
}
}

0 comments on commit 1c200ba

Please sign in to comment.