nextcloud/apps/gallery/lib/managers.php

134 lines
3.9 KiB
PHP
Raw Normal View History

2012-06-02 17:25:50 +04:00
<?php
namespace OC\Pictures;
class DatabaseManager {
2012-06-02 17:25:50 +04:00
private static $instance = null;
protected $cache = array();
2012-06-02 17:25:50 +04:00
const TAG = 'DatabaseManager';
public static function getInstance() {
if (self::$instance === null)
self::$instance = new DatabaseManager();
return self::$instance;
}
protected function getPathData($path) {
$stmt = \OCP\DB::prepare('SELECT * FROM *PREFIX*pictures_images_cache
WHERE uid_owner LIKE ? AND path like ? AND path not like ?');
$path_match = $path.'/%';
$path_notmatch = $path.'/%/%';
$result = $stmt->execute(array(\OCP\USER::getUser(), $path_match, $path_notmatch));
$this->cache[$path] = array();
while (($row = $result->fetchRow()) != false) {
$this->cache[$path][$row['path']] = $row;
}
}
2012-07-05 23:09:48 +04:00
public function setFileData($path, $width, $height) {
$stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
$stmt->execute(array(\OCP\USER::getUser(), $path, $width, $height));
$ret = array('path' => $path, 'width' => $width, 'height' => $height);
2012-07-05 23:42:39 +04:00
$dir = dirname($path);
2012-07-05 23:09:48 +04:00
$this->cache[$dir][$path] = $ret;
return $ret;
}
2012-06-02 17:25:50 +04:00
public function getFileData($path) {
$gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
$path = $gallery_path.$path;
$dir = dirname($path);
if (!isset($this->cache[$dir])) {
$this->getPathData($dir);
}
if (isset($this->cache[$dir][$path])) {
return $this->cache[$dir][$path];
2012-06-02 17:25:50 +04:00
}
$image = new \OC_Image();
if (!$image->loadFromFile($path)) {
return false;
}
2012-07-05 23:09:48 +04:00
$ret = $this->setFileData($path, $image->width(), $image->height());
unset($image);
$this->cache[$dir][$path] = $ret;
2012-06-08 23:55:28 +04:00
return $ret;
2012-06-02 17:25:50 +04:00
}
private function __construct() {}
}
class ThumbnailsManager {
private static $instance = null;
const TAG = 'ThumbnailManager';
const THUMBNAIL_HEIGHT = 150;
2012-06-02 17:25:50 +04:00
public static function getInstance() {
if (self::$instance === null)
self::$instance = new ThumbnailsManager();
return self::$instance;
}
public function getThumbnail($path) {
$gallery_storage = \OCP\Files::getStorage('gallery');
if ($gallery_storage->file_exists($path)) {
return new \OC_Image($gallery_storage->getLocalFile($path));
2012-06-02 17:25:50 +04:00
}
if (!\OC_Filesystem::file_exists($path)) {
\OC_Log::write(self::TAG, 'File '.$path.' don\'t exists', \OC_Log::WARN);
return false;
}
$image = new \OC_Image();
$image->loadFromFile(\OC_Filesystem::getLocalFile($path));
if (!$image->valid()) return false;
2012-06-02 17:25:50 +04:00
$image->fixOrientation();
$ret = $image->preciseResize( floor((self::THUMBNAIL_HEIGHT*$image->width())/$image->height()), self::THUMBNAIL_HEIGHT );
2012-06-02 17:25:50 +04:00
if (!$ret) {
\OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
unset($image);
return false;
}
$l = $gallery_storage->getLocalFile($path);
$image->save($l);
2012-06-02 17:25:50 +04:00
return $image;
}
2012-07-05 23:09:48 +04:00
public function getThumbnailWidth($image) {
return floor((self::THUMBNAIL_HEIGHT*$image->widthTopLeft())/$image->heightTopLeft());
2012-07-05 23:09:48 +04:00
}
2012-06-02 17:25:50 +04:00
public function getThumbnailInfo($path) {
$arr = DatabaseManager::getInstance()->getFileData($path);
2012-06-10 15:04:42 +04:00
if (!$arr) {
2012-07-05 23:09:48 +04:00
if (!\OC_Filesystem::file_exists($path)) {
\OC_Log::write(self::TAG, 'File '.$path.' don\'t exists', \OC_Log::WARN);
return false;
}
$image = new \OC_Image();
$image->loadFromFile(\OC_Filesystem::getLocalFile($path));
if (!$image->valid()) {
return false;
}
$arr = DatabaseManager::getInstance()->setFileData($path, $this->getThumbnailWidth($image), self::THUMBNAIL_HEIGHT);
2012-06-10 15:04:42 +04:00
}
2012-06-02 17:25:50 +04:00
$ret = array('filepath' => $arr['path'],
2012-06-10 15:04:42 +04:00
'width' => $arr['width'],
'height' => $arr['height']);
2012-06-02 17:25:50 +04:00
return $ret;
}
public function delete($path) {
$thumbnail_storage = \OCP\Files::getStorage('gallery');
if ($thumbnail_storage->file_exists($path)) {
$thumbnail_storage->unlink($path);
}
2012-06-02 17:25:50 +04:00
}
private function __construct() {}
}