managers.php 2.7 KB

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