<commit_before><?php

namespace OC\Pictures;

class DatabaseManager {
	private static $instance = null;
	protected $cache = array();
	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;
		}
	}

	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);
		unset($image);
		$dir = dirname($path);
		$this->cache[$dir][$path] = $ret;
		return $ret;
	}

	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];
		}
		$image = new \OC_Image();
		if (!$image->loadFromFile($path)) {
			return false;
		}
		$ret = $this->setFileData($path, $image->width(), $image->height());
		unset($image);
		$this->cache[$dir][$path] = $ret;
		return $ret;
	}
	
	private function __construct() {}
}

class ThumbnailsManager {
	
	private static $instance = null;
	const TAG = 'ThumbnailManager';
	
	public static function getInstance() {
		if (self::$instance === null)
			self::$instance = new ThumbnailsManager();
		return self::$instance;
	}

	public function getThumbnail($path) {
		$gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
		if (file_exists($gallery_path.$path)) {
			return new \OC_Image($gallery_path.$path);
		}
		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;

		$image->fixOrientation();

		$ret = $image->preciseResize($this->getThumbnailWidth($image), $this->getThumbnailHeight($image));
		
		if (!$ret) {
			\OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
			unset($image);
			return false;
		}

		$image->save($gallery_path.'/'.$path);
		return $image;
	}

	public function getThumbnailWidth($image) {
		return floor((150*$image->widthTopLeft())/$image->heightTopLeft());
	}

	public function getThumbnailHeight($image) {
		return 150;
	}

	public function getThumbnailInfo($path) {
		$arr = DatabaseManager::getInstance()->getFileData($path);
		if (!$arr) {
			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), $this->getThumbnailHeight($image));
		}
		$ret = array('filepath' => $arr['path'],
					 'width' => $arr['width'],
					 'height' => $arr['height']);
		return $ret;
	}
	
	public function delete($path) {
		$thumbnail = \OCP\Config::getSystemValue('datadirectory').'/'.\OC_User::getUser()."/gallery".$path;
		if (file_exists($thumbnail)) {
			unlink($thumbnail);
		}
	}
	
	private function __construct() {}

}<commit_msg>fix thumbnail size calculation, use const for THUMBNAIL_HEIGHT<commit_after><?php

namespace OC\Pictures;

class DatabaseManager {        
	private static $instance = null;
	protected $cache = array();
	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;
		}
	}

	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);
		unset($image);
		$dir = dirname($path);
		$this->cache[$dir][$path] = $ret;
		return $ret;
	}

	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];
		}
		$image = new \OC_Image();
		if (!$image->loadFromFile($path)) {
			return false;
		}
		$ret = $this->setFileData($path, $image->width(), $image->height());
		unset($image);
		$this->cache[$dir][$path] = $ret;
		return $ret;
	}
	
	private function __construct() {}
}

class ThumbnailsManager {
	
	private static $instance = null;
	const TAG = 'ThumbnailManager';
        const THUMBNAIL_HEIGHT = 150;
	
	public static function getInstance() {
		if (self::$instance === null)
			self::$instance = new ThumbnailsManager();
		return self::$instance;
	}

	public function getThumbnail($path) {
		$gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
		if (file_exists($gallery_path.$path)) {
			return new \OC_Image($gallery_path.$path);
		}
		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;
                            
		$image->fixOrientation();
                
		$ret = $image->preciseResize( floor((self::THUMBNAIL_HEIGHT*$image->width())/$image->height()), self::THUMBNAIL_HEIGHT );
		
		if (!$ret) {
			\OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
			unset($image);
			return false;
		}
                
		$image->save($gallery_path.'/'.$path);
		return $image;
	}

	public function getThumbnailWidth($image) {
		return floor((self::THUMBNAIL_HEIGHT*$image->widthTopLeft())/$image->heightTopLeft());
	}

	public function getThumbnailInfo($path) {
		$arr = DatabaseManager::getInstance()->getFileData($path);
		if (!$arr) {
			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);
		}
		$ret = array('filepath' => $arr['path'],
					 'width' => $arr['width'],
					 'height' => $arr['height']);
		return $ret;
	}
	
	public function delete($path) {
		$thumbnail = \OCP\Config::getSystemValue('datadirectory').'/'.\OC_User::getUser()."/gallery".$path;
		if (file_exists($thumbnail)) {
			unlink($thumbnail);
		}
	}
	
	private function __construct() {}

}
{'fraction_non_alphanumeric': 0.20516304347826086, 'fraction_numerical': 0.0011116600790513835, 'mean_word_length': 4.441532258064516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5654413', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2805, 'n_words': 518}