View | Details | Raw Unified | Return to bug 41020
Collapse All | Expand All

(-)a/Koha/File/Transport.pm (+76 lines)
Lines 548-553 sub download_file { Link Here
548
    return $self->_download_file( $remote_file, $local_file );
548
    return $self->_download_file( $remote_file, $local_file );
549
}
549
}
550
550
551
=head3 delete_file
552
553
    my $success = $transport->delete_file($remote_file, \%options);
554
555
Deletes a file from the remote server. Automatically establishes a connection if needed.
556
557
B<Parameters:>
558
559
=over 4
560
561
=item * C<$remote_file> - Remote filename (not a path, just the filename) (required)
562
563
=item * C<\%options> - Optional hashref with keys:
564
565
=over 4
566
567
=item * C<path> - Directory path containing the file. If provided, uses this directory
568
for this operation only (simplified API). If omitted, behavior depends on whether
569
change_directory() has been called explicitly (see DESCRIPTION).
570
571
=back
572
573
=back
574
575
B<Usage Patterns:>
576
577
    # Pattern 1: Simplified API with custom path
578
    $transport->delete_file('old.txt', { path => '/temp/' });
579
580
    # Pattern 2: Simplified API with configured download_directory
581
    $transport->delete_file('old.txt');
582
583
    # Pattern 3: Traditional API with explicit directory
584
    $transport->change_directory('/temp/');
585
    $transport->delete_file('old.txt');
586
587
B<Returns:> True on success, undef on failure. Check object_messages() for details.
588
589
=cut
590
591
sub delete_file {
592
    my ( $self, $remote_file, $options ) = @_;
593
594
    return unless $self->_ensure_connected();
595
596
    # Only auto-change directory if:
597
    # 1. Options provided with custom path (simplified API), OR
598
    # 2. No explicit directory set by user AND default download_directory exists (traditional API)
599
    if ( $options && $options->{path} ) {
600
601
        # Simplified API - use custom path
602
        return unless $self->_auto_change_directory( 'download', $options->{path} );
603
    } elsif ( !$self->{_user_set_directory} ) {
604
605
        # Traditional API - use default directory only if user hasn't set one
606
        return unless $self->_auto_change_directory( 'download', undef );
607
    }
608
609
    return $self->_delete_file($remote_file);
610
}
611
612
=head3 _delete_file
613
614
    $transport->_delete_file($remote_file);
615
616
Internal method that performs the protocol-specific file deletion operation.
617
Must be implemented by subclasses. Called by delete_file after connection
618
and directory management.
619
620
=cut
621
622
sub _delete_file {
623
    my ($self) = @_;
624
    die "Subclass must implement _delete_file";
625
}
626
551
=head3 _download_file
627
=head3 _download_file
552
628
553
    $transport->_download_file($remote_file, $local_file);
629
    $transport->_download_file($remote_file, $local_file);
(-)a/Koha/File/Transport/FTP.pm (+30 lines)
Lines 122-129 sub _download_file { Link Here
122
    return 1;
122
    return 1;
123
}
123
}
124
124
125
=head3 _delete_file
126
127
    my $success = $server->_delete_file($filename);
128
129
Passed a filename, this will delete the file from the current directory of the server connection.
130
131
Returns true on success or undefined on failure.
132
133
=cut
134
135
sub _delete_file {
136
    my ( $self, $remote_file ) = @_;
137
    my $operation = "delete";
138
139
    $self->{connection}->delete($remote_file) or return $self->_abort_operation( $operation, $remote_file );
140
141
    $self->add_message(
142
        {
143
            message => $operation,
144
            type    => 'success',
145
            payload => {
146
                remote_file => $remote_file,
147
            }
148
        }
149
    );
150
151
    return 1;
152
}
153
125
=head3 _change_directory
154
=head3 _change_directory
126
155
156
127
    my $success = $server->_change_directory($directory);
157
    my $success = $server->_change_directory($directory);
