nextcloud/apps/gallery/lib/scanner.php

115 lines
3.5 KiB
PHP
Raw Normal View History

2011-10-02 14:24:02 +04:00
<?php
2011-09-26 00:32:08 +04:00
2012-01-08 14:01:25 +04:00
/**
* ownCloud - gallery application
*
* @author Bartek Przybylski
2012-03-09 19:28:26 +04:00
* @copyright 2012 Bartek Przybylski <bartek@alefzero.eu>
2012-01-08 14:01:25 +04:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-11 19:49:21 +04:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2012-01-08 14:01:25 +04:00
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
2012-03-11 19:49:21 +04:00
*
2012-01-08 14:01:25 +04:00
* You should have received a copy of the GNU Lesser General Public
2012-03-11 19:49:21 +04:00
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
2012-01-08 14:01:25 +04:00
*
*/
2011-12-08 23:04:56 +04:00
class OC_Gallery_Scanner {
2011-09-26 00:32:08 +04:00
2012-03-11 19:49:21 +04:00
public static function getGalleryRoot() {
return OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '/');
}
public static function getScanningRoot() {
return OC_Filesystem::getRoot().self::getGalleryRoot();
}
2011-09-26 00:32:08 +04:00
2012-03-11 19:49:21 +04:00
public static function cleanUp() {
OC_Gallery_Album::cleanup();
}
2012-03-11 19:49:21 +04:00
public static function createName($name) {
$name = basename($name);
return $name == '.' ? '' : $name;
}
2012-02-04 18:35:58 +04:00
2012-03-11 19:49:21 +04:00
// Scan single dir relative to gallery root
public static function scan($eventSource) {
$paths = self::findPaths();
$eventSource->send('count', count($paths));
$owner = OC_User::getUser();
foreach ($paths as $path) {
$name = self::createName($path);
$images = self::findFiles($path);
2011-09-26 00:32:08 +04:00
2012-03-11 19:49:21 +04:00
$result = OC_Gallery_Album::find($owner, null, $path);
// don't duplicate galleries with same path
if (!($albumId = $result->fetchRow())) {
OC_Gallery_Album::create($owner, $name, $path);
$result = OC_Gallery_Album::find($owner, $name);
$albumId = $result->fetchRow();
}
$albumId = $albumId['album_id'];
foreach ($images as $img) {
$result = OC_Gallery_Photo::find($albumId, $img);
if (!$result->fetchRow())
OC_Gallery_Photo::create($albumId, $img);
}
if (count($images))
self::createThumbnails($name, $images);
$eventSource->send('scanned', '');
}
$eventSource->send('done', 1);
}
2011-12-21 21:35:29 +04:00
2012-03-11 19:49:21 +04:00
public static function createThumbnails($albumName, $files) {
// create gallery thumbnail
$file_count = min(count($files), 10);
$thumbnail = imagecreatetruecolor($file_count*200, 200);
for ($i = 0; $i < $file_count; $i++) {
$image = OC_Gallery_Photo::getThumbnail($files[$i]);
if ($image && $image->valid()) {
imagecopyresampled($thumbnail, $image->resource(), $i*200, 0, 0, 0, 200, 200, 200, 200);
}
}
imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
}
2012-03-11 19:49:21 +04:00
public static function isPhoto($filename) {
$ext = strtolower(substr($filename, strrpos($filename, '.')+1));
return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif';
}
2011-09-26 00:32:08 +04:00
2012-03-11 19:49:21 +04:00
public static function findFiles($path) {
$images = OC_FileCache::searchByMime('image','', OC_Filesystem::getRoot().$path);
$new = array();
foreach ($images as $i)
if (strpos($i, '/',1) === FALSE)
$new[] = $path.$i;
return $new;
}
2012-03-11 19:49:21 +04:00
public static function findPaths() {
$images=OC_FileCache::searchByMime('image','', self::getScanningRoot());
$paths=array();
foreach($images as $image){
$path=dirname($image);
$path = self::getGalleryRoot().($path=='.'?'':$path);
if ($path !== '/') $path=rtrim($path,'/');
if(array_search($path,$paths)===false){
$paths[]=$path;
}
}
2012-03-11 19:49:21 +04:00
sort($paths);
return $paths;
}
2011-09-26 00:32:08 +04:00
}
2012-03-09 19:28:26 +04:00
2011-09-26 00:32:08 +04:00
?>