[PATCH 2/3] Move decision which files to handle into chunkers
Michael Hanselmann
public at hansmi.ch
Wed Aug 13 21:18:13 UTC 2008
This makes the code more flexible and makes it easier to add more
chunkers.
---
lib/Brackup/Chunker/Default.pm | 7 +++++++
lib/Brackup/Chunker/MP3.pm | 15 ++++++++++-----
lib/Brackup/File.pm | 25 +++++++++++++++++++------
3 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/lib/Brackup/Chunker/Default.pm b/lib/Brackup/Chunker/Default.pm
index a661f21..a4691ff 100644
--- a/lib/Brackup/Chunker/Default.pm
+++ b/lib/Brackup/Chunker/Default.pm
@@ -2,6 +2,13 @@ package Brackup::Chunker::Default;
use strict;
use warnings;
+sub handles {
+ my ($class, $file) = @_;
+
+ # This is the default chunker. It handles every file type.
+ return 1;
+}
+
sub get_offsets {
my ($class, $file, $cb) = @_;
diff --git a/lib/Brackup/Chunker/MP3.pm b/lib/Brackup/Chunker/MP3.pm
index fe5cf46..4943003 100644
--- a/lib/Brackup/Chunker/MP3.pm
+++ b/lib/Brackup/Chunker/MP3.pm
@@ -7,6 +7,15 @@ BEGIN {
$HAVE_MP3_INFO = eval "use MP3::Info (); 1;";
}
+sub handles {
+ my ($class, $file) = @_;
+
+ return 0 unless $file->root->smart_mp3_chunking;
+
+ # Only handle MP3 files
+ return $file->path =~ m/\.mp3$/i;
+}
+
sub get_offsets {
my ($class, $file, $cb) = @_;
@@ -41,13 +50,9 @@ sub main_music_range {
my ($file) = @_;
my $size = $file->size;
- # if not an mp3, include the whole file
- unless ($file =~ /\.mp3$/i) {
- return (0, $size);
- }
-
my $info = MP3::Info::get_mp3info($file);
unless ($info && defined $info->{OFFSET}) {
+ # TODO: call default chunker
return (0, $size);
}
diff --git a/lib/Brackup/File.pm b/lib/Brackup/File.pm
index 8dc2ee9..827eff1 100644
--- a/lib/Brackup/File.pm
+++ b/lib/Brackup/File.pm
@@ -12,6 +12,11 @@ use Brackup::PositionedChunk;
use Brackup::Chunker::Default;
use Brackup::Chunker::MP3;
+my @chunkers = (
+ 'Brackup::Chunker::MP3',
+ 'Brackup::Chunker::Default',
+);
+
sub new {
my ($class, %opts) = @_;
my $self = bless {}, $class;
@@ -99,12 +104,17 @@ sub cachekey {
# into its own small chunks). This way the mp3 metadata can be
# changed without needing to back up the entire file again ... just
# the changed metadata.
-sub file_chunker {
+sub find_file_chunker {
my $self = shift;
- if ($self->{path} =~ /\.mp3$/i && $self->{root}->smart_mp3_chunking) {
- return "Brackup::Chunker::MP3";
- }
- return "Brackup::Chunker::Default";
+
+ # Try to find chunker
+ foreach (@chunkers) {
+ if ($_->handles($self)) {
+ return $_;
+ }
+ }
+
+ die "No chunker found";
}
sub chunks {
@@ -122,11 +132,14 @@ sub chunks {
# then pass ourselves to it to get our chunks.
my @chunk_list;
+ # Get the appropriate FileChunker for this file type
+ my $chunker = $self->find_file_chunker();
+
my $size = $self->size;
my $last_offset = undef;
my $last_len = undef;
- $self->file_chunker->get_offsets($self, sub {
+ $chunker->get_offsets($self, sub {
my ($offset, $len) = @_;
# Lots of verification on offset and length
--
1.5.5.3
More information about the brackup
mailing list