128
158
129
Passed a directory name, this will change the current directory of the server connection.
159
Passed a directory name, this will change the current directory of the server connection.
(-)a/Koha/File/Transport/Local.pm (+58 lines)
Lines 390-395 sub _rename_file { Link Here
390
    return 1;
390
    return 1;
391
}
391
}
392
392
393
=head3 _delete_file
394
395
    $server->_delete_file();
396
397
Deletes a file in the current directory.
398
399
Returns true on success or undefined on failure.
400
401
=cut
402
403
sub _delete_file {
404
    my ( $self, $remote_file ) = @_;
405
    my $operation = "delete";
406
407
    my $directory = $self->{current_directory} || $self->download_directory || '.';
408
    my $source    = File::Spec->catfile( $directory, $remote_file );
409
410
    # Check if the file exists
411
    unless ( -e $source ) {
412
        $self->add_message(
413
            {
414
                message => $operation,
415
                type    => 'error',
416
                payload => {
417
                    error => "File not found: $source",
418
                    path  => $source
419
                }
420
            }
421
        );
422
        return;
423
    }
424
425
    # Attempt to delete the file
426
    unless ( unlink $source ) {
427
        $self->add_message(
428
            {
429
                message => $operation,
430
                type    => 'error',
431
                payload => {
432
                    error => "File could not be deleted: $!",
433
                    path  => $source
434
                }
435
            }
436
        );
437
        return;    # Return undef on failure
438
    }
439
440
    $self->add_message(
441
        {
442
            message => $operation,
443
            type    => 'success',
444
            payload => { path => $source }
445
        }
446
    );
447
448
    return 1;
449
}
450
393
=head3 _is_connected
451
=head3 _is_connected
394
452
395
Internal method to check if transport is currently connected.
453
Internal method to check if transport is currently connected.
(-)a/Koha/File/Transport/SFTP.pm (+30 lines)
Lines 21-26 use Koha::Logger; Link Here
21
21
22
use File::Spec;
22
use File::Spec;
23
use IO::File;
23
use IO::File;
24
use JSON qw( encode_json );
24
use Net::SFTP::Foreign;
25
use Net::SFTP::Foreign;
25
use Try::Tiny;
26
use Try::Tiny;
26
27
Lines 134-139 sub _download_file { Link Here
134
    return 1;
135
    return 1;
135
}
136
}
136
137
138
=head3 _delete_file
139
140
    my $success = $server->_delete_file($filename);
141
142
Passed a filename, this will delete the file from the current directory of the server connection.
143
144
Returns true on success or undefined on failure.
145
146
=cut
147
148
sub _delete_file {
149
    my ( $self, $remote_file ) = @_;
150
    my $operation = "delete";
151
152
    $self->{connection}->remove($remote_file) or return $self->_abort_operation( $operation, $remote_file );
153
154
    $self->add_message(
155
        {
156
            message => $operation,
157
            type    => 'success',
158
            payload => {
159
                remote_file => $remote_file,
160
            }
161
        }
162
    );
163
164
    return 1;
165
}
166
137
=head3 _change_directory
167
=head3 _change_directory
138
168
139
    my $success = $server->_change_directory($directory);
169
    my $success = $server->_change_directory($directory);
(-)a/t/db_dependent/Koha/File/Transports.t (-2 / +3 lines)
Lines 31-37 my $schema = Koha::Database->new->schema; Link Here
31
my $builder = t::lib::TestBuilder->new;
31
my $builder = t::lib::TestBuilder->new;
32
32
33
subtest 'Polymorphic object creation' => sub {
33
subtest 'Polymorphic object creation' => sub {
34
    plan tests => 6;
34
    plan tests => 7;
35
35
36
    $schema->storage->txn_begin;
36
    $schema->storage->txn_begin;
37
37
Lines 92-97 subtest 'Polymorphic object creation' => sub { Link Here
92
92
93
    can_ok( $local_transport, 'rename_file' );
93
    can_ok( $local_transport, 'rename_file' );
94
94
95
    can_ok( $local_transport, 'delete_file' );
96
95
    $schema->storage->txn_rollback;
97
    $schema->storage->txn_rollback;
96
};
98
};
97
99
98
- 

Return to bug 41020