From 76034163f9ca4f2c612aa4e24737bc2df6000b21 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Mon, 1 Jan 2024 16:56:04 +0900 Subject: [PATCH] Improve thumbnail handling for image and video search results #2230 --- common/js/common.js | 8 +- .../integration_search.model.php | 30 ++--- .../models/FileSearchResult.php | 120 ++++++++++++++++++ 3 files changed, 139 insertions(+), 19 deletions(-) create mode 100644 modules/integration_search/models/FileSearchResult.php diff --git a/common/js/common.js b/common/js/common.js index c60e2883b..849e6be44 100644 --- a/common/js/common.js +++ b/common/js/common.js @@ -706,13 +706,17 @@ function _displayMultimedia(src, width, height, options) { var clsid = ""; var codebase = ""; var html = ""; + var background = "black"; width = parseInt(width, 10); height = parseInt(height, 10); - if(/\.(gif|jpg|jpeg|bmp|png)$/i.test(src)){ + if (/\.(gif|jpe?g|bmp|png|webp)$/i.test(src)){ html = ''; } else { - html = ''; + if (options.thumbnail) { + background += " url('" + options.thumbnail + "');background-size:cover;background-position:center center"; + } + html = ''; html += ''; html += ''; } diff --git a/modules/integration_search/integration_search.model.php b/modules/integration_search/integration_search.model.php index 19df55d41..6bb0d3cb3 100644 --- a/modules/integration_search/integration_search.model.php +++ b/modules/integration_search/integration_search.model.php @@ -169,44 +169,40 @@ class integration_searchModel extends module $args->isvalid = 'Y'; $args->direct_download = $direct_download=='Y'?'Y':'N'; $args->exclude_secret = 'Y'; - // Get a list of documents - $oFileAdminModel = getAdminModel('file'); + + // Get a list of files + $oFileAdminModel = FileAdminModel::getInstance(); $output = $oFileAdminModel->getFileList($args); if(!$output->toBool() || !$output->data) return $output; $list = array(); foreach($output->data as $key => $val) { - $obj = new stdClass; + $obj = new \Rhymix\Modules\Integration_Search\Models\FileSearchResult; + $obj->file_srl = $val->file_srl; $obj->filename = $val->source_filename; + $obj->uploaded_filename = $val->uploaded_filename; $obj->download_count = $val->download_count; - if(substr($val->download_url,0,2)=='./') $val->download_url = substr($val->download_url,2); - $obj->download_url = Context::getRequestUri().$val->download_url; + $obj->download_url = \RX_BASEURL . preg_replace('!^\.\/!', '', $val->download_url); $obj->target_srl = $val->upload_target_srl; $obj->file_size = $val->file_size; + // Images - if(preg_match('/\.(jpe?g|gif|png|webp)$/i', $val->source_filename)) + if(preg_match('/\.(jpe?g|gif|png|bmp|webp)$/i', $val->source_filename)) { $obj->type = 'image'; - - $thumbnail_path = sprintf('files/thumbnails/%s',getNumberingPath($val->file_srl, 3)); - if(!is_dir($thumbnail_path)) FileHandler::makeDir($thumbnail_path); - $thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, 120, 120, 'crop'); - $thumbnail_url = Context::getRequestUri().$thumbnail_file; - if(!file_exists($thumbnail_file)) FileHandler::createImageFile($val->uploaded_filename, $thumbnail_file, 120, 120, 'jpg', 'crop'); - $obj->src = sprintf('%s', $thumbnail_url, htmlspecialchars($obj->filename, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), 120, 120); - // Videos } elseif(Rhymix\Framework\Filters\FilenameFilter::isDirectDownload($val->source_filename)) { $obj->type = 'multimedia'; - $obj->src = sprintf('', $val->uploaded_filename); - // Others + if ($val->thumbnail_filename) + { + $obj->video_thumbnail_url = \RX_BASEURL . preg_replace('!^\.\/!', '', $val->thumbnail_filename); + } } else { $obj->type = 'binary'; - $obj->src = ''; } $list[] = $obj; diff --git a/modules/integration_search/models/FileSearchResult.php b/modules/integration_search/models/FileSearchResult.php new file mode 100644 index 000000000..a047cc298 --- /dev/null +++ b/modules/integration_search/models/FileSearchResult.php @@ -0,0 +1,120 @@ +type !== 'image') + { + return ''; + } + + $thumbnail_path = sprintf('files/thumbnails/%s', getNumberingPath($this->file_srl, 3)); + if(!is_dir($thumbnail_path)) + { + FileHandler::makeDir($thumbnail_path); + } + $thumbnail_file = sprintf('%s%dx%d.%s.jpg', $thumbnail_path, $width, $height ?: $width, $type); + $thumbnail_url = \RX_BASEURL . $thumbnail_file; + if (!file_exists($thumbnail_file)) + { + FileHandler::createImageFile($this->uploaded_filename, $thumbnail_file, $width, $height ?: $width, 'jpg', $type, 50); + } + return $thumbnail_url; + } + + /** + * Display video. + * + * @param int $width + * @param int $height + * @return string + */ + public function displayVideo(int $width = 120, int $height = 0): string + { + if ($this->type !== 'multimedia') + { + return ''; + } + + $options = new \stdClass; + if ($this->video_thumbnail_url) + { + $options->thumbnail = $this->video_thumbnail_url; + } + + return vsprintf('', [ + json_encode(\RX_BASEURL . preg_replace('!^\.\/!', '', $this->uploaded_filename)), + $width, + $height ?: $width, + json_encode($options), + ]); + } + + /** + * Magic method to generate the 'src' attribute for backward compatibility. + * + * For images, it returns a 120x120 thumbnail. + * For videos, it returns a 80x80 preview. + * For other types of files, this method returns an empty string. + */ + public function __get(string $key) + { + if ($key === 'src') + { + if ($this->type === 'image') + { + return vsprintf('%s', [ + $this->getThumbnail(120, 120), + escape($this->filename, false), + ]); + } + elseif ($this->type === 'multimedia') + { + return $this->displayVideo(80, 80); + } + else + { + return ''; + } + } + else + { + return null; + } + } +}