App-DownloadsDirUtils

 view release on metacpan or  search on metacpan

lib/App/DownloadsDirUtils.pm  view on Meta::CPAN

                my $arg_spec = shift;
                $arg_spec->{default} = scalar list_downloads_dirs();
            },
            exclude_filename_pattern => sub {
                my $arg_spec = shift;
                $arg_spec->{default} = '/\.part\z/';
            },
        },
        output_code => sub {
            no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
            my %args = @_;
            $args{dirs} //= scalar list_downloads_dirs();
            $args{exclude_filename_pattern} //= qr/\.part\z/;
            &{"File::Util::Sort::$which"}(%args);
        },
    );
    die "Can't generate ${which}_download(): $res->[0] - $res->[1]"
        unless $res->[0] == 200;

    $res = gen_modified_sub(
        summary => "Move the $which file(s) from the downloads directories to current directory",
        description => <<"MARKDOWN",

This is a thin wrapper for the <prog:${which}-download> utility; the wrapper
moves the files to current directory. It hopes to be a convenient helper to
organize your downloads.

MARKDOWN
        output_name => "mv_${which}_download_here",
        base_name   => "${which}_download",
        add_args => {
            to_dir => {
                schema => 'dirname*',
                default => '.',
            },
            overwrite => {
                schema => 'true*',
                cmdline_aliases => {O=>{}},
            },
            as => {
                summary => 'Rename file',
                schema => 'pathname::unix::basename*',
            },
        },
        modify_meta => sub {
            my $meta = shift;
            $meta->{features} //= {};
            $meta->{features}{dry_run} = 1;
        },
        output_code => sub {
            no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
            require File::Basename;
            require File::Copy::Recursive;

            my %args = @_;

            my $to_dir = delete($args{to_dir}) // '.';

            my $res = &{"${which}_download"}(%args);
            return $res unless $res->[0] == 200;
            return [404, "No $which file(s) returned"] unless @{ $res->[2] };

            my $envres = envresmulti();
            my $i = 0;
            for my $file (@{ $res->[2] }) {
                $i++;
                my $targetpath = $to_dir . '/' . ($args{as} // File::Basename::basename($file));
                if (-e $targetpath && !$args{overwrite}) {
                    $envres->add_result(409, "File already exists '$targetpath', please specify -O to overwrite", {item_id=>$file});
                } elsif ($args{-dry_run}) {
                    log_info "DRY-RUN: [%d/%d] Moving %s to %s ...", $i, scalar(@{ $res->[2] }), $file, $targetpath;
                    $envres->add_result(200, "OK (dry-run)", {item_id=>$file});
                } else {
                    log_info "[%d/%d] Moving %s to %s ...", $i, scalar(@{ $res->[2] }), $file, $targetpath;
                    my $ok = File::Copy::Recursive::rmove($file, $targetpath);
                    if ($ok) {
                        $envres->add_result(200, "OK", {item_id=>$file});
                    } else {
                        $envres->add_result(500, "Error: $!", {item_id=>$file});
                    }
                }
            }
            $envres->as_struct;
        },
    );
    die "Can't generate mv_${which}_download_here(): $res->[0] - $res->[1]"
        unless $res->[0] == 200;
} # $which

1;
# ABSTRACT: Utilities related to downloads directories

__END__

=pod

=encoding UTF-8

=head1 NAME

App::DownloadsDirUtils - Utilities related to downloads directories

=head1 VERSION

This document describes version 0.008 of App::DownloadsDirUtils (from Perl distribution App-DownloadsDirUtils), released on 2025-05-03.

=head1 DESCRIPTION

This distribution provides the following command-line utilities:

=over

=item 1. L<foremost-download>

=item 2. L<hindmost-download>

=item 3. L<largest-download>

=item 4. L<list-downloads-dirs>

=item 5. L<mv-foremost-download-here>



( run in 1.536 second using v1.01-cache-2.11-cpan-39bf76dae61 )