WebService-Bluga-Webthumb

 view release on metacpan or  search on metacpan

lib/WebService/Bluga/Webthumb.pm  view on Meta::CPAN

at which the contents of that directory can be served from - it's up to you
to make that happen via your preferred method.

Note that if fetching and caching the thumbnail fails, we will return the
direct URL to the webthumb API such that the client's browser gets a chance
to fetch it from there instead.  I'm not sure this is the best behaviour,
it was what happened before 0.06 without much thought put in to it.  It makes
some sense as it may lead to the client being able to fetch the thumbnail
if the problem was between our server and the webthumb service, but it may
also be an unexpected surprise - so a future release may add an option to
change this behaviour and cause a hard failure if fetching & caching the
thumbnail fails (poke me if that would be useful to you!)

=item cache_url_stub

As above, if you use C<cache_dir> to arrange for generated thumbnails to be
fetched and cached locally, you must set C<cache_url_stub> to the URL at
which the contents of that directory are served; the returned thumbnail URL
will then be the value of C<cache_url_stub> with the filename (which is the
hash of the URL and desired size) appended).

=item timeout

When fetching thumbnails from the webthumb server to cache locally, this
timeout value decides how long we wait - it defaults to 3 seconds.

=back

=cut

sub new {
    my $class = shift;
    if (@_ % 2 != 0) {
        croak "Uneven number of parameters provided";
    }

    my %params = @_;
    
    # TODO: more extensive validation
    if (!$params{user} || !$params{api_key}) {
        croak "'user' and 'api_key' params must be provided";
    }

    if (exists $params{size} 
        && !grep { $params{size} eq $_ } qw(small medium medium2 large)
    ) {
        croak "Invalid size $params{size} supplied!";
    } elsif (!exists $params{size}) {
        $params{size} = 'medium';
    }

    if (!exists $params{cache}) {
        $params{cache} = 14;
    }

    if (exists $params{cache_dir} && ! exists $params{cache_url_stub}) {
        croak "Must supply cache_url_stub if you supply cache_dir";
    }

    my $self = \%params;
    bless $self => $class;

    $self->{ua} = LWP::UserAgent->new(
        agent => __PACKAGE__ . '/' . $VERSION,
        timeout => $params{timeout} || 3,
    );
    return $self;
}

=back

=head1 Instance methods

=over 4

=item thumb_url

Given an URL, and optionally C<size> / C<cache> params to override those from
the object, returns an URL to the thumbnail, to use in an IMG tag.

=cut

sub thumb_url {
    my ($self, $url, $params) = @_;

    # Get our params, use defaults from the object
    $params ||= {};
    $params->{$_} ||= $self->{$_}
        for qw(size cache cache_dir cache_url_stub);

    # First, if we're caching locally, we need to see if we already have a
    # cached version; if so, it's easy
    if (my $url = $self->_get_cached_url($url, $params)) {
        return $url;
    }

    # Generate the appropriate URL:
    my $uri = URI->new('http://webthumb.bluga.net/easythumb.php');
    $uri->query_form(
        url   => $url,
        size  => $params->{size},
        cache => $params->{cache},
        user  => $self->{user},
        hash  => Digest::MD5::md5_hex(join '',
            strftime("%Y%m%d", gmtime(time())),
            $url,
            $self->{api_key}
        ),
    );

    # If we're caching, we want to fetch the resulting thumbnail and store it
    # locally, then return the URL to that instead
    if ($params->{cache_dir}) {
        my $req = $self->{ua}->get($uri);
        if ($req->is_success) {
            my $url = $self->_cache_image($url, $params, $req->content);
            return $url if defined $url;
        } else {
            # We couldn't cache it, just return the calculated URL; maybe
            # the user will have more luck fetching it directly as they
            # would have without caching enabled (and this is the implied

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 7.256 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-cec75d87357c )