add copyright/license headers
[id3fs.git] / lib / ID3FS / AudioFile.pm
1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package ID3FS::AudioFile;
18
19 use strict;
20 use warnings;
21 use ID3FS::AudioFile::Mp3;
22 use ID3FS::AudioFile::Ogg;
23 use ID3FS::AudioFile::Flac;
24
25 sub new
26 {
27     my $proto=shift;
28     my $class=ref($proto) || $proto;
29     my $self={};
30     bless($self,$class);
31
32     my $path=shift;
33     my $ext=($path=~/.*\.(.*)/)[0];
34     return undef unless($ext);
35     my $me=shift;
36     $ext=lc($ext);
37     if($ext eq "mp3")
38     {
39         $self->{audiofile}=ID3FS::AudioFile::Mp3->new($path);
40     }
41     elsif($ext eq "ogg")
42     {
43         $self->{audiofile}=ID3FS::AudioFile::Ogg->new($path);
44     }
45     elsif($ext eq "flac")
46     {
47         $self->{audiofile}=ID3FS::AudioFile::Flac->new($path);
48     }
49     else
50     {
51         print("$me: $path: Unknown extension: $ext\n");
52         return undef;
53     }
54     return $self;
55 }
56
57 sub artist
58 {
59     my $self=shift;
60     return $self->sanitise($self->stripslashes($self->{audiofile}->artist()));
61 }
62
63 sub album
64 {
65     my $self=shift;
66     return $self->sanitise($self->stripslashes($self->{audiofile}->album()));
67 }
68
69 sub audiotype
70 {
71     my $self=shift;
72     return $self->sanitise($self->stripslashes($self->{audiofile}->audiotype()));
73 }
74
75 sub haspic
76 {
77     return undef; # FIXME
78 #    my $self=shift;
79 #    return $self->{audiofile}->haspic();
80 }
81
82 sub v1genre
83 {
84     my $self=shift;
85     return $self->sanitise($self->stripslashes($self->{audiofile}->v1genre()));
86 }
87
88 sub year
89 {
90     my $self=shift;
91     my $year=$self->sanitise($self->stripslashes($self->{audiofile}->year()));
92     if(defined($year) && $year =~/(\d{4})/)
93     {
94         $year=$1;
95     }
96     return $year;
97 }
98
99 sub tags
100 {
101     my $self=shift;
102     my @intags=$self->{audiofile}->tags();
103     my @outtags=();
104     return() unless(@intags);
105     @intags = grep { defined($_); } @intags;
106     # combine then split on commas
107     # so multiple comma-delimited tags will work
108     @intags=split(/\s*,\s*/, join(', ', @intags));
109     for my $tag (@intags)
110     {
111         next unless(length($tag));
112         next unless($tag =~ /\S+/);
113         $tag=$self->sanitise($tag);
114         my ($tagname, $tagval)=($tag, undef);
115         if($tag=~/^([^\/]+)\/(.*)/)
116         {
117             ($tagname, $tagval)=($1, $2);
118         }
119         push(@outtags, [ $tagname, $tagval ]);
120     }
121     return @outtags;
122 }
123
124 sub sanitise
125 {
126     my ($self, $text)=@_;
127     $text =~ s/[^[:print:]]//g if(defined($text));
128     return $text;
129 }
130
131 sub stripslashes
132 {
133     my ($self, $text)=@_;
134     $text =~ s/\//-/g if(defined($text));
135     return $text;
136 }
137
138 1;