From 07cf709eeb0be12376a1e471aa37bfd2cd8a9caf Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Sun, 15 Jan 2012 15:31:17 +0100 Subject: [PATCH] better scanning model for gallery, more feedback to user while scanning --- apps/gallery/ajax/galleryOp.php | 40 +++++++++++++++++++++++------- apps/gallery/js/album_cover.js | 43 +++++++++++++++++++++++++++------ apps/gallery/js/albums.js | 24 +++--------------- apps/gallery/lib/scanner.php | 25 ++++++++++++++++--- 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php index a62f0fe3f5..f162fe2bc4 100644 --- a/apps/gallery/ajax/galleryOp.php +++ b/apps/gallery/ajax/galleryOp.php @@ -37,39 +37,61 @@ function handleRemove($name) { OC_Gallery_Album::remove(OC_User::getUser(), $name); } -function handleGetThumbnails($albumname) -{ +function handleGetThumbnails($albumname) { OC_JSON::checkLoggedIn(); $photo = new OC_Image(); $photo->loadFromFile(OC::$CONFIG_DATADIRECTORY.'/../gallery/'.$albumname.'.png'); $photo->show(); } -function handleGalleryScanning() -{ +function handleGalleryScanning() { OC_JSON::checkLoggedIn(); OC_Gallery_Scanner::cleanup(); OC_JSON::success(array('albums' => OC_Gallery_Scanner::scan('/'))); } +function handleFilescan() { + OC_JSON::checkLoggedIn(); + $pathlist = OC_Gallery_Scanner::find_paths('/'); + sort($pathlist); + OC_JSON::success(array('paths' => $pathlist)); +} + +function handlePartialCreate($path) { + OC_JSON::checkLoggedIn(); + 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; + OC_Gallery_Scanner::scanDir($path, $albums); + OC_JSON::success(array('album_details' => $albums)); +} + if ($_GET['operation']) { switch($_GET['operation']) { - case "rename": + case 'rename': handleRename($_GET['oldname'], $_GET['newname']); OC_JSON::success(array('newname' => $_GET['newname'])); break; - case "remove": + case 'remove': handleRemove($_GET['name']); OC_JSON::success(); break; - case "get_covers": + case 'get_covers': handleGetThumbnails($_GET['albumname']); break; - case "scan": + case 'scan': handleGalleryScanning(); break; + case 'filescan': + handleFilescan(); + break; + case 'partial_create': + handlePartialCreate($_GET['path']); + break; default: - OC_JSON::error(array('cause' => "Unknown operation")); + OC_JSON::error(array('cause' => 'Unknown operation')); } } ?> diff --git a/apps/gallery/js/album_cover.js b/apps/gallery/js/album_cover.js index 0009eb04e4..e50593c008 100644 --- a/apps/gallery/js/album_cover.js +++ b/apps/gallery/js/album_cover.js @@ -4,10 +4,11 @@ $(document).ready(function() { if (r.status == 'success') { for (var i in r.albums) { var a = r.albums[i]; - Albums.add(a.name, a.numOfItems, a.bgPath); + Albums.add(a.name, a.numOfItems); } var targetDiv = document.getElementById('gallery_list'); if (targetDiv) { + $(targetDiv).html(''); Albums.display(targetDiv); } else { alert('Error occured: no such layer `gallery_list`'); @@ -30,14 +31,42 @@ function createNewAlbum() { } } +var albumCounter = 0; +var totalAlbums = 0; + function scanForAlbums() { + var albumCounter = 0; + var totalAlbums = 0; + $('#notification').text("Scanning directories"); $("#notification").fadeIn(); $("#notification").slideDown(); - $.getJSON('ajax/galleryOp.php?operation=scan', function(r) { - $("#notification").fadeOut(); - $("#notification").slideUp(); + $.getJSON('ajax/galleryOp.php?operation=filescan', function(r) { + if (r.status == 'success') { - window.location.reload(true); + totalAlbums = r.paths.length; + $('#notification').text("Creating thumbnails ... " + Math.floor((albumCounter/totalAlbums)*100) + "%"); + for(var a in r.paths) { + $.getJSON('ajax/galleryOp.php?operation=partial_create&path='+r.paths[a], function(r) { + + if (r.status == 'success') { + Albums.add(r.album_details.albumName, r.album_details.imagesCount); + } + + albumCounter++; + $('#notification').text("Creating thumbnails ... " + Math.floor((albumCounter/totalAlbums)*100) + "%"); + if (albumCounter == totalAlbums) { + $("#notification").fadeOut(); + $("#notification").slideUp(); + var targetDiv = document.getElementById('gallery_list'); + if (targetDiv) { + targetDiv.innerHTML = ''; + Albums.display(targetDiv); + } else { + alert('Error occured: no such layer `gallery_list`'); + } + } + }); + } } else { alert('Error occured: ' + r.message); } @@ -48,8 +77,8 @@ function galleryRemove(albumName) { if (confirm("Do you wan't to remove album " + albumName + "?")) { $.getJSON("ajax/galleryOp.php", {operation: "remove", name: albumName}, function(r) { if (r.status == "success") { - $("#gallery_album_box[title='"+albumName+"']").remove(); - Albums.remove(albumName); + $("#gallery_album_box[title='"+albumName+"']").remove(); + Albums.remove(albumName); } else { alert("Error: " + r.cause); } diff --git a/apps/gallery/js/albums.js b/apps/gallery/js/albums.js index 2fb1ac89ba..546aadba49 100644 --- a/apps/gallery/js/albums.js +++ b/apps/gallery/js/albums.js @@ -12,13 +12,9 @@ Albums={ // album with the same name wont be insered, // and false will be returned // true on success - add: function(album_name, num, bgPath) { - for (var a in Albums.albums) { - if (a.name == album_name) { - return false; - } - } - Albums.albums.push({name: album_name, numOfCovers: num, backgroundPath: bgPath}); + add: function(album_name, num) { + if (Albums.albums[album_name] != undefined) return false; + Albums.albums[album_name] = {name: album_name, numOfCovers: num}; return true; }, // remove element with given name @@ -40,19 +36,7 @@ Albums={ // return element which match given name // of undefined if such element do not exist find: function(name) { - var i = -1, tmp = 0; - for (var k in Albums.albums) { - var a = Albums.albums[k]; - if (a.name == name) { - i = tmp; - break; - } - tmp++; - } - if (i != -1) { - return Albums.albums[i]; - } - return undefined; + return Albums.albums[name]; }, // displays gallery in linear representation // on given element, and apply default styles for gallery diff --git a/apps/gallery/lib/scanner.php b/apps/gallery/lib/scanner.php index 3eba9260ac..4ce38a9953 100644 --- a/apps/gallery/lib/scanner.php +++ b/apps/gallery/lib/scanner.php @@ -50,15 +50,14 @@ class OC_Gallery_Scanner { while (($filename = readdir($dh)) !== false) { $filepath = ($path[strlen($path)-1]=='/'?$path:$path.'/').$filename; if (substr($filename, 0, 1) == '.') continue; - if (OC_Filesystem::is_dir($filepath)) { - self::scanDir($filepath, $albums); - } elseif (self::isPhoto($path.'/'.$filename)) { + if (self::isPhoto($path.'/'.$filename)) { $current_album['images'][] = $filepath; } } } $current_album['imagesCount'] = count($current_album['images']); - $albums[] = $current_album; + $albums['imagesCount'] = $current_album['imagesCount']; + $albums['albumName'] = $current_album['name']; $result = OC_Gallery_Album::find(OC_User::getUser(), $current_album['name']); if ($result->numRows() == 0 && count($current_album['images'])) { @@ -92,5 +91,23 @@ class OC_Gallery_Scanner { 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; + } } ?>