From edb9b06c0863636ffa3a3ab97047d80de677bc23 Mon Sep 17 00:00:00 2001 From: Dave Lambley Date: Wed, 14 Mar 2012 17:24:39 +0000 Subject: [PATCH] Allow the temporary upload key to be selected on the fly. --- lib/MogileFS/Client/CallbackFile.pm | 7 ++++++- t/callbackfile_works.t | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/MogileFS/Client/CallbackFile.pm b/lib/MogileFS/Client/CallbackFile.pm index d53d4e8..9e861e7 100644 --- a/lib/MogileFS/Client/CallbackFile.pm +++ b/lib/MogileFS/Client/CallbackFile.pm @@ -56,7 +56,7 @@ sub store_file_from_fh { my $self = shift; return undef if $self->{readonly}; - my ($key, $class, $read_fh, $eventual_length, $opts) = @_; + my ($_key, $class, $read_fh, $eventual_length, $opts) = @_; $opts ||= {}; # Hint to Linux that doubling readahead will probably pay off. @@ -69,12 +69,17 @@ sub store_file_from_fh { my $create_close_args = $opts->{create_close_args} || {}; my @dests; # ( [devid,path,fid], [devid,path,fid], ... ) + + my $key; + my $get_new_dest = sub { if (@dests) { return pop @dests; } foreach (1..5) { + $key = ref($_key) eq 'CODE' ? $_key->() : $_key; + $self->run_hook('store_file_start', $self, $key, $class, $opts); $self->run_hook('new_file_start', $self, $key, $class, $opts); diff --git a/t/callbackfile_works.t b/t/callbackfile_works.t index 20e5ef4..8383e2f 100644 --- a/t/callbackfile_works.t +++ b/t/callbackfile_works.t @@ -57,6 +57,30 @@ ok $mogc, 'Have client'; }; } +{ + open(my $read_fh, "<", $0) or die "failed to open $0: $!"; + isa_ok($read_fh, 'GLOB'); + + my $exp_len = -s $read_fh; + my $key; + my $callback = $mogc->store_file_from_fh(sub { + $key = "test-".int(rand(100000)); + }, 'rip', $read_fh, $exp_len, {}); + + isa_ok($callback, 'CODE'); + $callback->($exp_len, 1); + + diag "key finally is $key\n"; + + lives_ok { + my ($fh, $fn) = tempfile; + $mogc->read_to_file($key, $fn); + is( -s $fn, $exp_len, 'Read file back with correct length' ) + or system("diff -u $0 $fn"); + is sha1($fn), $exp_sha, 'Read file back with correct SHA1'; + unlink $fn; + }; +} done_testing;