diff --git a/app/Console/Commands/import.php b/app/Console/Commands/import.php new file mode 100644 index 00000000000..43c28f980ee --- /dev/null +++ b/app/Console/Commands/import.php @@ -0,0 +1,176 @@ +photoFunctions = $photoFunctions; + $this->albumFunctions = $albumFunctions; + $this->sessionFunctions = $sessionFunctions; + } + + /** + * 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 + * + * @return void + */ + 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 = ''; + $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%s (id: %d)', + $line_indent, + $tile, + $own_album['title'], + $own_album['id'] + )); + } + + $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. + * + * @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 ImportController($this->photoFunctions, $this->albumFunctions, $this->sessionFunctions); + Session::put('UserID', $owner_id); + + if ($album_id == null) { + $album_title = null; + $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.'); + } + $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 + )); + } + + $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); + foreach ($iterator as $file) { + if ($file->isDir()) { + continue; + } + + $prefix = 'Processing file ' . $file; + try { + $ret = $import_controller->photo($file, $delete_imported, $album_id); + } catch (Exception $e) { + $this->error($prefix . ' ERROR'); + $this->error($e); + continue; + } + $this->info($prefix . ' OK'); + } + $this->info('Done importing.'); + } +}