managers.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace OC\Pictures;
  3. class DatabaseManager {
  4. private static $instance = null;
  5. const TAG = 'DatabaseManager';
  6. public static function getInstance() {
  7. if (self::$instance === null)
  8. self::$instance = new DatabaseManager();
  9. return self::$instance;
  10. }
  11. public function getFileData($path) {
  12. $gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
  13. $path = $gallery_path.$path;
  14. $stmt = \OCP\DB::prepare('SELECT * FROM *PREFIX*pictures_images_cache WHERE uid_owner LIKE ? AND path = ?');
  15. $result = $stmt->execute(array(\OCP\USER::getUser(), $path));
  16. if (($row = $result->fetchRow()) != false) {
  17. return $row;
  18. }
  19. $image = new \OC_Image();
  20. if (!$image->loadFromFile($path)) {
  21. return false;
  22. }
  23. $stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
  24. $stmt->execute(array(\OCP\USER::getUser(), $path, $image->width(), $image->height()));
  25. $ret = array('path' => $path, 'width' => $image->width(), 'height' => $image->height());
  26. unset($image);
  27. return $ret;
  28. }
  29. private function __construct() {}
  30. }
  31. class ThumbnailsManager {
  32. private static $instance = null;
  33. const TAG = 'ThumbnailManager';
  34. public static function getInstance() {
  35. if (self::$instance === null)
  36. self::$instance = new ThumbnailsManager();
  37. return self::$instance;
  38. }
  39. public function getThumbnail($path) {
  40. $gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
  41. if (file_exists($gallery_path.$path)) {
  42. return new \OC_Image($gallery_path.$path);
  43. }
  44. if (!\OC_Filesystem::file_exists($path)) {
  45. \OC_Log::write(self::TAG, 'File '.$path.' don\'t exists', \OC_Log::WARN);
  46. return false;
  47. }
  48. $image = new \OC_Image();
  49. $image->loadFromFile(\OC_Filesystem::getLocalFile($path));
  50. if (!$image->valid()) return false;
  51. $image->fixOrientation();
  52. $ret = $image->preciseResize(floor((150*$image->width())/$image->height()), 150);
  53. if (!$ret) {
  54. \OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
  55. unset($image);
  56. return false;
  57. }
  58. $image->save($gallery_path.'/'.$path);
  59. return $image;
  60. }
  61. public function getThumbnailInfo($path) {
  62. $arr = DatabaseManager::getInstance()->getFileData($path);
  63. if (!$arr) {
  64. $thubnail = $this->getThumbnail($path);
  65. unset($thubnail);
  66. $arr = DatabaseManager::getInstance()->getFileData($path);
  67. }
  68. $ret = array('filepath' => $arr['path'],
  69. 'width' => $arr['width'],
  70. 'height' => $arr['height']);
  71. return $ret;
  72. }
  73. public function delete($path) {
  74. unlink(\OCP\Config::getSystemValue('datadirectory').'/'.\OC_User::getUser()."/gallery".$path);
  75. }
  76. private function __construct() {}
  77. }
  78. ?>