Beispiel #1
0
 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);
 }
 /**
  * return a preview of a file
  *
  * @param string $file The path to the file where you want a thumbnail from
  * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
  * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
  * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
  * @return \OCP\IImage
  */
 public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false)
 {
     $preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp);
     return $preview->getPreview();
 }
Beispiel #3
0
    $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();
Beispiel #4
0
 public function testKeepAspectRatioCover()
 {
     $originalWidth = 1680;
     $originalHeight = 1050;
     $originalAspectRation = $originalWidth / $originalHeight;
     $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', 150, 150);
     $preview->setKeepAspect(true);
     $preview->setMode(\OC\Preview::MODE_COVER);
     $image = $preview->getPreview();
     $aspectRatio = $image->width() / $image->height();
     $this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2));
     $this->assertGreaterThanOrEqual(150, $image->width());
     $this->assertGreaterThanOrEqual(150, $image->height());
 }
Beispiel #5
0
 /**
  * Tests if unsupported previews return an empty object
  */
 public function testUnsupportedPreviewsReturnEmptyObject()
 {
     $width = 400;
     $height = 200;
     // Previews for odt files are not enabled
     $imgData = file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.odt');
     $imgPath = '/' . self::TEST_PREVIEW_USER1 . '/files/testimage.odt';
     $this->rootView->file_put_contents($imgPath, $imgData);
     $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.odt', $width, $height);
     $preview->getPreview();
     $image = $preview->getPreview();
     $this->assertSame(false, $image->valid());
 }
Beispiel #6
0
 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()));
 }
Beispiel #7
0
 /**
  * @dataProvider txtBlacklist
  *
  * @param $extension
  * @param $data
  * @param $expectedResult
  */
 public function testIsTransparent($extension, $data, $expectedResult)
 {
     $x = 32;
     $y = 32;
     $sample = '/' . self::TEST_PREVIEW_USER1 . '/files/test.' . $extension;
     $this->rootView->file_put_contents($sample, $data);
     $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.' . $extension, $x, $y);
     $image = $preview->getPreview();
     $resource = $image->resource();
     //http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
     $colorIndex = imagecolorat($resource, 1, 1);
     $colorInfo = imagecolorsforindex($resource, $colorIndex);
     $this->assertSame($expectedResult, $colorInfo['alpha'] === 127, 'Failed asserting that only previews for text files are transparent.');
 }