X-Git-Url: https://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FAudioFile.pm;h=ffd78d63d4e6240a0f76c8e098aef9ab2a8130ab;hb=9ef40433a1ba4044561f5941b07ba0d90a085942;hp=91462bad5e7f84f53b1b567ae8d2462b2b4173bc;hpb=c59f9ef4a820793a58120c25d0c1a773afed79f8;p=id3fs.git diff --git a/lib/ID3FS/AudioFile.pm b/lib/ID3FS/AudioFile.pm index 91462ba..ffd78d6 100644 --- a/lib/ID3FS/AudioFile.pm +++ b/lib/ID3FS/AudioFile.pm @@ -1,35 +1,199 @@ -package ID3FS::File; +# id3fs - a FUSE-based filesystem for browsing audio metadata +# Copyright (C) 2010 Ian Beckwith +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +package ID3FS::AudioFile; use strict; use warnings; -use ID3FS::File::Mp3; -use ID3FS::File::Ogg; -use ID3FS::File::Flac; +use ID3FS::AudioFile::Mp3; +use ID3FS::AudioFile::Ogg; +use ID3FS::AudioFile::Flac; -# omg a factory class, I feel vaguely dirty sub new { - my (undef,$path)=@_; - my $ext=($path=~/.*\.(.*)/)[0]; + my $proto=shift; + my $class=ref($proto) || $proto; + my $self={}; + bless($self,$class); + + $self->{path}=shift; + my $ext=($self->{path}=~/.*\.(.*)/)[0]; return undef unless($ext); + my $me=shift; $ext=lc($ext); if($ext eq "mp3") { - return ID3FS::File::Mp3->new($path); + $self->{audiofile}=ID3FS::AudioFile::Mp3->new($self->{path}); } elsif($ext eq "ogg") { - return ID3FS::File::Ogg->new($path); + $self->{audiofile}=ID3FS::AudioFile::Ogg->new($self->{path}); } elsif($ext eq "flac") { - return ID3FS::File::Flac->new($path); + $self->{audiofile}=ID3FS::AudioFile::Flac->new($self->{path}); } else { - print("Unknown extension: $ext\n"); + print("$me: $self->{path}: Unknown extension: $ext\n"); return undef; } + return $self; +} + +sub set +{ + my ($self, $thing, $value)=@_; + if($value) + { + $value=$self->sanitise($self->stripslashes($value)); + $self->{audiofile}->$thing($value); + } + else + { + $value=$self->sanitise($self->stripslashes($self->{audiofile}->$thing())); + } + return $value; +} + +sub artist { return(shift->set("artist", @_)); } +sub album { return(shift->set("album", @_)); } +sub track { return(shift->set("track", @_)); } +sub tracknum { return(shift->set("tracknum", @_)); } +sub v1genre { return(shift->set("v1genre", @_)); } +sub comment { return(shift->set("comment", @_)); } +sub audiotype { return(shift->set("audiotype")); } +sub haspic { return(shift->set("haspic")); } +sub path { return(shift->{path}); } + +sub delete_artist { shift->{audiofile}->delete_artist(); } +sub delete_album { shift->{audiofile}->delete_album(); } +sub delete_track { shift->{audiofile}->delete_track(); } +sub delete_tracknum { shift->{audiofile}->delete_tracknum(); } +sub delete_year { shift->{audiofile}->delete_year(); } +sub delete_v1genre { shift->{audiofile}->delete_v1genre(); } +sub delete_comment { shift->{audiofile}->delete_comment(); } +sub delete_all { shift->{audiofile}->delete_all(); } +sub delete_genre { shift->{audiofile}->delete_genre(); } +sub delete_tags { shift->{audiofile}->delete_tags(@_); } +sub channels { shift->{audiofile}->channels(); } +sub bitrate { shift->{audiofile}->bitrate(); } +sub samplerate { shift->{audiofile}->samplerate(@_); } + +sub year +{ + my ($self, $year)=@_; + if($year) + { + $year=$self->format_year($year); + $self->{audiofile}->year($year); + } + else + { + $year=$self->{audiofile}->year(); + } + return $year; +} + +sub format_year +{ + my ($self, $year)=@_; + if($year) + { + $year=$self->sanitise($self->stripslashes($year)); + if(defined($year) && $year =~/(\d{4})/) + { + $year=$1; + } + } + return $year; +} + +sub add_tags +{ + my($self, $tags)=@_; + my @tags=split(/\s*,\s*/, $tags); + $self->{audiofile}->add_tags(@tags); +} + +sub tags +{ + my $self=shift; + my @intags=$self->{audiofile}->tags(); + my @outtags=(); + return() unless(@intags); + @intags = grep { defined($_); } @intags; + # combine then split on commas + # so multiple comma-delimited tags will work + @intags=split(/\s*,\s*/, join(', ', @intags)); + for my $tag (@intags) + { + next unless(length($tag)); + next unless($tag =~ /\S+/); + $tag=$self->sanitise($tag); + my ($tagname, $tagval)=($tag, undef); + if($tag=~/^([^\/]+)\/(.*)/) + { + ($tagname, $tagval)=($1, $2); + } + push(@outtags, [ $tagname, $tagval ]); + } + return @outtags; +} + +sub write +{ + shift->{audiofile}->write(); +} + +sub sanitise +{ + my ($self, $text)=@_; + $text =~ s/[^[:print:]]//g if(defined($text)); + return $text; +} + +sub stripslashes +{ + my ($self, $text)=@_; + $text =~ s/\//-/g if(defined($text)); + return $text; +} + +# This location for these subs is pretty much arbitrary +sub uniq +{ + # class method + shift if(ref($_[0]) eq "ID3FS::AudioFile"); + my (@things)=@_; + my %hash=(); + @hash{@things}=(); + return(sort keys(%hash)); +} + +sub list_remove +{ + my($remove, $list)=@_; + return(()) unless($list && @$list); + my @list=@$list; + for my $tag (@$remove) + { + @list=grep { $_ ne $tag; } @list; + } + return(@list); } 1;