public function testIsCached() { $sourceFile = __DIR__ . '/../data/testimage.png'; $userId = $this->getUniqueID(); $this->createUser($userId, 'pass'); $storage = new Temporary(); $storage->mkdir('files'); $this->registerMount($userId, $storage, '/' . $userId); \OC_Util::tearDownFS(); \OC_Util::setupFS($userId); $preview = new \OC\Preview($userId, 'files'); $view = new View('/' . $userId . '/files'); $view->file_put_contents('test.png', file_get_contents($sourceFile)); $info = $view->getFileInfo('test.png'); $preview->setFile('test.png', $info); $preview->setMaxX(64); $preview->setMaxY(64); $this->assertFalse($preview->isCached($info->getId())); $preview->getPreview(); $this->assertEquals('thumbnails/' . $info->getId() . '/64-64.png', $preview->isCached($info->getId())); }
exit; } $sharedFile = \OC\Files\Filesystem::normalizePath($file); } if ($linkedItem['item_type'] === 'file') { $parent = $pathInfo['parent']; $path = $view->getPath($parent); $sharedFile = $pathInfo['name']; } $path = \OC\Files\Filesystem::normalizePath($path, false); if (substr($path, 0, 1) === '/') { $path = substr($path, 1); } if ($maxX === 0 || $maxY === 0) { \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST); \OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG); exit; } $root = 'files/' . $path; try { $preview = new \OC\Preview($userId, $root); $preview->setFile($sharedFile); $preview->setMaxX($maxX); $preview->setMaxY($maxY); $preview->setScalingUp($scalingUp); $preview->setKeepAspect($keepAspect); $preview->showPreview(); } catch (\Exception $e) { \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR); \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); }
/** * @brief offer preview for download * * if no preview exists for this file, send icon instead * * @param string $path full path to file * @param string type type of preview requested */ public static function servePreview($path, $type) { \OCP\User::checkLoggedIn(); \OC::$server->getSession()->close(); $i = \OC\Files\Filesystem::getFileInfo($path, false); /* check for predefined cover, if found replace $path with that of cover file */ $meta = Meta::get($i['fileid']); if ($meta['cover']) { $path = pathinfo($path)['dirname'] . '/' . $meta['cover']; $i = \OC\Files\Filesystem::getFileInfo($path, false); } if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) { $preview = new \OC\Preview(\OC_User::getUser(), 'files'); $preview->setFile($path); switch ($type) { case 'cover': $preview->setMaxX(Config::getApp('cover-x', '200')); $preview->setMaxY(Config::getApp('cover-y', '200')); break; case 'thumbnail': $preview->setMaxX(Config::getApp('thumb-x', '36')); $preview->setMaxY(Config::getApp('thumb-y', '36')); break; } $preview->showPreview(); } else { // no preview, serve icon instead $scheme = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http'; header("Location: " . $scheme . "://" . $_SERVER['HTTP_HOST'] . \OC_Helper::mimetypeIcon($i->getMimeType())); /* Note: relative URL should be enough (RFC7231) but some OPDS clients * (especially those in dedicated book readers) might not support them * * header("Location: " . \OC_Helper::mimetypeIcon($i->getMimeType())); */ } }