2012-12-14 07:19:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of preview documents
|
|
|
|
*
|
|
|
|
* @category DMS
|
2013-02-14 11:10:53 +00:00
|
|
|
* @package SeedDMS_Preview
|
2012-12-14 07:19:07 +00:00
|
|
|
* @license GPL 2
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2010, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for managing creation of preview images for documents.
|
|
|
|
*
|
|
|
|
* @category DMS
|
2013-02-14 11:10:53 +00:00
|
|
|
* @package SeedDMS_Preview
|
2012-12-14 07:19:07 +00:00
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2011, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
2013-02-14 11:10:53 +00:00
|
|
|
class SeedDMS_Preview_Previewer {
|
2012-12-14 07:19:07 +00:00
|
|
|
/**
|
|
|
|
* @var string $cacheDir location in the file system where all the
|
|
|
|
* cached data like thumbnails are located. This should be an
|
|
|
|
* absolute path.
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $previewDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var integer $width maximum width/height of resized image
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $width;
|
|
|
|
|
|
|
|
function __construct($previewDir, $width=40) {
|
|
|
|
if(!is_dir($previewDir)) {
|
2013-02-14 11:10:53 +00:00
|
|
|
if (!SeedDMS_Core_File::makeDir($previewDir)) {
|
2012-12-14 07:19:07 +00:00
|
|
|
$this->previewDir = '';
|
|
|
|
} else {
|
|
|
|
$this->previewDir = $previewDir;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->previewDir = $previewDir;
|
|
|
|
}
|
|
|
|
$this->width = intval($width);
|
|
|
|
}
|
|
|
|
|
2015-08-08 07:35:44 +00:00
|
|
|
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
|
|
|
$descriptorspec = array(
|
|
|
|
0 => array("pipe", "r"),
|
|
|
|
1 => array("pipe", "w"),
|
|
|
|
2 => array("pipe", "w")
|
|
|
|
);
|
|
|
|
$pipes = array();
|
|
|
|
|
|
|
|
$timeout += time();
|
|
|
|
$process = proc_open($cmd, $descriptorspec, $pipes);
|
|
|
|
if (!is_resource($process)) {
|
|
|
|
throw new Exception("proc_open failed on: " . $cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
do {
|
|
|
|
$timeleft = $timeout - time();
|
|
|
|
$read = array($pipes[1]);
|
|
|
|
stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
|
|
|
|
|
|
|
|
if (!empty($read)) {
|
|
|
|
$output .= fread($pipes[1], 8192);
|
|
|
|
}
|
|
|
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
|
|
|
|
|
|
|
if ($timeleft <= 0) {
|
|
|
|
proc_terminate($process);
|
|
|
|
throw new Exception("command timeout on: " . $cmd);
|
|
|
|
} else {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
|
2013-04-29 17:30:25 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the physical filename of the preview image on disk
|
|
|
|
*
|
|
|
|
* @param object $object document content or document file
|
|
|
|
* @param integer $width width of preview image
|
|
|
|
* @return string file name of preview image
|
|
|
|
*/
|
|
|
|
protected function getFileName($object, $width) { /* }}} */
|
|
|
|
$document = $object->getDocument();
|
|
|
|
$dir = $this->previewDir.'/'.$document->getDir();
|
|
|
|
switch(get_class($object)) {
|
|
|
|
case "SeedDMS_Core_DocumentContent":
|
2015-09-03 13:13:06 +00:00
|
|
|
$target = $dir.'p'.$object->getVersion().'-'.$width;
|
2013-04-29 17:30:25 +00:00
|
|
|
break;
|
|
|
|
case "SeedDMS_Core_DocumentFile":
|
2015-09-03 13:13:06 +00:00
|
|
|
$target = $dir.'f'.$object->getID().'-'.$width;
|
2013-04-29 17:30:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $target;
|
|
|
|
} /* }}} */
|
|
|
|
|
2015-09-03 13:13:06 +00:00
|
|
|
/**
|
|
|
|
* Create a preview image for a given file
|
|
|
|
*
|
|
|
|
* @param string $infile name of input file including full path
|
|
|
|
* @param string $dir directory relative to $this->previewDir
|
|
|
|
* @param string $mimetype MimeType of input file
|
|
|
|
* @param integer $width width of generated preview image
|
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
|
|
|
public function createRawPreview($infile, $dir, $mimetype, $width=0, $target='') { /* {{{ */
|
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
|
|
|
if(!is_dir($this->previewDir.'/'.$dir)) {
|
|
|
|
if (!SeedDMS_Core_File::makeDir($this->previewDir.'/'.$dir)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!file_exists($infile))
|
|
|
|
return false;
|
|
|
|
if(!$target)
|
|
|
|
$target = $this->previewDir.$dir.md5($infile).'-'.$width;
|
|
|
|
if($target != '' && (!file_exists($target.'.png') || filectime($target.'.png') < filectime($infile))) {
|
|
|
|
$cmd = '';
|
|
|
|
switch($mimetype) {
|
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpeg":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/svg+xml":
|
|
|
|
$cmd = 'convert -resize '.$width.'x '.$infile.' '.$target.'.png';
|
|
|
|
break;
|
|
|
|
case "application/pdf":
|
|
|
|
case "application/postscript":
|
|
|
|
$cmd = 'convert -density 100 -resize '.$width.'x '.$infile.'[0] '.$target.'.png';
|
|
|
|
break;
|
|
|
|
case "text/plain":
|
|
|
|
$cmd = 'convert -resize '.$width.'x '.$infile.'[0] '.$target.'.png';
|
|
|
|
break;
|
|
|
|
case "application/x-compressed-tar":
|
|
|
|
$cmd = 'tar tzvf '.$infile.' | convert -density 100 -resize '.$width.'x text:-[0] '.$target.'.png';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if($cmd) {
|
|
|
|
//exec($cmd);
|
|
|
|
try {
|
|
|
|
self::execWithTimeout($cmd);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} /* }}} */
|
|
|
|
|
2013-04-29 17:30:25 +00:00
|
|
|
public function createPreview($object, $width=0) { /* {{{ */
|
2015-09-03 13:13:06 +00:00
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
$document = $object->getDocument();
|
|
|
|
$file = $document->_dms->contentDir.$object->getPath();
|
|
|
|
$target = $this->getFileName($object, $width);
|
|
|
|
return $this->createRawPreview($file, $document->getDir(), $object->getMimeType(), $width, $target);
|
|
|
|
|
2012-12-14 07:19:07 +00:00
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
2013-04-29 17:30:25 +00:00
|
|
|
$document = $object->getDocument();
|
2012-12-14 07:19:07 +00:00
|
|
|
$dir = $this->previewDir.'/'.$document->getDir();
|
|
|
|
if(!is_dir($dir)) {
|
2013-02-14 11:10:53 +00:00
|
|
|
if (!SeedDMS_Core_File::makeDir($dir)) {
|
2012-12-14 07:19:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 17:30:25 +00:00
|
|
|
$file = $document->_dms->contentDir.$object->getPath();
|
2012-12-14 07:19:07 +00:00
|
|
|
if(!file_exists($file))
|
|
|
|
return false;
|
2013-04-29 17:30:25 +00:00
|
|
|
$target = $this->getFileName($object, $width);
|
2015-09-03 13:13:06 +00:00
|
|
|
if($target !== false && (!file_exists($target.'.png') || filectime($target.'.png') < $object->getDate())) {
|
2012-12-14 07:19:07 +00:00
|
|
|
$cmd = '';
|
2013-04-29 17:30:25 +00:00
|
|
|
switch($object->getMimeType()) {
|
2012-12-14 07:19:07 +00:00
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpeg":
|
|
|
|
case "image/jpg":
|
2014-03-18 09:04:12 +00:00
|
|
|
case "image/svg+xml":
|
2015-09-03 13:13:06 +00:00
|
|
|
$cmd = 'convert -resize '.$width.'x '.$file.' '.$target.'.png';
|
2012-12-14 07:19:07 +00:00
|
|
|
break;
|
|
|
|
case "application/pdf":
|
2014-03-18 09:04:12 +00:00
|
|
|
case "application/postscript":
|
2015-09-03 13:13:06 +00:00
|
|
|
$cmd = 'convert -density 100 -resize '.$width.'x '.$file.'[0] '.$target.'.png';
|
2014-03-18 09:04:12 +00:00
|
|
|
break;
|
|
|
|
case "text/plain":
|
2015-09-03 13:13:06 +00:00
|
|
|
$cmd = 'convert -resize '.$width.'x '.$file.'[0] '.$target.'.png';
|
2014-03-18 09:04:12 +00:00
|
|
|
break;
|
|
|
|
case "application/x-compressed-tar":
|
2015-09-03 13:13:06 +00:00
|
|
|
$cmd = 'tar tzvf '.$file.' | convert -density 100 -resize '.$width.'x text:-[0] '.$target.'.png';
|
2014-03-18 09:04:12 +00:00
|
|
|
break;
|
2012-12-14 07:19:07 +00:00
|
|
|
}
|
|
|
|
if($cmd) {
|
2015-08-08 07:35:44 +00:00
|
|
|
//exec($cmd);
|
|
|
|
try {
|
|
|
|
self::execWithTimeout($cmd);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
}
|
2012-12-14 07:19:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} /* }}} */
|
|
|
|
|
2015-09-03 13:13:06 +00:00
|
|
|
public function hasRawPreview($infile, $dir, $width=0) { /* {{{ */
|
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
|
|
|
$target = $this->previewDir.$dir.md5($infile).'-'.$width;
|
|
|
|
if($target !== false && file_exists($target.'.png') && filectime($target.'.png') >= filectime($infile)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} /* }}} */
|
|
|
|
|
2013-04-29 17:30:25 +00:00
|
|
|
public function hasPreview($object, $width=0) { /* {{{ */
|
2012-12-14 07:19:07 +00:00
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
2013-04-29 17:30:25 +00:00
|
|
|
$target = $this->getFileName($object, $width);
|
2015-09-03 13:13:06 +00:00
|
|
|
if($target !== false && file_exists($target.'.png') && filectime($target.'.png') >= $object->getDate()) {
|
2012-12-14 07:19:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} /* }}} */
|
|
|
|
|
2015-09-03 13:13:06 +00:00
|
|
|
public function getRawPreview($infile, $dir, $width=0) { /* {{{ */
|
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$target = $this->previewDir.$dir.md5($infile).'-'.$width;
|
|
|
|
if($target && file_exists($target.'.png')) {
|
|
|
|
readfile($target.'.png');
|
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
|
2013-04-29 17:30:25 +00:00
|
|
|
public function getPreview($object, $width=0) { /* {{{ */
|
2012-12-14 07:19:07 +00:00
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
2013-04-29 17:30:25 +00:00
|
|
|
|
|
|
|
$target = $this->getFileName($object, $width);
|
2015-09-03 13:13:06 +00:00
|
|
|
if($target && file_exists($target.'.png')) {
|
|
|
|
readfile($target.'.png');
|
2012-12-14 07:19:07 +00:00
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
|
2013-04-29 17:30:25 +00:00
|
|
|
public function deletePreview($document, $object, $width=0) { /* {{{ */
|
2012-12-14 07:19:07 +00:00
|
|
|
if($width == 0)
|
|
|
|
$width = $this->width;
|
|
|
|
else
|
|
|
|
$width = intval($width);
|
|
|
|
if(!$this->previewDir)
|
|
|
|
return false;
|
2013-04-29 17:30:25 +00:00
|
|
|
|
|
|
|
$target = $this->getFileName($object, $width);
|
2012-12-14 07:19:07 +00:00
|
|
|
} /* }}} */
|
|
|
|
}
|
|
|
|
?>
|