nextcloud/apps/gallery/lib/scanner.php

114 lines
4.0 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
* @copyright 2012 Bartek Przybylski bart.p.pl@gmail.com
*
* 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/>.
*
*/
2011-09-26 00:32:08 +04:00
require_once('base.php'); // base lib
2011-12-21 21:35:29 +04:00
require_once('images_utils.php');
2011-09-26 00:32:08 +04:00
2011-12-08 23:04:56 +04:00
class OC_Gallery_Scanner {
2011-09-26 00:32:08 +04:00
public static function scan($root) {
$albums = array();
self::scanDir($root, $albums);
return $albums;
}
public static function cleanUp() {
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_albums');
$stmt->execute(array());
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*gallery_photos');
$stmt->execute(array());
}
2011-09-26 00:32:08 +04:00
public static function scanDir($path, &$albums) {
$current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array());
$current_album['name'] = str_replace('/', '.', str_replace(OC::$CONFIG_DATADIRECTORY, '', $current_album['name']));
$current_album['name'] = ($current_album['name']==='.') ?
2011-12-30 20:06:29 +04:00
'main' :
trim($current_album['name'],'.');
2011-09-26 00:32:08 +04:00
if ($dh = OC_Filesystem::opendir($path)) {
while (($filename = readdir($dh)) !== false) {
$filepath = ($path[strlen($path)-1]=='/'?$path:$path.'/').$filename;
2011-09-26 00:32:08 +04:00
if (substr($filename, 0, 1) == '.') continue;
if (self::isPhoto($path.'/'.$filename)) {
2011-09-26 00:32:08 +04:00
$current_album['images'][] = $filepath;
}
}
2011-09-26 00:32:08 +04:00
}
$current_album['imagesCount'] = count($current_album['images']);
$albums['imagesCount'] = $current_album['imagesCount'];
$albums['albumName'] = $current_album['name'];
2011-12-21 21:35:29 +04:00
2011-12-08 23:04:56 +04:00
$result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']);
if ($result->numRows() == 0 && count($current_album['images'])) {
OC_Gallery_Album::create(OC_User::getUser(), $current_album['name'], $path);
2011-12-08 23:04:56 +04:00
$result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']);
2011-09-26 00:32:08 +04:00
}
$albumId = $result->fetchRow();
$albumId = $albumId['album_id'];
foreach ($current_album['images'] as $img) {
2011-12-08 23:04:56 +04:00
$result = OC_Gallery_Photo::find($albumId, $img);
2011-09-26 00:32:08 +04:00
if ($result->numRows() == 0) {
2011-12-08 23:04:56 +04:00
OC_Gallery_Photo::create($albumId, $img);
2011-09-26 00:32:08 +04: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++) {
CroppedThumbnail(OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/files/'.$files[$i], 200, 200, $thumbnail, $i*200);
}
imagepng($thumbnail, OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/' . $albumName.'.png');
2011-09-26 00:32:08 +04:00
}
public static function isPhoto($filename) {
if (substr(OC_Filesystem::getMimeType($filename), 0, 6) == "image/")
return 1;
return 0;
}
public static function find_paths($path) {
$ret = array();
$dirres;
$addpath = FALSE;
if (($dirres = OC_Filesystem::opendir($path)) == FALSE) return $ret;
while (($file = readdir($dirres)) != FALSE) {
if ($file[0] == '.') continue;
if (OC_Filesystem::is_dir($path.$file))
$ret = array_merge($ret, self::find_paths($path.$file.'/'));
if (self::isPhoto($path.$file)) $addpath = TRUE;
}
if ($addpath) $ret[] = $path;
return $ret;
}
2011-09-26 00:32:08 +04:00
}
?>