Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create categories only by the top level subfolder #686

Closed
Enrique-Venegas-A opened this issue Aug 23, 2022 · 3 comments
Closed

Create categories only by the top level subfolder #686

Enrique-Venegas-A opened this issue Aug 23, 2022 · 3 comments

Comments

@Enrique-Venegas-A
Copy link

Please describe your suggestion, and the problem it'd solve.
When using the "Subfolders to Categories" script, an option to create them only by the top folders would be great to separate content by origin.

image

With the current behavior a category would be created for "F1, F2 .. F9", my proposition would be to be able to choose between that or to create categories only for the top level "D11, D12, D13 and D14".

In my own gallery I currently have a folder for site1 and another for site2, in one of those, the files are stored in separated folders by authors, in the other one, all of them are just there. This produces an inconsistent set of tags that quickly becomes unusable and unmanageable when you have hundreds/thousands of files/folders.

@Difegue
Copy link
Owner

Difegue commented Sep 6, 2022

The current FolderToCat script uses find to drill down into the directory tree, adding files as it finds them using their parent folder.

Implementing this issue would probably require a variant of the algorithm, using finddepth instead to drill all the way down first, then go back up adding all the files under their most ancient subfolder.
Not particularly difficult, but slightly tricky!

@Ftbom
Copy link

Ftbom commented Jan 16, 2023

I made some changes to FolderToCat.pm and it works well for me.

...
        parameters =>
-         [ { type => "bool", desc => "Delete all your static categories before creating the ones matching your subfolders" } ]
+         [ { type => "bool", desc => "Delete all your static categories before creating the ones matching your subfolders" },
+           { type => "bool", desc => "Create categories by the top level subfolder" }]
...
    shift;
    my $lrr_info          = shift;
-   my ($delete_old_cats) = @_;
+   my ($delete_old_cats, $by_top_folder) = @_;
    my $logger            = get_logger( "Folder2Category", "plugins" );
    my $userdir           = LANraragi::Model::Config->get_userdir;

    my %subfolders;
    my @created_categories;
+   my $dirname;
...
    find(
        {   wanted => sub {
                return if $File::Find::dir eq $userdir;    # Direct children of the content dir are excluded
-
-               my $dirname = basename($File::Find::dir);
+               if (not($by_top_folder)) {
+               	$dirname = basename($File::Find::dir);
+               }
                if ( is_archive($_) ) {
                    unless ( exists( $subfolders{$dirname} ) ) {
                        $subfolders{$dirname} = [];        # Create array in hash for this folder
                    }
                    push @{ $subfolders{$dirname} }, $_;
                }
+               elsif ($by_top_folder) {
+               	$dirname = basename($File::Find::dir);
+               }	
            },
            no_chdir    => 1,
            follow_fast => 1
        },
        $userdir
    );

You can download the file from here.

@Difegue
Copy link
Owner

Difegue commented Jan 31, 2023

That doesn't seem to work in all cases -- I believe this code would end up creating one single category named after the deepest subfolder.

It did give me a simple idea for doing this without finddepth though, so thanks!

@Difegue Difegue closed this as completed Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants