| 1 | package App::Pimpd::Transfer; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | BEGIN { | ||
| 5 | use Exporter; | ||
| 6 | use vars qw(@ISA @EXPORT); | ||
| 7 | @ISA = qw(Exporter); | ||
| 8 | @EXPORT = qw( | ||
| 9 | cp | ||
| 10 | cp_album | ||
| 11 | cp_list | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | |||
| 15 | use Carp qw(confess); | ||
| 16 | use File::Copy; | ||
| 17 | use App::Pimpd; | ||
| 18 | use App::Pimpd::Validate; | ||
| 19 | use File::Basename qw(basename); | ||
| 20 | use File::LsColor qw(ls_color); | ||
| 21 | |||
| 22 | sub cp_album { | ||
| 23 | my $destination = shift; | ||
| 24 | |||
| 25 | my $album = $mpd->current->album; | ||
| 26 | my $artist = $mpd->current->artist; | ||
| 27 | |||
| 28 | if(!defined($album)) { | ||
| 29 | warn "song doesn't have any album tag!\n"; | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | my @tracks = grep { | ||
| 34 | $artist eq $_->artist | ||
| 35 | } $mpd->collection->songs_from_album($album);; | ||
| 36 | |||
| 37 | @tracks = map { $config{music_directory} . '/' . $_->file } @tracks; | ||
| 38 | |||
| 39 | if(remote_host()) { | ||
| 40 | for(@tracks) { | ||
| 41 | #$_ = escape($_); | ||
| 42 | _scp($_, $destination); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | else { | ||
| 47 | for(@tracks) { | ||
| 48 | #$_ = escape($_); | ||
| 49 | if(copy($_, $destination)) { | ||
| 50 | printf("%40.40s => %s\n", basename($_), $destination); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | warn("cp_album: $!"); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub cp { | ||
| 61 | my $destination = shift; | ||
| 62 | #is_existing_dir($destination); # FIXME | ||
| 63 | |||
| 64 | if(empty_playlist()) { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $file; | ||
| 69 | if($config{music_directory} =~ m|.+/$|m) { | ||
| 70 | $file = $config{music_directory} .= $mpd->current->file; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | $file = $config{music_directory} . '/' . $mpd->current->file; | ||
| 74 | } | ||
| 75 | |||
| 76 | if(remote_host()) { | ||
| 77 | return _scp($file, $destination); | ||
| 78 | } | ||
| 79 | |||
| 80 | else { | ||
| 81 | if(copy($file, $destination)) { | ||
| 82 | printf("'%s' -> '%s'\n", ls_color(basename($file)), $destination); | ||
| 83 | return 1; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | confess("cp: $!"); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _scp { | ||
| 93 | my($source, $dest) = @_; | ||
| 94 | $source = escape($source); | ||
| 95 | |||
| 96 | system('scp', '-r', | ||
| 97 | "-P $config{ssh_port}", | ||
| 98 | qq($config{ssh_host}:$source), $dest | ||
| 99 | ) == 0;# or confess("scp: $!"); | ||
| 100 | |||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | __END__ |