Browse Source

[UPDATE] file previewing

experimental
Georgi Hristov 2 years ago
parent
commit
9df1768629
5 changed files with 123 additions and 69 deletions
  1. +7
    -4
      modules/Utils/Attachment/AttachmentCommon_0.php
  2. +24
    -34
      modules/Utils/Attachment/FileActionHandler.php
  3. +63
    -20
      modules/Utils/FileStorage/ActionHandler.php
  4. +26
    -9
      modules/Utils/FileStorage/FileStorageCommon_0.php
  5. +3
    -2
      modules/Utils/RecordBrowser/RecordBrowserCommon_0.php

+ 7
- 4
modules/Utils/Attachment/AttachmentCommon_0.php View File

@@ -279,10 +279,13 @@ class Utils_AttachmentCommon extends ModuleCommon {
self::$mark_as_read = array();
}

$text = (!$view?'<b style="float:left;margin-right:30px;">'.$row['title'].'</b> ':''). $text . self::display_files($row, $nolink);
$text = (!$view && $row['title']?'<b style="float:left;margin-right:30px;">'.$row['title'].'</b> ':''). $text;
if($row['sticky']) $text = '<img src="'.Base_ThemeCommon::get_template_file('Utils_Attachment','sticky.png').'" hspace=3 align="left"> '.$text;

return $text;
$files = self::display_files($row, $nolink);
return implode('<br><br>', array_filter([$text, $files]));
}
public static function display_files($row, $nolink = false, $desc = null, $tab = null) {
@@ -302,8 +305,8 @@ class Utils_AttachmentCommon extends ModuleCommon {
}
}
$inline_nodes = array_filter($inline_nodes);
return implode('<br>', $labels) . ($inline_nodes? '<hr>': '') . implode('<hr>', $inline_nodes);
return implode('<br>', $labels) . ($inline_nodes? '<hr>': '') . implode('&nbsp;', $inline_nodes);
}

