/ */ namespace Files; use Files\MimeType; use Zend\Mvc\MvcEvent; class Module { /** * Add a basic preview handler for primitive (web-safe) types. */ public function onBootstrap(MvcEvent $event) { $application = $event->getApplication(); $services = $application->getServiceManager(); $formats = $services->get('formats'); $url = $services->get('viewhelpermanager')->get('url'); $events = $services->get('queue')->getEventManager(); // attach to archive cleanup event $events->attach( 'task.cleanup.archive', function ($event) use ($services) { $archiveFile = $event->getParam('id'); $data = $event->getParam('data'); $statusFile = isset($data['statusFile']) ? $data['statusFile'] : null; try { $result = $services->get('archiver')->removeArchive($archiveFile, $statusFile); } catch (\Exception $e) { $services->get('logger')->err($e); } } ); $formats->addHandler( new Format\Handler( // can-preview callback function ($file, $extension, $mimeType, $request) use ($url) { $isWebSafeImage = MimeType::isWebSafeImage($mimeType); if ($request && $request->getUri()->getPath() == $url('diff') && $file->isText() && !$isWebSafeImage ) { return false; } return $file->isText() || strpos($mimeType, '/pdf') || $isWebSafeImage; }, // render-preview callback function ($file, $extension, $mimeType, $request) use ($services) { $helpers = $services->get('ViewHelperManager'); $url = $helpers->get('url'); $escapeUrl = $helpers->get('escapeUrl'); $viewUrl = $url('view', array('path' => trim($file->getDepotFilename(), '/'))) . '?v=' . $escapeUrl($file->getRevspec()); if (strpos($mimeType, '/pdf')) { return '
' . '' . '

It appears you don\'t have a pdf plugin for this browser.

' . '
' . '
'; } if (MimeType::isWebSafeImage($mimeType)) { return '
' . '' . '
'; } // making it this far means that the file must be text $fileSize = $helpers->get('fileSize'); $escapeHtml = $helpers->get('escapeHtml'); $isPlain = $extension === 'txt' || !strlen($extension); $maxSize = 1048576; // 1MB $contents = $file->getDepotContents( array( $file::UTF8_CONVERT => true, $file::UTF8_SANITIZE => true, $file::MAX_FILESIZE => $maxSize ), $cropped ); return '
'
                         .  $escapeHtml($contents)
                         .  ($cropped ? 'Snip (>' . $fileSize($maxSize) . ')' : '')
                         . '
'; } ), 'default' ); } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getAutoloaderConfig() { return array( 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } }