|
Lines 37-43
Koha::File::Transport - Base class for file transport handling
Link Here
|
| 37 |
|
37 |
|
| 38 |
=head1 DESCRIPTION |
38 |
=head1 DESCRIPTION |
| 39 |
|
39 |
|
| 40 |
Base class providing common functionality for FTP/SFTP file transport. |
40 |
Base class providing common functionality for FTP/SFTP/Local file transport. |
|
|
41 |
|
| 42 |
This class provides both a traditional explicit API and a simplified auto-managing API. |
| 43 |
|
| 44 |
Traditional API requires manual connection management: |
| 45 |
$transport->connect(); |
| 46 |
$transport->change_directory($path); |
| 47 |
$transport->upload_file($local, $remote); |
| 48 |
$transport->disconnect(); |
| 49 |
|
| 50 |
Simplified API handles connections automatically: |
| 51 |
$transport->upload_file($local, $remote, { path => '/custom/path' }); |
| 52 |
# OR using default upload directory: |
| 53 |
$transport->upload_file($local, $remote); |
| 41 |
|
54 |
|
| 42 |
=cut |
55 |
=cut |
| 43 |
|
56 |
|
|
Lines 92-97
sub store {
Link Here
|
| 92 |
return $self; |
105 |
return $self; |
| 93 |
} |
106 |
} |
| 94 |
|
107 |
|
|
|
108 |
=head3 _ensure_connected |
| 109 |
|
| 110 |
$transport->_ensure_connected(); |
| 111 |
|
| 112 |
Internal method that ensures a connection exists, connecting if needed. |
| 113 |
Returns true if connected, false if connection failed. |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
sub _ensure_connected { |
| 118 |
my ($self) = @_; |
| 119 |
|
| 120 |
# Check if already connected (transport-specific) |
| 121 |
return 1 if $self->_is_connected(); |
| 122 |
|
| 123 |
# Reset directory state on new connection |
| 124 |
$self->{_user_set_directory} = 0; |
| 125 |
|
| 126 |
# Attempt to connect |
| 127 |
return $self->connect(); |
| 128 |
} |
| 129 |
|
| 130 |
=head3 _is_connected |
| 131 |
|
| 132 |
my $connected = $transport->_is_connected(); |
| 133 |
|
| 134 |
Internal method to check if transport is currently connected. |
| 135 |
Must be implemented by subclasses. |
| 136 |
|
| 137 |
=cut |
| 138 |
|
| 139 |
sub _is_connected { |
| 140 |
my ($self) = @_; |
| 141 |
die "Subclass must implement _is_connected"; |
| 142 |
} |
| 143 |
|
| 144 |
=head3 _auto_change_directory |
| 145 |
|
| 146 |
$transport->_auto_change_directory($dir_type, $custom_path); |
| 147 |
|
| 148 |
Internal method that automatically changes to the appropriate directory. |
| 149 |
$dir_type is 'upload' or 'download', $custom_path is optional override. |
| 150 |
|
| 151 |
=cut |
| 152 |
|
| 153 |
sub _auto_change_directory { |
| 154 |
my ( $self, $dir_type, $custom_path ) = @_; |
| 155 |
|
| 156 |
my $target_dir; |
| 157 |
if ($custom_path) { |
| 158 |
$target_dir = $custom_path; |
| 159 |
} elsif ( $dir_type eq 'upload' ) { |
| 160 |
$target_dir = $self->upload_directory; |
| 161 |
} elsif ( $dir_type eq 'download' ) { |
| 162 |
$target_dir = $self->download_directory; |
| 163 |
} |
| 164 |
|
| 165 |
return 1 unless $target_dir; # No directory to change to |
| 166 |
|
| 167 |
return $self->change_directory($target_dir); |
| 168 |
} |
| 169 |
|
| 95 |
=head3 plain_text_password |
170 |
=head3 plain_text_password |
| 96 |
|
171 |
|
| 97 |
my $password = $server->plain_text_password; |
172 |
my $password = $server->plain_text_password; |
|
Lines 201-215
sub connect {
Link Here
|
| 201 |
|
276 |
|
| 202 |
=head3 upload_file |
277 |
=head3 upload_file |
| 203 |
|
278 |
|
| 204 |
$transport->upload_file($file); |
279 |
# Traditional explicit API: |
|
|
280 |
$transport->upload_file($local_file, $remote_file); |
| 281 |
|
| 282 |
# Simplified auto-managing API: |
| 283 |
$transport->upload_file($local_file, $remote_file, { path => '/custom/path' }); |
| 205 |
|
284 |
|
| 206 |
Method for uploading a file to the current file server |
285 |
Method for uploading a file to the current file server. |
|
|
286 |
|
| 287 |
When called with options hashref, automatically handles connection and directory management. |
| 288 |
When called without options, uses traditional explicit workflow. |
| 207 |
|
289 |
|
| 208 |
=cut |
290 |
=cut |
| 209 |
|
291 |
|
| 210 |
sub upload_file { |
292 |
sub upload_file { |
| 211 |
my ( $self, $local_file, $remote_file ) = @_; |
293 |
my ( $self, $local_file, $remote_file, $options ) = @_; |
| 212 |
die "Subclass must implement upload_file"; |
294 |
|
|
|
295 |
return unless $self->_ensure_connected(); |
| 296 |
|
| 297 |
# Only auto-change directory if: |
| 298 |
# 1. Options provided with custom path (simplified API), OR |
| 299 |
# 2. No explicit directory set by user AND default upload_directory exists (traditional API) |
| 300 |
if ( $options && $options->{path} ) { |
| 301 |
|
| 302 |
# Simplified API - use custom path |
| 303 |
return unless $self->_auto_change_directory( 'upload', $options->{path} ); |
| 304 |
} elsif ( !$self->{_user_set_directory} ) { |
| 305 |
|
| 306 |
# Traditional API - use default directory only if user hasn't set one |
| 307 |
return unless $self->_auto_change_directory( 'upload', undef ); |
| 308 |
} |
| 309 |
|
| 310 |
return $self->_upload_file( $local_file, $remote_file ); |
| 311 |
} |
| 312 |
|
| 313 |
=head3 _upload_file |
| 314 |
|
| 315 |
$transport->_upload_file($local_file, $remote_file); |
| 316 |
|
| 317 |
Internal method that performs the protocol-specific upload operation. |
| 318 |
Must be implemented by subclasses. Called by upload_file after connection |
| 319 |
and directory management. |
| 320 |
|
| 321 |
=cut |
| 322 |
|
| 323 |
sub _upload_file { |
| 324 |
my ($self) = @_; |
| 325 |
die "Subclass must implement _upload_file"; |
| 213 |
} |
326 |
} |
| 214 |
|
327 |
|
| 215 |
=head3 download_file |
328 |
=head3 download_file |
|
Lines 221-241
Method for downloading a file from the current file server
Link Here
|
| 221 |
=cut |
334 |
=cut |
| 222 |
|
335 |
|
| 223 |
sub download_file { |
336 |
sub download_file { |
| 224 |
my ( $self, $remote_file, $local_file ) = @_; |
337 |
my ( $self, $remote_file, $local_file, $options ) = @_; |
| 225 |
die "Subclass must implement download_file"; |
338 |
|
|
|
339 |
return unless $self->_ensure_connected(); |
| 340 |
|
| 341 |
# Only auto-change directory if: |
| 342 |
# 1. Options provided with custom path (simplified API), OR |
| 343 |
# 2. No explicit directory set by user AND default download_directory exists (traditional API) |
| 344 |
if ( $options && $options->{path} ) { |
| 345 |
|
| 346 |
# Simplified API - use custom path |
| 347 |
return unless $self->_auto_change_directory( 'download', $options->{path} ); |
| 348 |
} elsif ( !$self->{_user_set_directory} ) { |
| 349 |
|
| 350 |
# Traditional API - use default directory only if user hasn't set one |
| 351 |
return unless $self->_auto_change_directory( 'download', undef ); |
| 352 |
} |
| 353 |
|
| 354 |
return $self->_download_file( $remote_file, $local_file ); |
| 226 |
} |
355 |
} |
| 227 |
|
356 |
|
| 228 |
=head3 change_directory |
357 |
=head3 _download_file |
| 229 |
|
358 |
|
| 230 |
my $files = $transport->change_directory($path); |
359 |
$transport->_download_file($remote_file, $local_file); |
| 231 |
|
360 |
|
| 232 |
Method for changing the current directory on the connected file server |
361 |
Internal method that performs the protocol-specific download operation. |
|
|
362 |
Must be implemented by subclasses. Called by download_file after connection |
| 363 |
and directory management. |
| 233 |
|
364 |
|
| 234 |
=cut |
365 |
=cut |
| 235 |
|
366 |
|
| 236 |
sub change_directory { |
367 |
sub _download_file { |
| 237 |
my ( $self, $path ) = @_; |
368 |
my ($self) = @_; |
| 238 |
die "Subclass must implement change_directory"; |
369 |
die "Subclass must implement _download_file"; |
|
|
370 |
} |
| 371 |
|
| 372 |
=head3 rename_file |
| 373 |
|
| 374 |
my $success = $transport->rename_file($old_name, $new_name); |
| 375 |
|
| 376 |
Method for renaming a file on the current file server |
| 377 |
|
| 378 |
=cut |
| 379 |
|
| 380 |
sub rename_file { |
| 381 |
my ( $self, $old_name, $new_name ) = @_; |
| 382 |
|
| 383 |
return unless $self->_ensure_connected(); |
| 384 |
|
| 385 |
return $self->_rename_file( $old_name, $new_name ); |
| 386 |
} |
| 387 |
|
| 388 |
=head3 _rename_file |
| 389 |
|
| 390 |
$transport->_rename_file($old_name, $new_name); |
| 391 |
|
| 392 |
Internal method that performs the protocol-specific file rename operation. |
| 393 |
Must be implemented by subclasses. Called by rename_file after connection |
| 394 |
verification. |
| 395 |
|
| 396 |
=cut |
| 397 |
|
| 398 |
sub _rename_file { |
| 399 |
my ($self) = @_; |
| 400 |
die "Subclass must implement _rename_file"; |
| 239 |
} |
401 |
} |
| 240 |
|
402 |
|
| 241 |
=head3 list_files |
403 |
=head3 list_files |
|
Lines 247-267
Method for listing files in the current directory of the connected file server
Link Here
|
| 247 |
=cut |
409 |
=cut |
| 248 |
|
410 |
|
| 249 |
sub list_files { |
411 |
sub list_files { |
| 250 |
my ( $self, $path ) = @_; |
412 |
my ( $self, $options ) = @_; |
| 251 |
die "Subclass must implement list_files"; |
413 |
|
|
|
414 |
return unless $self->_ensure_connected(); |
| 415 |
|
| 416 |
# Only auto-change directory if: |
| 417 |
# 1. Options provided with custom path (simplified API), OR |
| 418 |
# 2. No explicit directory set by user AND default download_directory exists (traditional API) |
| 419 |
if ( $options && $options->{path} ) { |
| 420 |
|
| 421 |
# Simplified API - use custom path |
| 422 |
return unless $self->_auto_change_directory( 'download', $options->{path} ); |
| 423 |
} elsif ( !$self->{_user_set_directory} ) { |
| 424 |
|
| 425 |
# Traditional API - use default directory only if user hasn't set one |
| 426 |
return unless $self->_auto_change_directory( 'download', undef ); |
| 427 |
} |
| 428 |
|
| 429 |
return $self->_list_files(); |
| 252 |
} |
430 |
} |
| 253 |
|
431 |
|
| 254 |
=head3 rename_file |
432 |
=head3 _list_files |
| 255 |
|
433 |
|
| 256 |
my $success = $transport->rename_file($old_name, $new_name); |
434 |
my $files = $transport->_list_files(); |
| 257 |
|
435 |
|
| 258 |
Method for renaming a file on the current file server |
436 |
Internal method that performs the protocol-specific file listing operation. |
|
|
437 |
Must be implemented by subclasses. Called by list_files after connection |
| 438 |
and directory management. |
| 259 |
|
439 |
|
| 260 |
=cut |
440 |
=cut |
| 261 |
|
441 |
|
| 262 |
sub rename_file { |
442 |
sub _list_files { |
| 263 |
my ( $self, $old_name, $new_name ) = @_; |
443 |
my ($self) = @_; |
| 264 |
die "Subclass must implement rename_file"; |
444 |
die "Subclass must implement _list_files"; |
| 265 |
} |
445 |
} |
| 266 |
|
446 |
|
| 267 |
=head3 disconnect |
447 |
=head3 disconnect |
|
Lines 272-277
Method for disconnecting from the current file server
Link Here
|
| 272 |
|
452 |
|
| 273 |
=cut |
453 |
=cut |
| 274 |
|
454 |
|
|
|
455 |
=head3 change_directory |
| 456 |
|
| 457 |
my $files = $transport->change_directory($path); |
| 458 |
|
| 459 |
Method for changing the current directory on the connected file server |
| 460 |
|
| 461 |
=cut |
| 462 |
|
| 463 |
sub change_directory { |
| 464 |
my ( $self, $path ) = @_; |
| 465 |
|
| 466 |
# Mark that user has explicitly set a directory |
| 467 |
$self->{_user_set_directory} = 1; |
| 468 |
|
| 469 |
die "Subclass must implement change_directory"; |
| 470 |
} |
| 471 |
|
| 275 |
sub disconnect { |
472 |
sub disconnect { |
| 276 |
my ($self) = @_; |
473 |
my ($self) = @_; |
| 277 |
die "Subclass must implement disconnect"; |
474 |
die "Subclass must implement disconnect"; |