public static function description_callback($row,$nolink=false) {


+ 24
- 34
modules/Utils/Attachment/FileActionHandler.php View File

@@ -44,7 +44,7 @@ class Utils_Attachment_FileActionHandler
protected function getFile(Request $request, $disposition)
{
$filestorageId = $request->get('id');
$type = $request->get('action');
$action = $request->get('action');
$crypted = $request->get('crypted');
$recordId = $request->get('record');
$filePack = is_array($filestorageId);
@@ -64,50 +64,41 @@ class Utils_Attachment_FileActionHandler
$size = 0;
foreach ($filestorageIds as $filestorageId) {
$meta = Utils_FileStorageCommon::meta($filestorageId);
$buffer = Utils_FileStorageCommon::read_content($filestorageId);
if($crypted) {
$buffer = Utils_AttachmentCommon::decrypt($buffer, $_SESSION['client']['cp'.$recordId]);
if (!$filePack && $action == 'inline' && ($thumbnail = $this->createThumbnail($meta))) {
$mime = $thumbnail['mime'];
$filename = $thumbnail['filename'];
$buffer = $thumbnail['contents'];
}
else {
$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename']);
$filename = $meta['filename'];
$buffer = Utils_FileStorageCommon::read_content($filestorageId);
if ($buffer===false) throw new Utils_FileStorage_Exception('File decryption error');
if($crypted) {
$buffer = Utils_AttachmentCommon::decrypt($buffer, $_SESSION['client']['cp'.$recordId]);
if ($buffer===false) throw new Utils_FileStorage_Exception('File decryption error');
}
}
$size += filesize($meta['file']);
@ini_set('memory_limit',ceil($size*2/1024/1024+64).'M');
if ($filePack)
$zip->addFromString($meta['filename'], $buffer);
$zip->addFromString($filename, $buffer);
}
if ($filePack)
$zip->close();
} catch (Utils_FileStorage_Exception $ex) {
if (Base_AclCommon::i_am_admin()) {
return new Response($ex->getMessage(), 400);
}
return false;
return Acl::i_am_admin()? new Response($ex->getMessage(), 400): false;
}
$type = self::actions[$type];
$time = time();
foreach ($filestorageIds as $filestorageId) {
$remote_address = get_client_ip_address();
$remote_host = gethostbyaddr($remote_address);
DB::Execute('INSERT INTO utils_filestorage_access(file_id,date_accessed,accessed_by,type,ip_address,host_name) '.
'VALUES (%d,%T,%d,%d,%s,%s)',array($filestorageId,$time,Acl::get_user()?Acl::get_user():0,$type,$remote_address,$remote_host));
}
$this->logFileAccessed($filestorageId, $action);
$response = new Response();
if (!$filePack) {
$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename']);
$response->setContent($buffer);
$response->headers->set('Content-Type', $mime);
$response->headers->set('Content-Length', strlen($buffer));
$response->headers->set('Content-Disposition', "$disposition; filename=\"$meta[filename]\"");
}
else {
if ($filePack) {
ob_start();
$fp = fopen($zipFilename, 'rb');
while (!feof($fp)) {
@@ -117,13 +108,12 @@ class Utils_Attachment_FileActionHandler
@unlink($zipFilename);
$buffer = ob_get_clean();
$response->setContent($buffer);
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Length', strlen($buffer));
$response->headers->set('Content-Disposition', "attachment; filename=note_".$recordId.'.zip');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
$response = $this->createFileResponse($buffer, 'application/zip', 'attachment', "note_$recordId.zip", true);
}
else {
$response = $this->createFileResponse($buffer, $mime, $disposition, $filename);
}
return $response;
}
}

+ 63
- 20
modules/Utils/FileStorage/ActionHandler.php View File

@@ -10,9 +10,9 @@ class Utils_FileStorage_ActionHandler
*
* @var array Possible actions to execute
*/
protected $allowedActions = ['download', 'preview', 'remote'];
protected $allowedActions = ['download', 'preview', 'inline', 'remote'];

const actions = ['download'=>0, 'preview'=>1, 'remote'=>2];
const actions = ['download'=>0, 'preview'=>1, 'inline' => 1, 'remote'=>2];

/**
* You can override this variable to allow access for not logged in users
@@ -34,7 +34,7 @@ class Utils_FileStorage_ActionHandler

protected function getHandlingScript()
{
return 'modules/Utils/FileStorage/file.php';
return get_epesi_url() . 'modules/Utils/FileStorage/file.php';
}

protected function getRemoteScript()
@@ -94,7 +94,8 @@ class Utils_FileStorage_ActionHandler
case 'download':
return $this->getFile($request, 'attachment');
case 'preview':
return $this->getFile($request, 'inline');
case 'inline':
return $this->getFile($request, 'inline');
case 'remote':
switch($method) {
case Request::METHOD_POST:
@@ -109,34 +110,76 @@ class Utils_FileStorage_ActionHandler
protected function getFile(Request $request, $disposition)
{
$filestorageId = $request->get('id');
$type = $request->get('action');
$action = $request->get('action');
try {
$meta = Utils_FileStorageCommon::meta($filestorageId);
$buffer = Utils_FileStorageCommon::read_content($filestorageId);

if ($action == 'inline' && ($thumbnail = $this->createThumbnail($meta))) {
$mime = $thumbnail['mime'];
$filename = $thumbnail['filename'];
$buffer = $thumbnail['contents'];
}
else {
$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename']);
$filename = $meta['filename'];
$buffer = Utils_FileStorageCommon::read_content($filestorageId);
}
} catch (Utils_FileStorage_Exception $ex) {
if (Base_AclCommon::i_am_admin()) {
return new Response($ex->getMessage(), 400);
}
return false;
}
$type = self::actions[$type];

$remote_address = get_client_ip_address();
$remote_host = gethostbyaddr($remote_address);
DB::Execute('INSERT INTO utils_filestorage_access(file_id,date_accessed,accessed_by,type,ip_address,host_name) '.
'VALUES (%d,%T,%d,%d,%s,%s)',array($filestorageId,time(),Acl::get_user()?Acl::get_user():0,$type,$remote_address,$remote_host));
$this->logFileAccessed($filestorageId, $action);

$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename']);

$response = new Response();
$response->setContent($buffer);
$response->headers->set('Content-Type', $mime);
$response->headers->set('Content-Length', strlen($buffer));
$response->headers->set('Content-Disposition', "$disposition; filename=\"$meta[filename]\"");
return $response;
return $this->createFileResponse($buffer, $mime, $disposition, $filename);
}
protected function createFileResponse($content, $mime, $disposition, $filename, $nocache = false) {
$response = new Response();
$response->setContent($content);
$response->headers->set('Content-Type', $mime);
$response->headers->set('Content-Length', strlen($content));
$response->headers->set('Content-Disposition', "$disposition; filename=\"$filename\"");
if ($nocache) {
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
}
return $response;
}
protected function logFileAccessed($filestorageId, $action, $time = null) {
$remote_address = get_client_ip_address();
$remote_host = gethostbyaddr($remote_address);
DB::Execute('INSERT INTO utils_filestorage_access(file_id,date_accessed,accessed_by,type,ip_address,host_name) ' . 'VALUES (%d,%T,%d,%d,%s,%s)', [
$filestorageId,
$time ?: time(),
Acl::get_user() ?: 0,
self::actions[$action],
$remote_address,
$remote_host
]);
}

protected function createThumbnail($meta)
{
if (!Utils_FileStorageCommon::get_pdf_thumbnail_possible($meta)) return false;
$image = new Imagick($meta['file'] . '[0]');
$image->setImageFormat('jpg');
$mime = 'image/jpeg';
$filename = 'preview.jpeg';
$contents = $image . '';
return compact('mime', 'filename', 'contents');
}
protected function createRemote(Request $request)
{
$params = $request->query->all();


+ 26
- 9
modules/Utils/FileStorage/FileStorageCommon_0.php View File

@@ -37,7 +37,7 @@ class Utils_FileStorageCommon extends ModuleCommon {
*
* @return string File label with link
*/
public static function get_file_label($id, $nolink = false, $icon = true, $action_urls = null)
public static function get_file_label($id, $nolink = false, $icon = true, $action_urls = null, $label = null, $inline = false)
{
$file_exists = self::file_exists($id, false);
@@ -50,16 +50,17 @@ class Utils_FileStorageCommon extends ModuleCommon {
$icon_img = '';
}
$filename = '';
$meta = null;
try {
$meta = is_numeric($id) ? self::meta($id) : $id;
$filename = $meta['filename'];
} catch (Exception $e) {
}
if (!$filename = $label) {
$filename = ($meta['filename']?? '') ?: htmlspecialchars('<' . __('missing filename') . '>');
}
$filename = $filename ?: htmlspecialchars('<' . __('missing filename') . '>');
if ($nolink) {
if ($nolink || !$meta) {
return $filename . ($file_exists ? '': ' [' . __('missing file') . ']');
}
@@ -78,10 +79,13 @@ class Utils_FileStorageCommon extends ModuleCommon {
$link_href = Utils_TooltipCommon::open_tag_attrs($tooltip_text);
}
}
return '<div class="file_link"><a ' . $link_href . '>' . $icon_img . '<span class="file_name">' . $filename . '</span></a></div>';
$ret = '<a ' . $link_href . '>' . $icon_img . '<span class="file_name">' . $filename . '</span></a>';
return $inline? $ret: '<div class="file_link">'.$ret.'</div>';
}
public static function get_file_inline_node($id, $action_urls = null)
public static function get_file_inline_node($id, $action_urls = null, $max_width = '200px')
{
if (!self::file_exists($id, false)) return '';
@@ -90,15 +94,22 @@ class Utils_FileStorageCommon extends ModuleCommon {
if ($action_urls === null) {
$action_urls = self::get_default_action_urls($meta['id']);
}
$max_width .= is_numeric($max_width)? 'px': '';

$type = self::get_mime_type($meta['file'], $meta['filename'], null, false);
switch ($type) {
case 'application/pdf':
if (!self::get_pdf_thumbnail_possible($meta)) {
$ret = '';
break;
}
// image
case 'image/jpeg':
case 'image/gif':
case 'image/png':
case 'image/bmp':
$ret = '<a href="' . $action_urls['preview'] . '" target="_blank"><img src="' . $action_urls['preview'] . '" class="file_inline" style="max-width: 100%" /></a>';
$ret = '<a href="' . $action_urls['preview'] . '" target="_blank"><img src="' . $action_urls['inline'] . '" class="file_inline" style="max-width: ' . $max_width . '" /></a>';
break;

default:
@@ -109,6 +120,12 @@ class Utils_FileStorageCommon extends ModuleCommon {
return $ret;
}
public static function get_pdf_thumbnail_possible($meta) {
$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename'], null, false);

return $mime == 'application/pdf' && class_exists('Imagick');
}
public static function get_default_action_urls($meta) {
$id = is_numeric($meta)? $meta: $meta['id'];


+ 3
- 2
modules/Utils/RecordBrowser/RecordBrowserCommon_0.php View File

@@ -3574,12 +3574,13 @@ class Utils_RecordBrowserCommon extends ModuleCommon {
if(!empty($fileStorageId)) {
$actions = $fileHandler->getActionUrlsRB($fileStorageId, $tab, $r['id'], $desc['id']);
$labels[]= Utils_FileStorageCommon::get_file_label($fileStorageId, $nolink, true, $actions);
$inline_nodes[]= Utils_FileStorageCommon::get_file_inline_node($fileStorageId, $actions);
if (!$desc['nopreview']?? false)
$inline_nodes[]= Utils_FileStorageCommon::get_file_inline_node($fileStorageId, $actions, $desc['max-width']?? '200px');
}
}
$inline_nodes = array_filter($inline_nodes);
return implode('<br>', $labels) . ($inline_nodes? '<hr>': '') . implode('<hr>', $inline_nodes);
return implode('<br>', $labels) . ($inline_nodes? '<hr>': '') . implode('&nbsp;', $inline_nodes);
}

public static function QFfield_file(&$form, $field, $label, $mode, $default, $desc, $rb_obj)


Loading…
Cancel
Save