Skip to content

Commit

Permalink
Allow the temporary upload key to be selected on the fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Lambley committed Mar 14, 2012
1 parent a2c4939 commit edb9b06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/MogileFS/Client/CallbackFile.pm
Expand Up @@ -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.
Expand All @@ -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);

Expand Down
24 changes: 24 additions & 0 deletions t/callbackfile_works.t
Expand Up @@ -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;

0 comments on commit edb9b06

Please sign in to comment.