From f6075cc1fe516c3eb553b6b28380b133b7da8dda Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Sun, 11 Mar 2012 16:49:21 +0100 Subject: [PATCH] gallery migration to events system --- apps/gallery/ajax/galleryOp.php | 44 +++----- apps/gallery/img/loading.gif | Bin 0 -> 1849 bytes apps/gallery/js/album_cover.js | 112 +++----------------- apps/gallery/js/albums.js | 2 +- apps/gallery/js/scanner.js | 34 ++++++ apps/gallery/lib/album.php | 103 +++++++++--------- apps/gallery/lib/photo.php | 9 +- apps/gallery/lib/scanner.php | 174 ++++++++++++++++--------------- apps/gallery/templates/index.php | 59 ++++++----- lib/image.php | 2 +- 10 files changed, 245 insertions(+), 294 deletions(-) create mode 100644 apps/gallery/img/loading.gif create mode 100644 apps/gallery/js/scanner.js diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php index 64f1b1697b..1f280623ce 100644 --- a/apps/gallery/ajax/galleryOp.php +++ b/apps/gallery/ajax/galleryOp.php @@ -21,6 +21,7 @@ * */ +header('Content-type: text/html; charset=UTF-8') ; require_once('../../../lib/base.php'); OC_JSON::checkLoggedIn(); @@ -47,28 +48,21 @@ function handleGetThumbnails($albumname) { } function handleGalleryScanning() { - OC_Gallery_Scanner::cleanup(); - OC_JSON::success(array('albums' => OC_Gallery_Scanner::scan('/'))); + OC_DB::beginTransaction(); + set_time_limit(0); + $eventSource = new OC_EventSource(); + OC_Gallery_Scanner::scan($eventSource); + $eventSource->close(); + OC_DB::commit(); } function handleFilescan($cleanup) { if ($cleanup) OC_Gallery_Album::cleanup(); - $root = OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '').'/'; - $pathlist = OC_Gallery_Scanner::find_paths($root); + $pathlist = OC_Gallery_Scanner::find_paths(); sort($pathlist); OC_JSON::success(array('paths' => $pathlist)); } -function handlePartialCreate($path) { - if (empty($path)) OC_JSON::error(array('cause' => 'No path specified')); - if (!OC_Filesystem::is_dir($path.'/')) OC_JSON::error(array('cause' => 'Invalid path given')); - - $album = OC_Gallery_Album::find(OC_User::getUser(), null, $path); - $albums = array(); - OC_Gallery_Scanner::scanDir($path, $albums); - OC_JSON::success(array('album_details' => $albums)); -} - function handleStoreSettings($root, $order) { if (!OC_Filesystem::file_exists($root)) { OC_JSON::error(array('cause' => 'No such file or directory')); @@ -80,7 +74,8 @@ function handleStoreSettings($root, $order) { } $current_root = OC_Preferences::getValue(OC_User::getUser(),'gallery', 'root', '/'); - $root = trim(rtrim($root, '/')); + $root = trim($root); + $root = rtrim($root, '/').'/'; $rescan = $current_root==$root?'no':'yes'; OC_Preferences::setValue(OC_User::getUser(), 'gallery', 'root', $root); OC_Preferences::setValue(OC_User::getUser(), 'gallery', 'order', $order); @@ -90,11 +85,9 @@ function handleStoreSettings($root, $order) { function handleGetGallery($path) { $a = array(); $root = OC_Preferences::getValue(OC_User::getUser(),'gallery', 'root', '/'); - if (strlen($root) > 1) - $path = $root.'/'.trim($path, '/'); - else - $path = '/'.ltrim($path, '/'); - if (strlen($path) > 1) $path = rtrim($path, '/'); + $path = utf8_decode(rtrim($root.$path,'/')); + if($path == '') $path = '/'; + $pathLen = strlen($path); $result = OC_Gallery_Album::find(OC_User::getUser(), null, $path); $album_details = $result->fetchRow(); @@ -104,7 +97,7 @@ function handleGetGallery($path) { $album_name = $r['album_name']; $size=OC_Gallery_Album::getAlbumSize($r['album_id']); - $a[] = array('name' => utf8_encode($album_name), 'numOfItems' => min($size, 10),'path'=>$r['album_path']); + $a[] = array('name' => utf8_encode($album_name), 'numOfItems' => min($size, 10),'path'=>substr($r['album_path'], $pathLen)); } $result = OC_Gallery_Photo::find($album_details['album_id']); @@ -134,18 +127,9 @@ if ($_GET['operation']) { case 'scan': handleGalleryScanning(); break; - case 'filescan': - handleFilescan($_GET['cleanup']); - break; - case 'partial_create': - handlePartialCreate(urldecode($_GET['path'])); - break; case 'store_settings': handleStoreSettings($_GET['root'], $_GET['order']); break; - case 'get_galleries': - handleGetGalleries($_GET['path']); - break; case 'get_gallery': handleGetGallery($_GET['path']); break; diff --git a/apps/gallery/img/loading.gif b/apps/gallery/img/loading.gif new file mode 100644 index 0000000000000000000000000000000000000000..5b33f7e54f4e55b6b8774d86d96895db9af044b4 GIT binary patch literal 1849 zcma*odr(tX9tZI2z31lM+(&YVk%mZ}5P~KlG2s=WSbGzm0!x7^P##Mnh7t-jP!X0Q zk_SQ}Po-L1tlDK;6l?(>v)e5ZBQx4|Y-Q?nr@Px3?9h(3ZWr3^tj=`TP57gKr87N$ zp2wWee1GRRCwo_xahnw)5cxNPJbCg2L6DV|6`#+yw6v6!mDS$f9-JvFD^n;GQ&UrZ zzh5jCkByB101O60U0q#p_1BM>Cv-vP?&s4@g_((4_1L=L$(a91)0=J91Gas#R{McE znYG^9*0A5YZ>#;~+Wkn(W5B0^yELIYLP!K}mB~<)AM@1&nqekynuaEGqPrzoH|KodRXJy)%+w_fu3nE5>@Bd_b zqC$EQ;{c`T&?EsNO|igL9gC7Ygxv?aQUEXMq?~>wg{EyW;VcJ37CUF#HjrT=KQO_* zS>M9yydXk18D(+QDJ1>r);Lav_uYKp$T?4vr{Q$lTo&pKv^?(>L-)G2*lwH!Ah7k? z7oH<8h-(KTKt5V6$8gF)C7Io&P5=SjTh)=zV=E2EUhQZP##L8S{d%UK>>+y82>+FV+#^BzW7u3F)Bb>=lYQ%%j`F>ASe zo*cw@V#u6T`A2He;70mR(V&iV&-7{qP~=SRf&jm9-T{*ZeZ}$rd0#6c&fLG^xJcf5 z+p<`wJYgW+_s*V{uI$nMB;%8`S_3>PfGOj3Rq}@Cx^+j?rk92fANSFDBYnOqQ>Vdj z)(|$AhP4t&Lb=Gvo2#3Gl%9<=Gv`Mz?Po@P4iLF!x}GUWJICDlFk-hS^Whyh7x~VH z@0vD1>HYD4&e+~yzS*-sFR{9`{QEEZO1zg7>R&7cHts-6j!xHVdA8eI+ZlVzd%`es zJT@$#GX(gvCJ1oJN%yLBK}{V=V;seo;!w|Yte!W1%5qLNFWqvZW>h&IiH+oPT=b@E zPhGzv5=(Un*X>v`>%8h_nj^NdYcE6NHS_ifkCV$*D)Tqrbu`s;<=t<4 zAHNqNV?6(g<1PY-w@#I-WYFViz?9TrkMr)u0g`O`u|>T;k|2sV*YF^punvT;$SuTy{j3Gv)yqD!R_CF>yR)MzmmYS5v+~R zXAdD%ng9?df;wd8GxR#%3O+gz};Vo;)sK%Bj-q>Oq%R7JU-KD?vYu>#2UjaDo z&8$>5xW~?KPD_#XFToU1hIb*VOMidUr6iYiO0N|i-7s`T8!cFT`rN!^1Pt78J93i6 z5HI1wIM$94m{3SLDvISDe6$ZG1;eq_D9RTaaC>=cO{@Bs>$IlPCPJJ$h$)-3vzNUQ6OsN#_zWxey!_9%hxwH2_dEJi=yY|1c7nDm2_Lm!Cof8-R_+9UkS zcBE(o47yE)oMR(Q=dp1a2wTX5KvvGyLqlWTa7V&!A*|w|)ax~1_~aJ0=_Lilg*0iQk7#ZD EAHN$8j{pDw literal 0 HcmV?d00001 diff --git a/apps/gallery/js/album_cover.js b/apps/gallery/js/album_cover.js index 71de1adae1..8cafce35f9 100644 --- a/apps/gallery/js/album_cover.js +++ b/apps/gallery/js/album_cover.js @@ -10,17 +10,24 @@ function returnToElement(num) { crumbCount--; paths.pop(); } - path=paths[paths.length-1]; - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: 'get_gallery', path: path }, albumClickHandler); + var p=''; + for (var i in paths) p += paths[i]+'/'; + $('#g-album-loading').show(); + $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: 'get_gallery', path: p }, albumClickHandler); } -function albumClick(title,path) { - paths.push(path); +function albumClick(title) { + paths.push(title); crumbCount++; - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: 'get_gallery', path: path }, albumClickHandler); + var p = ''; + for (var i in paths) p += paths[i]+'/'; + $('#g-album-loading').show(); + $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: 'get_gallery', path: p }, function(r) { + albumClickHandler(r); if ($('#g-album-navigation :last-child')) $('#g-album-navigation :last-child').removeClass('last'); - $('#g-album-navigation').append(''); + $('#g-album-navigation').append(''); + }); } function albumClickHandler(r) { @@ -40,7 +47,7 @@ function albumClickHandler(r) { Albums.display(targetDiv); //$('#gallery_list').sortable({revert:true}); $('.album').each(function(i, el) { - $(el).click(albumClick.bind(null,$(el).attr('title'),$(el).data('path'))); + $(el).click(albumClick.bind(null,$(el).attr('title'))); //$(el).draggable({connectToSortable: '#gallery_list', handle: '.dummy'}); }); } else { @@ -49,100 +56,15 @@ function albumClickHandler(r) { } else { OC.dialogs.alert(t('gallery', 'Error: ') + r.message, t('gallery', 'Internal error')); } -} - -function createNewAlbum() { - var name = prompt("album name", ""); - if (name != null && name != "") { - $.getJSON(OC.filePath('gallery','ajax','createAlbum.php'), {album_name: name}, function(r) { - if (r.status == "success") { - var v = ''; - $('div#gallery_list').append(v); - } - }); - } + $('#g-album-loading').hide(); } var albumCounter = 0; var totalAlbums = 0; function scanForAlbums(cleanup) { - cleanup = cleanup?true:false; - var albumCounter = 0; - var totalAlbums = 0; - $('#g-scan-button').attr('disabled', 'true'); - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {cleanup: cleanup,operation:'filescan'}, function(r) { - - if (r.status == 'success') { - totalAlbums = r.paths.length; - if (totalAlbums == 0) { - $('#notification').text(t('gallery', "No photos found")).fadeIn().slideDown().delay(3000).fadeOut().slideUp(); - return; - } - $('#scanprogressbar').progressbar({ value: (albumCounter/totalAlbums)*100 }).fadeIn(); - for(var a in r.paths) { - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'),{operation:'partial_create','path':r.paths[a]}, function(r) { - - albumCounter++; - $('#scanprogressbar').progressbar({ value: (albumCounter/totalAlbums)*100 }); - if (albumCounter == totalAlbums) { - $('#scanprogressbar').fadeOut(); - var targetDiv = document.getElementById('gallery_list'); - if (targetDiv) { - targetDiv.innerHTML = ''; - Albums.photos = []; - Albums.albums = []; - returnToElement(0); - } else { - alert('Error occured: no such layer `gallery_list`'); - } - $('#g-scan-button').attr('disabled', null); - } - }); - } - } else { - alert('Error occured: ' + r.message); - } - }); -} - -function galleryRemove(albumName) { - OC.dialogs.confirm(t('gallery', 'Do you want to remove album ') + decodeURIComponent(escape(albumName)), - t('gallery', 'Remove confirmation'), - function(decision) { - if (decision) { - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: "remove", name: decodeURIComponent(escape(albumName))}, function(r) { - if (r.status == "success") { - $(".gallery_box").filterAttr('data-album',albumName).remove(); - Albums.remove(albumName); - } else { - OC.dialogs.alert(r.cause, "Error"); - } - }); - } - } - ); -} - -function galleryRename(name) { - OC.dialogs.prompt(t('gallery', 'New album name'), - t('gallery', 'Change name'), - name, - function(newname) { - if (newname == name || newname == '') return; - if (Albums.find(newname)) { - OC.dialogs.alert('Album ' + newname + ' exists', 'Alert'); - return; - } - $.getJSON(OC.filePath('gallery','ajax','galleryOp.php'), {operation: 'rename', oldname: name, newname: newname}, function(r) { - if (r.status == 'success') { - Albums.rename($(".gallery_box").filterAttr('data-album',name), newname); - } else { - OC.dialogs.alert('Error: ' + r.cause, 'Error'); - } - }); - } - ); + Scanner.scanAlbums(); + return; } function settings() { diff --git a/apps/gallery/js/albums.js b/apps/gallery/js/albums.js index afdfbd3cc8..d3326841cc 100644 --- a/apps/gallery/js/albums.js +++ b/apps/gallery/js/albums.js @@ -42,7 +42,7 @@ Albums={ // displays gallery in linear representation // on given element, and apply default styles for gallery display: function(element) { - var displayTemplate = ''; + var displayTemplate = ''; for (var i in Albums.albums) { var a = Albums.albums[i]; var local=$(displayTemplate); diff --git a/apps/gallery/js/scanner.js b/apps/gallery/js/scanner.js new file mode 100644 index 0000000000..804ee9d229 --- /dev/null +++ b/apps/gallery/js/scanner.js @@ -0,0 +1,34 @@ +Scanner={ + albumsFound:0, + eventSource:null, + albumsScanned:0, + scanAlbums:function(callback){ + $('#scanprogressbar').progressbar({value:0}); + $('#scanprogressbar').fadeIn(); + $('#scan input.start').hide(); + $('#scan input.stop').show(); + Scanner.albumsScanned=0; + Scanner.eventSource=new OC.EventSource(OC.linkTo('gallery', 'ajax/galleryOp.php'),{operation:'scan'}); + Scanner.eventSource.listen('count', function(total){Scanner.albumsFound=total;}); + Scanner.eventSource.listen('scanned', function(data) { + Scanner.albumsScanned++; + var progress=(Scanner.albumsScanned/Scanner.albumsFound)*100; + $('#scanprogressbar').progressbar('value',progress); + }); + Scanner.eventSource.listen('done', function(count){ + $('#scan input.start').show(); + $('#scan input.stop').hide(); + $('#scanprogressbar').fadeOut(); + returnToElement(0); + }); + if (callback) + callback(); + }, + stop:function() { + Scanner.eventSource.close(); + $('#scan input.start').show(); + $('#scan input.stop').hide(); + $('#scanprogressbar').fadeOut(); + } +} + diff --git a/apps/gallery/lib/album.php b/apps/gallery/lib/album.php index 6f492440ea..e8698590e6 100644 --- a/apps/gallery/lib/album.php +++ b/apps/gallery/lib/album.php @@ -13,11 +13,11 @@ * * 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 +* 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 . +* License along with this library. If not, see . * */ @@ -26,84 +26,83 @@ require_once('base.php'); class OC_Gallery_Album { public static function create($owner, $name, $path){ $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_albums (uid_owner, album_name, album_path, parent_path) VALUES (?, ?, ?, ?)'); - $stmt->execute(array($owner, $name, $path, self::getParentPath($path))); + $stmt->execute(array($owner, $name, $path, self::getParentPath($path))); } - public static function cleanup() { - $albums = self::find(OC_User::getUser()); - while ($r = $albums->fetchRow()) { - OC_Gallery_Photo::removeByAlbumId($r['album_id']); - self::remove(OC_User::getUser(), $r['album_name']); - } - } + public static function cleanup() { + $albums = self::find(OC_User::getUser()); + while ($r = $albums->fetchRow()) { + OC_Gallery_Photo::removeByAlbumId($r['album_id']); + self::remove(OC_User::getUser(), $r['album_name']); + } + } - public static function getParentPath($path) { - return dirname($path); - } + public static function getParentPath($path) { + return $path === '/' ? '' : dirname($path); + } - public static function remove($owner, $name=null, $path=null, $parent=null) { - $sql = 'DELETE FROM *PREFIX*gallery_albums WHERE uid_owner LIKE ?'; + public static function remove($owner, $name=null, $path=null, $parent=null) { + $sql = 'DELETE FROM *PREFIX*gallery_albums WHERE uid_owner LIKE ?'; $args = array($owner); if (!is_null($name)){ - $sql .= ' AND album_name LIKE ?'; + $sql .= ' AND album_name LIKE ?'; $args[] = $name; - } - if (!is_null($path)){ - $sql .= ' AND album_path LIKE ?'; - $args[] = $path; - } - if (!is_null($parent)){ - $sql .= ' AND parent_path LIKE ?'; - $args[] = $parent; - } + } + if (!is_null($path)){ + $sql .= ' AND album_path LIKE ?'; + $args[] = $path; + } + if (!is_null($parent)){ + $sql .= ' AND parent_path LIKE ?'; + $args[] = $parent; + } $stmt = OC_DB::prepare($sql); return $stmt->execute($args); } - public static function removeByName($owner, $name) { self::remove($ownmer, $name); } - public static function removeByPath($owner, $path) { self::remove($owner, null, $path); } - public static function removeByParentPath($owner, $parent) { self::remove($owner, null, null, $parent); } + public static function removeByName($owner, $name) { self::remove($ownmer, $name); } + public static function removeByPath($owner, $path) { self::remove($owner, null, $path); } + public static function removeByParentPath($owner, $parent) { self::remove($owner, null, null, $parent); } - public static function find($owner, $name=null, $path=null, $parent=null){ + public static function find($owner, $name=null, $path=null, $parent=null){ $sql = 'SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner = ?'; $args = array($owner); if (!is_null($name)){ $sql .= ' AND album_name = ?'; $args[] = $name; - } - if (!is_null($path)){ - $sql .= ' AND album_path = ?'; - $args[] = $path; - } - if (!is_null($parent)){ - $sql .= ' AND parent_path = ?'; - $args[] = $parent; - } - $order = OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'order', 'ASC'); - $sql .= ' ORDER BY album_name ' . $order; + } + if (!is_null($path)){ + $sql .= ' AND album_path = ?'; + $args[] = $path; + } + if (!is_null($parent)){ + $sql .= ' AND parent_path = ?'; + $args[] = $parent; + } + $order = OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'order', 'ASC'); + $sql .= ' ORDER BY album_name ' . $order; $stmt = OC_DB::prepare($sql); return $stmt->execute($args); } - public static function changePath($oldname, $newname, $owner) { - $stmt = OC_DB::prepare('UPDATE *PREFIX*gallery_albums SET album_path=? WHERE uid_owner=? AND album_path=?'); - $stmt->execute(array($newname, $owner, $oldname)); - } + public static function changePath($oldname, $newname, $owner) { + $stmt = OC_DB::prepare('UPDATE *PREFIX*gallery_albums SET album_path=? WHERE uid_owner=? AND album_path=?'); + $stmt->execute(array($newname, $owner, $oldname)); + } - public static function changeThumbnailPath($oldname, $newname) { - require_once('../../../lib/base.php'); - $thumbpath = OC::$CONFIG_DATADIRECTORY.'/../gallery/'; - rename($thumbpath.$oldname.'.png', $thumbpath.$newname.'.png'); - } + public static function changeThumbnailPath($oldname, $newname) { + require_once('../../../lib/base.php'); + $thumbpath = OC::$CONFIG_DATADIRECTORY.'/../gallery/'; + rename($thumbpath.$oldname.'.png', $thumbpath.$newname.'.png'); + } - public static function getAlbumSize($id){ + public static function getAlbumSize($id){ $sql = 'SELECT COUNT(*) as size FROM *PREFIX*gallery_photos WHERE album_id = ?'; $stmt = OC_DB::prepare($sql); $result=$stmt->execute(array($id))->fetchRow(); return $result['size']; - } - + } } ?> diff --git a/apps/gallery/lib/photo.php b/apps/gallery/lib/photo.php index 872ecc9488..463df2b5f4 100644 --- a/apps/gallery/lib/photo.php +++ b/apps/gallery/lib/photo.php @@ -69,11 +69,12 @@ class OC_Gallery_Photo { public static function getThumbnail($image_name) { $save_dir = OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/'; $save_dir .= dirname($image_name). '/'; - $thumb_file = $save_dir . $image_name; + $image_path = self::getGalleryRoot().$image_name; + $thumb_file = $save_dir . basename($image_name); if (file_exists($thumb_file)) { $image = new OC_Image($thumb_file); } else { - $imagePath = OC_Filesystem::getLocalFile($image_name); + $imagePath = OC_Filesystem::getLocalFile($image_path); if(!file_exists($imagePath)) { return null; } @@ -93,4 +94,8 @@ class OC_Gallery_Photo { } return null; } + + public static function getGalleryRoot() { + return OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', ''); + } } diff --git a/apps/gallery/lib/scanner.php b/apps/gallery/lib/scanner.php index 00ab7cf718..1d74b1fb50 100644 --- a/apps/gallery/lib/scanner.php +++ b/apps/gallery/lib/scanner.php @@ -13,100 +13,102 @@ * * 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 +* 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 . +* License along with this library. If not, see . * */ -require_once('../../../lib/base.php'); - class OC_Gallery_Scanner { - public static function getScanningRoot() { - return OC_Filesystem::getRoot().OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '/'); - } - - public static function scan($root) { - $albums = array(); - self::scanDir($root, $albums); - return $albums; - } - - public static function cleanUp() { - OC_Gallery_Album::cleanup(); - } - - public static function createName($name) { - return basename($name); - } - - public static function scanDir($path, &$albums) { - $current_album = array('name'=> $path, 'imagesCount' => 0, 'images' => array()); - $current_album['name'] = self::createName($current_album['name']); - - if ($dh = OC_Filesystem::opendir($path.'/')) { - while (($filename = readdir($dh)) !== false) { - $filepath = ($path[strlen($path)-1]=='/'?$path:$path.'/').$filename; - if (substr($filename, 0, 1) == '.') continue; - if (self::isPhoto($path.'/'.$filename)) { - $current_album['images'][] = $filepath; - } - } - } - $current_album['imagesCount'] = count($current_album['images']); - $albums['imagesCount'] = $current_album['imagesCount']; - $albums['albumName'] = utf8_encode($current_album['name']); - - $result = OC_Gallery_Album::find(OC_User::getUser(), /*$current_album['name']*/ null, $path); - // don't duplicate galleries with same path (bug oc-33) - if (!($albumId = $result->fetchRow()) && count($current_album['images'])) { - OC_Gallery_Album::create(OC_User::getUser(), $current_album['name'], $path); - $result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']); - $albumId = $result->fetchRow(); - } - $albumId = $albumId['album_id']; - foreach ($current_album['images'] as $img) { - $result = OC_Gallery_Photo::find($albumId, $img); - if (!$result->fetchRow()) { - OC_Gallery_Photo::create($albumId, $img); - } - } - 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++) { - $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'); - } - - public static function isPhoto($filename) { - $ext = strtolower(substr($filename, strrpos($filename, '.')+1)); - return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif'; - } - - public static function find_paths($path) { - $images=OC_FileCache::searchByMime('image','', self::getScanningRoot()); - $paths=array(); - foreach($images as $image){ - $path=substr(dirname($image), strlen(self::getScanningRoot()));; - if(array_search($path,$paths)===false){ - $paths[]=$path; - } + public static function getGalleryRoot() { + return OC_Preferences::getValue(OC_User::getUser(), 'gallery', 'root', '/'); + } + public static function getScanningRoot() { + return OC_Filesystem::getRoot().self::getGalleryRoot(); + } + + public static function cleanUp() { + OC_Gallery_Album::cleanup(); + } + + public static function createName($name) { + $name = basename($name); + return $name == '.' ? '' : $name; + } + + // 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); + + $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); + } + + 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'); + } + + public static function isPhoto($filename) { + $ext = strtolower(substr($filename, strrpos($filename, '.')+1)); + return $ext=='png' || $ext=='jpeg' || $ext=='jpg' || $ext=='gif'; + } + + 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; + } + + 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; + } + } + sort($paths); + return $paths; } - return $paths; - } } ?> diff --git a/apps/gallery/templates/index.php b/apps/gallery/templates/index.php index 7f0281e6a3..54fbcfd015 100644 --- a/apps/gallery/templates/index.php +++ b/apps/gallery/templates/index.php @@ -1,6 +1,7 @@
-
-
- - -
-
-
- main -
-
+
+
+ + + +
+
+
+ main +
+
+