2011-10-02 10:24:02 +00:00
|
|
|
<?php
|
2011-09-25 20:32:08 +00:00
|
|
|
|
2012-01-08 10:01:25 +00:00
|
|
|
/**
|
|
|
|
* ownCloud - gallery application
|
|
|
|
*
|
|
|
|
* @author Bartek Przybylski
|
2012-03-09 15:28:26 +00:00
|
|
|
* @copyright 2012 Bartek Przybylski <bartek@alefzero.eu>
|
2012-01-08 10:01:25 +00: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
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-03-09 15:28:26 +00:00
|
|
|
require_once('../../../lib/base.php');
|
2011-09-25 20:32:08 +00:00
|
|
|
|
2011-12-08 19:04:56 +00:00
|
|
|
class OC_Gallery_Scanner {
|
2011-09-25 20:32:08 +00:00
|
|
|
|
2012-03-09 15:28:26 +00:00
|
|
|
public static function getScanningRoot() {
|
|
|
|
return OC_Filesystem::getRoot().OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '/');
|
|
|
|
}
|
|
|
|
|
2011-09-25 20:32:08 +00:00
|
|
|
public static function scan($root) {
|
|
|
|
$albums = array();
|
|
|
|
self::scanDir($root, $albums);
|
|
|
|
return $albums;
|
|
|
|
}
|
|
|
|
|
2011-10-18 19:43:33 +00:00
|
|
|
public static function cleanUp() {
|
2012-03-09 15:28:26 +00:00
|
|
|
OC_Gallery_Album::cleanup();
|
2011-10-18 19:43:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 14:35:58 +00:00
|
|
|
public static function createName($name) {
|
2012-03-04 17:28:41 +00:00
|
|
|
return basename($name);
|
2012-02-04 14:35:58 +00:00
|
|
|
}
|
|
|
|
|
2011-09-25 20:32:08 +00:00
|
|
|
public static function scanDir($path, &$albums) {
|
|
|
|
$current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
|
2012-02-04 14:35:58 +00:00
|
|
|
$current_album['name'] = self::createName($current_album['name']);
|
2011-09-25 20:32:08 +00:00
|
|
|
|
2012-03-07 15:24:49 +00:00
|
|
|
if ($dh = OC_Filesystem::opendir($path.'/')) {
|
2011-09-25 20:32:08 +00:00
|
|
|
while (($filename = readdir($dh)) !== false) {
|
2012-01-07 23:14:34 +00:00
|
|
|
$filepath = ($path[strlen($path)-1]=='/'?$path:$path.'/').$filename;
|
2011-09-25 20:32:08 +00:00
|
|
|
if (substr($filename, 0, 1) == '.') continue;
|
2012-01-15 14:31:17 +00:00
|
|
|
if (self::isPhoto($path.'/'.$filename)) {
|
2011-09-25 20:32:08 +00:00
|
|
|
$current_album['images'][] = $filepath;
|
|
|
|
}
|
2011-10-18 19:43:33 +00:00
|
|
|
}
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
|
|
|
$current_album['imagesCount'] = count($current_album['images']);
|
2012-01-15 14:31:17 +00:00
|
|
|
$albums['imagesCount'] = $current_album['imagesCount'];
|
2012-03-03 23:35:37 +00:00
|
|
|
$albums['albumName'] = utf8_encode($current_album['name']);
|
2011-12-21 17:35:29 +00:00
|
|
|
|
2012-01-17 19:00:12 +00:00
|
|
|
$result = OC_Gallery_Album::find(OC_User::getUser(), /*$current_album['name']*/ null, $path);
|
|
|
|
// don't duplicate galleries with same path (bug oc-33)
|
2012-03-07 16:31:01 +00:00
|
|
|
if (!($albumId = $result->fetchRow()) && count($current_album['images'])) {
|
|
|
|
OC_Gallery_Album::create(OC_User::getUser(), $current_album['name'], $path);
|
2011-12-08 19:04:56 +00:00
|
|
|
$result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']);
|
2012-03-07 16:31:01 +00:00
|
|
|
$albumId = $result->fetchRow();
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
|
|
|
$albumId = $albumId['album_id'];
|
|
|
|
foreach ($current_album['images'] as $img) {
|
2011-12-08 19:04:56 +00:00
|
|
|
$result = OC_Gallery_Photo::find($albumId, $img);
|
2012-03-07 16:31:01 +00:00
|
|
|
if (!$result->fetchRow()) {
|
2011-12-08 19:04:56 +00:00
|
|
|
OC_Gallery_Photo::create($albumId, $img);
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-18 19:43:33 +00:00
|
|
|
if (count($current_album['images'])) {
|
|
|
|
self::createThumbnail($current_album['name'],$current_album['images']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createThumbnail($albumName, $files) {
|
|
|
|
$file_count = min(count($files), 10);
|
|
|
|
$thumbnail = imagecreatetruecolor($file_count*200, 200);
|
|
|
|
for ($i = 0; $i < $file_count; $i++) {
|
2012-02-08 20:09:47 +00:00
|
|
|
$image = OC_Gallery_Photo::getThumbnail($files[$i]);
|
|
|
|
if ($image && $image->valid()) {
|
|
|
|
imagecopyresampled($thumbnail, $image->resource(), $i*200, 0, 0, 0, 200, 200, 200, 200);
|
|
|
|
}
|
2011-10-18 19:43:33 +00:00
|
|
|
}
|
|
|
|
imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isPhoto($filename) {
|
2012-01-20 22:26:39 +00:00
|
|
|
$ext = strtolower(substr($filename, strrpos($filename, '.')+1));
|
|
|
|
return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif';
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
2012-01-15 14:31:17 +00:00
|
|
|
|
|
|
|
public static function find_paths($path) {
|
2012-03-09 15:28:26 +00:00
|
|
|
$images=OC_FileCache::searchByMime('image','', self::getScanningRoot());
|
2012-03-03 21:04:34 +00:00
|
|
|
$paths=array();
|
|
|
|
foreach($images as $image){
|
2012-03-09 15:28:26 +00:00
|
|
|
$path=substr(dirname($image), strlen(self::getScanningRoot()));;
|
2012-03-03 21:04:34 +00:00
|
|
|
if(array_search($path,$paths)===false){
|
|
|
|
$paths[]=$path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $paths;
|
2012-01-15 14:31:17 +00:00
|
|
|
}
|
2011-09-25 20:32:08 +00:00
|
|
|
}
|
2012-03-09 15:28:26 +00:00
|
|
|
|
2011-09-25 20:32:08 +00:00
|
|
|
?>
|