$root = \OC\Files\Filesystem::getPath($linkItem['file_source']) . '/'; $images = array_map(function ($image) use($root) { return $root . $image; }, $images); } else { $root = ''; OCP\JSON::checkLoggedIn(); $user = OCP\User::getUser(); } session_write_close(); $eventSource = new OC_EventSource(); foreach ($images as $image) { $height = 200 * $scale; if ($square) { $width = 200 * $scale; } else { $width = 400 * $scale; } $userView = new \OC\Files\View('/' . $user); $preview = new \OC\Preview($user, 'files', '/' . $image, $width, $height); $preview->setKeepAspect(!$square); $fileInfo = $userView->getFileInfo('/files/' . $image); // if the thumbnails is already cached, get it directly from the filesystem to avoid decoding and re-encoding the image $imageName = substr($image, strlen($root)); if ($path = $preview->isCached($fileInfo->getId())) { $eventSource->send('preview', array('image' => $imageName, 'preview' => base64_encode($userView->file_get_contents('/' . $path)))); } else { $eventSource->send('preview', array('image' => $imageName, 'preview' => (string) $preview->getPreview())); } } $eventSource->close();
public function testCreationFromCached() { $sampleFile = '/' . $this->user . '/files/test.txt'; $this->rootView->file_put_contents($sampleFile, 'dummy file data'); // create base preview $x = 150; $y = 150; $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); $fileInfo = $this->rootView->getFileInfo($sampleFile); $fileId = $fileInfo['fileid']; $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true); // create smaller previews $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50); $isCached = $preview->isCached($fileId); $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached); }
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())); }