From a4a2a562f19e9c1d9dc30b72d39c82c5b5f8ac09 Mon Sep 17 00:00:00 2001 From: Andre Merboldt Date: Sun, 21 Apr 2019 14:09:18 +0200 Subject: [PATCH 1/6] support import of single photos --- app/Console/Commands/import.php | 140 ++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 app/Console/Commands/import.php diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php new file mode 100644 index 0000000000..d1b8d59f67 --- /dev/null +++ b/app/Console/Commands/import.php @@ -0,0 +1,140 @@ +photoFunctions = $photoFunctions; + $this->albumFunctions = $albumFunctions; + } + + /** + * Prints the album tree in a DFS fashion + * + * @param albums $photoFunctions + * @return void + */ + public function print_album(\Illuminate\Database\Eloquent\Collection $albums, int $parent_id, int $indent=0) + { + $child_albums = $albums->where('parent_id', '=', $parent_id); + $line_indent = ''; + for ($i = 0; $i < $indent; $i++) { + $line_indent .= ' '; // TODO tree-style + } + if ($parent_id != 0) { + $own_album = $albums->where('id', '=', $parent_id)->first(); + $this->line(sprintf("%s- %s (id: %d)", + $line_indent, + $own_album['title'], + $own_album['id'], + )); + } + foreach ($child_albums as $album) { + $this->print_album($albums, $album->id, $indent + 1); + } + } + + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $directory = $this->argument('dir'); + $flatten = $this->option('flatten'); + $album_id = $this->option('album_id'); + $owner_id = 0; // $this->option('owner_id'); + $import_controller = new \App\Http\Controllers\ImportController($this->photoFunctions, $this->albumFunctions); + Session::put('UserID', $owner_id); +; + + if ($album_id == null) { + $album_title = null; + $headers = ['Id', 'Title']; + $albums = \App\Album::all(['id', 'title', 'parent_id']); + $this->print_album($albums, 0 /* parent */, 0 /* indent */); + do { + if ($album_title != null) { + $this->line('No album ' . $album_title . ' found. Try again.'); + } + $album_title = $this->ask('album to insert into (title)?'); + $sel_albums = $albums->where('title', '=', $album_title); + } while (count($sel_albums) == 0); + assert(count($sel_albums), 1); + $album_id = $sel_albums->first()['id']; + $this->line( + sprintf( + 'Selected album "%s" (id: %d)', + $album_title, + $album_id + )); + } + + + $dir_iterator = new \RecursiveDirectoryIterator("/home/spotlight/photos", \FilesystemIterator::SKIP_DOTS); + $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); + foreach ($iterator as $file) { + if ($file->isDir()) { + continue; + } + + $prefix = "Processing file ".$file; + try { + $ret = $import_controller->photo($file, $album_id); + } catch (\Exception $e) { + $this->error($prefix." ERROR"); + $this->error($e); + continue; + } + $this->info($prefix." OK"); + } + $this->info("Done importing."); + } +} From 51fbaa3686b9e499487f5c211fd58286cdf14fcb Mon Sep 17 00:00:00 2001 From: Andre Merboldt Date: Sun, 21 Apr 2019 14:24:34 +0200 Subject: [PATCH 2/6] support album id option correctly --- app/Console/Commands/import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php index d1b8d59f67..bc309d212d 100644 --- a/app/Console/Commands/import.php +++ b/app/Console/Commands/import.php @@ -18,7 +18,7 @@ class import extends Command * * @var string */ - protected $signature = 'lychee:import {dir : directory to import} {--flatten} {--album_id : Album ID to import to} + protected $signature = 'lychee:import {dir : directory to import} {--flatten} {--album_id= : Album ID to import to} {--owner_id=0 : User}'; /** From 28f58ac4cb018416f2834ee81b77fd7e509ff083 Mon Sep 17 00:00:00 2001 From: Andre Merboldt Date: Sun, 21 Apr 2019 14:26:10 +0200 Subject: [PATCH 3/6] use option instead of hardcoded --- app/Console/Commands/import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php index bc309d212d..146ae39c0e 100644 --- a/app/Console/Commands/import.php +++ b/app/Console/Commands/import.php @@ -118,7 +118,7 @@ public function handle() } - $dir_iterator = new \RecursiveDirectoryIterator("/home/spotlight/photos", \FilesystemIterator::SKIP_DOTS); + $dir_iterator = new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS); $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $file) { if ($file->isDir()) { From 0196d50ffbcf2217c3d0f421ba83de700de44419 Mon Sep 17 00:00:00 2001 From: ildyria Date: Mon, 6 May 2019 17:09:57 +0200 Subject: [PATCH 4/6] tree display --- app/Console/Commands/import.php | 79 +++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php index 146ae39c0e..e39d7b71ac 100644 --- a/app/Console/Commands/import.php +++ b/app/Console/Commands/import.php @@ -2,14 +2,17 @@ namespace App\Console\Commands; -use App\Configs; +use App\Album; use App\Http\Controllers\ImportController; use App\ModelFunctions\AlbumFunctions; -use App\ModelFunctions\Helpers; use App\ModelFunctions\PhotoFunctions; -use App\Photo; +use Exception; +use FilesystemIterator; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Session; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; class import extends Command { @@ -18,15 +21,16 @@ class import extends Command * * @var string */ - protected $signature = 'lychee:import {dir : directory to import} {--flatten} {--album_id= : Album ID to import to} - {--owner_id=0 : User}'; + protected $signature = 'lychee:import {dir : directory to import} {--flatten} {--album_id= : Album ID to import to}'; +// {--owner_id=0 : User}'; +// We import as admin as we are in CLI /** * The console command description. * * @var string */ - protected $description = 'Import a flattened directory into a directory'; + protected $description = 'Import a flattened directory into an album'; /** * @var AlbumFunctions @@ -38,6 +42,8 @@ class import extends Command */ private $photoFunctions; + + /** * Create a new command instance. * @@ -53,33 +59,58 @@ public function __construct(PhotoFunctions $photoFunctions, AlbumFunctions $albu $this->albumFunctions = $albumFunctions; } + + /** * Prints the album tree in a DFS fashion * - * @param albums $photoFunctions + * @param Collection $albums + * @param int $parent_id + * @param int $indent + * @param int $map + * @param bool $is_last * @return void */ - public function print_album(\Illuminate\Database\Eloquent\Collection $albums, int $parent_id, int $indent=0) + public function print_album(Collection $albums, int $parent_id, int $indent = 0, int $map = 0, bool $is_last = false) { $child_albums = $albums->where('parent_id', '=', $parent_id); $line_indent = ''; - for ($i = 0; $i < $indent; $i++) { - $line_indent .= ' '; // TODO tree-style + $i = $indent - 1; + while ($i > 0) { + $i--; + if ((($map >> $i) & 1) == 1) { + $line_indent = '| ' . $line_indent; + } + else { + $line_indent = ' ' . $line_indent; + } } + + $tile = $is_last ? '└ ' : '├ '; + if ($parent_id != 0) { $own_album = $albums->where('id', '=', $parent_id)->first(); - $this->line(sprintf("%s- %s (id: %d)", + $this->line(sprintf("%s%s%s (id: %d)", $line_indent, + $tile, $own_album['title'], - $own_album['id'], + $own_album['id'] )); } - foreach ($child_albums as $album) { - $this->print_album($albums, $album->id, $indent + 1); + + $map_next = ($map << 1) | !$is_last; + while ($child_albums->count()) { + $child_album = $child_albums->pop(); + $this->print_album($albums, $child_album->id, $indent + 1, $map_next, $child_albums->count() == 0); + } + if ($is_last) + { + $this->line(sprintf("%s",$line_indent)); } } + /** * Execute the console command. * @@ -91,18 +122,20 @@ public function handle() $flatten = $this->option('flatten'); $album_id = $this->option('album_id'); $owner_id = 0; // $this->option('owner_id'); - $import_controller = new \App\Http\Controllers\ImportController($this->photoFunctions, $this->albumFunctions); - Session::put('UserID', $owner_id); -; + $import_controller = new ImportController($this->photoFunctions, $this->albumFunctions); + Session::put('UserID', $owner_id);; if ($album_id == null) { $album_title = null; - $headers = ['Id', 'Title']; - $albums = \App\Album::all(['id', 'title', 'parent_id']); + $albums = Album::all([ + 'id', + 'title', + 'parent_id' + ]); $this->print_album($albums, 0 /* parent */, 0 /* indent */); do { if ($album_title != null) { - $this->line('No album ' . $album_title . ' found. Try again.'); + $this->line('No album '.$album_title.' found. Try again.'); } $album_title = $this->ask('album to insert into (title)?'); $sel_albums = $albums->where('title', '=', $album_title); @@ -118,8 +151,8 @@ public function handle() } - $dir_iterator = new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS); - $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); + $dir_iterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); + $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $file) { if ($file->isDir()) { continue; @@ -128,7 +161,7 @@ public function handle() $prefix = "Processing file ".$file; try { $ret = $import_controller->photo($file, $album_id); - } catch (\Exception $e) { + } catch (Exception $e) { $this->error($prefix." ERROR"); $this->error($e); continue; From a2360aef4f1116504246054abdda8da3d26c1c1a Mon Sep 17 00:00:00 2001 From: ildyria Date: Fri, 27 Dec 2019 18:13:10 +0100 Subject: [PATCH 5/6] fix import --- app/Console/Commands/import.php | 47 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php index e39d7b71ac..b51fa056c3 100644 --- a/app/Console/Commands/import.php +++ b/app/Console/Commands/import.php @@ -23,7 +23,7 @@ class import extends Command */ protected $signature = 'lychee:import {dir : directory to import} {--flatten} {--album_id= : Album ID to import to}'; // {--owner_id=0 : User}'; -// We import as admin as we are in CLI + // We import as admin as we are in CLI /** * The console command description. @@ -42,13 +42,12 @@ class import extends Command */ private $photoFunctions; - - /** * Create a new command instance. * * @param PhotoFunctions $photoFunctions * @param AlbumFunctions $albumFunctions + * * @return void */ public function __construct(PhotoFunctions $photoFunctions, AlbumFunctions $albumFunctions) @@ -59,16 +58,15 @@ public function __construct(PhotoFunctions $photoFunctions, AlbumFunctions $albu $this->albumFunctions = $albumFunctions; } - - /** - * Prints the album tree in a DFS fashion + * Prints the album tree in a DFS fashion. * * @param Collection $albums - * @param int $parent_id - * @param int $indent - * @param int $map - * @param bool $is_last + * @param int $parent_id + * @param int $indent + * @param int $map + * @param bool $is_last + * * @return void */ public function print_album(Collection $albums, int $parent_id, int $indent = 0, int $map = 0, bool $is_last = false) @@ -80,8 +78,7 @@ public function print_album(Collection $albums, int $parent_id, int $indent = 0, $i--; if ((($map >> $i) & 1) == 1) { $line_indent = '| ' . $line_indent; - } - else { + } else { $line_indent = ' ' . $line_indent; } } @@ -90,7 +87,7 @@ public function print_album(Collection $albums, int $parent_id, int $indent = 0, if ($parent_id != 0) { $own_album = $albums->where('id', '=', $parent_id)->first(); - $this->line(sprintf("%s%s%s (id: %d)", + $this->line(sprintf('%s%s%s (id: %d)', $line_indent, $tile, $own_album['title'], @@ -103,14 +100,11 @@ public function print_album(Collection $albums, int $parent_id, int $indent = 0, $child_album = $child_albums->pop(); $this->print_album($albums, $child_album->id, $indent + 1, $map_next, $child_albums->count() == 0); } - if ($is_last) - { - $this->line(sprintf("%s",$line_indent)); + if ($is_last) { + $this->line(sprintf('%s', $line_indent)); } } - - /** * Execute the console command. * @@ -123,19 +117,19 @@ public function handle() $album_id = $this->option('album_id'); $owner_id = 0; // $this->option('owner_id'); $import_controller = new ImportController($this->photoFunctions, $this->albumFunctions); - Session::put('UserID', $owner_id);; + Session::put('UserID', $owner_id); if ($album_id == null) { $album_title = null; $albums = Album::all([ 'id', 'title', - 'parent_id' + 'parent_id', ]); $this->print_album($albums, 0 /* parent */, 0 /* indent */); do { if ($album_title != null) { - $this->line('No album '.$album_title.' found. Try again.'); + $this->line('No album ' . $album_title . ' found. Try again.'); } $album_title = $this->ask('album to insert into (title)?'); $sel_albums = $albums->where('title', '=', $album_title); @@ -150,6 +144,7 @@ public function handle() )); } + $delete_imported = Configs::get_value('delete_imported', '0') === '1'; $dir_iterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); @@ -158,16 +153,16 @@ public function handle() continue; } - $prefix = "Processing file ".$file; + $prefix = 'Processing file ' . $file; try { - $ret = $import_controller->photo($file, $album_id); + $ret = $import_controller->photo($file, $delete_imported, $album_id); } catch (Exception $e) { - $this->error($prefix." ERROR"); + $this->error($prefix . ' ERROR'); $this->error($e); continue; } - $this->info($prefix." OK"); + $this->info($prefix . ' OK'); } - $this->info("Done importing."); + $this->info('Done importing.'); } } From a83a3ede76ea5ee9fd309e42a94f6deca91e1942 Mon Sep 17 00:00:00 2001 From: ildyria Date: Fri, 27 Dec 2019 19:00:54 +0100 Subject: [PATCH 6/6] fix more errors --- app/Console/Commands/import.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php index b51fa056c3..43c28f980e 100644 --- a/app/Console/Commands/import.php +++ b/app/Console/Commands/import.php @@ -6,6 +6,7 @@ use App\Http\Controllers\ImportController; use App\ModelFunctions\AlbumFunctions; use App\ModelFunctions\PhotoFunctions; +use App\ModelFunctions\SessionFunctions; use Exception; use FilesystemIterator; use Illuminate\Console\Command; @@ -42,20 +43,27 @@ class import extends Command */ private $photoFunctions; + /** + * @var SessionFunctions + */ + private $sessionFunctions; + /** * Create a new command instance. * - * @param PhotoFunctions $photoFunctions - * @param AlbumFunctions $albumFunctions + * @param PhotoFunctions $photoFunctions + * @param AlbumFunctions $albumFunctions + * @param SessionFunctions $sessionFunctions * * @return void */ - public function __construct(PhotoFunctions $photoFunctions, AlbumFunctions $albumFunctions) + public function __construct(PhotoFunctions $photoFunctions, AlbumFunctions $albumFunctions, SessionFunctions $sessionFunctions) { parent::__construct(); $this->photoFunctions = $photoFunctions; $this->albumFunctions = $albumFunctions; + $this->sessionFunctions = $sessionFunctions; } /** @@ -116,7 +124,7 @@ public function handle() $flatten = $this->option('flatten'); $album_id = $this->option('album_id'); $owner_id = 0; // $this->option('owner_id'); - $import_controller = new ImportController($this->photoFunctions, $this->albumFunctions); + $import_controller = new ImportController($this->photoFunctions, $this->albumFunctions, $this->sessionFunctions); Session::put('UserID', $owner_id); if ($album_id == null) {