From 82c224ee7a0bd87373ceba3bf5ed9eefe29514fc Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Wed, 18 Apr 2012 00:43:16 +0200 Subject: [PATCH] MDL-32471 send_stored_file() now supports the preview option If for any reason the preview image can't be generated for the given file (eg we do not support its mimetype yet), the function will send 404 Not Found HTTP header. This will be useful for the lazy loading of the file thumbnails as the JavaScript will simply ignore such response and will not replace the default icon for the file. In the future, a fallback thumbnail generator can be implemented that would generate some sort of default preview for all files (eg using the mimetype icon like some desktop OS do). --- lib/filelib.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/filelib.php b/lib/filelib.php index eebedb09b56..ea0e4fcd8d6 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1984,6 +1984,23 @@ function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownl $dontdie = true; } + if (!empty($options['preview'])) { + // replace the file with its preview + $fs = get_file_storage(); + $stored_file = $fs->get_file_preview($stored_file, $options['preview']); + if (!$stored_file) { + // unable to create a preview of the file + send_header_404(); + die(); + } else { + // preview images have fixed cache lifetime and they ignore forced download + // (they are generated by GD and therefore they are considered reasonably safe). + $lifetime = DAYSECS; + $filter = 0; + $forcedownload = false; + } + } + if (!$stored_file or $stored_file->is_directory()) { // nothing to serve if ($dontdie) {