Adapted DMSLib download method to be more generic

DMSLib now extends FHC Controller to use its new method 'outputFile',
which sets HTTP headers and reads the file.
DMSLib uses now download function to accept params from Controller like
own filename or special disposition.
Loading document and retrieving file info is outsourced into own method
to be reusable.

Signed-off-by: cris-technikum <hainberg@technikum-wien.at>
This commit is contained in:
Cris
2021-04-21 17:38:07 +02:00
committed by cris-technikum
parent 731c5b9461
commit 6dcb5e3a65
2 changed files with 112 additions and 33 deletions
+86 -33
View File
@@ -2,7 +2,7 @@
if (! defined('BASEPATH')) exit('No direct script access allowed');
class DmsLib
class DmsLib extends FHC_Controller
{
const FILE_CONTENT_PROPERTY = 'file_content';
@@ -21,6 +21,34 @@ class DmsLib
$this->ci->load->model('content/DmsVersion_model', 'DmsVersionModel');
$this->ci->load->model('content/DmsFS_model', 'DmsFSModel');
}
/**
* Load a DMS Document.
* If no version is particularly given, the latest version is loaded.
*
* @param $dms_id
* @param integer $version
* @return array
*/
public function load($dms_id, $version = null)
{
if (is_numeric($dms_id))
{
$this->ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id');
$this->ci->DmsModel->addOrder('version', 'DESC');
$this->ci->DmsModel->addLimit(1);
if (!is_numeric($version))
{
return $this->ci->DmsModel->load($dms_id);
}
else
{
return $this->ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version));
}
}
return error('The parameter DMS ID must be a number');
}
/**
* Read a DMS Document from the Filesystem
@@ -134,53 +162,78 @@ class DmsLib
}
/**
* Download a document
* Download a document.
*
* @param $dms_id
* @param null $filename If String is given, it will be used as filename on download
* @param string $filename $filename If String is given, it will be used as filename on download
* @param string $disposition [inline | attachment]
* Inline opens doc in new tab. Attachment displays download dialog box.
* Inline opens doc in new tab. Attachment displays download dialog box.
*/
public function download($dms_id, $filename = null, $disposition = 'inline')
{
if (!is_numeric($dms_id))
{
show_error('Wrong parameter');
}
$result = $this->getFileInfo($dms_id);
if ($disposition != 'inline' && $disposition != 'attachment')
{
$disposition = 'inline';
}
$this->ci->DmsVersionModel->addSelect('filename');
$result = $this->ci->DmsVersionModel->loadWhere(array('dms_id' => $dms_id));
if (isError($result))
{
show_error(getError($result));
return error(getError($result));
}
$fileObj = getData($result);
// Change filename, if filename is provided
if (is_string($filename))
{
$fileObj->name = $filename;
}
$filename = is_string($filename) && !isEmptyString($filename) ? $filename : $result->retval[0]->filename;
$file = DMS_PATH. $result->retval[0]->filename;
if (file_exists($file))
// Add file disposition
if ($disposition == 'attachment')
{
$finfo = new finfo(FILEINFO_MIME);
header('Content-Description: File Transfer');
header('Content-Type: '.$finfo->file($file));
header('Content-Disposition: '. $disposition. '; filename="'. $filename. '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
$fileObj->disposition = 'attachment';
}
else
{
show_error('File does not exist');
$fileObj->disposition = 'inline';
}
// Output file
if(!$this->outputFile($fileObj))
{
return error('Error on file output');
}
}
/**
* Get file information.
*
* @param $dms_id
* @param integer $version
* @return array with File Object.
*/
public function getFileInfo($dms_id, $version = null)
{
if (!is_numeric($dms_id))
{
return error('Wrong parameter');
}
// Load file
$result = $this->load($dms_id, $version);
if (isError($result))
{
return error(getError($result));
}
// Store file information in fileObj
$fileObj = new StdClass();
$fileObj->filename = getData($result)[0]->filename;
$fileObj->file = DMS_PATH. getData($result)[0]->filename;
$fileObj->name = DMS_PATH. getData($result)[0]->name; // original users filename
$fileObj->mimetype = DMS_PATH. getData($result)[0]->mimetype;
return success($fileObj);
}
/**