Hi everybody,
I posted to the users list on Friday about trouble I was having with
automatic thumbnailing. Long story short, mixed-case filenames were
colliding with lowercase URI formats.
Here's that thread, if you're interested:
http://www.gossamer-threads.com/lists/bricolage/users/39880
David came up with a better fix for this bug than the one I submitted,
which is awesome. It raised a question for me, though. This is more a
matter of programming practice than an actual Bricolage question, but I
wonder if somebody could shed a little light.
The code in find_or_create_alternate goes like this:
1. Figure out the URI of the desired thumbnail
2. Search for an existing document with that URI (return it if found)
3. No existing document with that URI. Make a new one and return it
Here's the bit of code that figures out the URI of the desired
thumbnail:
my $uri = do {
# We need to use the same element type and the new file name, so that
# the URI is correct. So we trick get_element_type() to return the
# element type and file name we need. Yeah, it's a hack, but it's the
# cleanest way to do it without creating unnecesary pain.
local $self->{_element_type_object} = $et;
local $self->{file_name} = $alt_fn;
URI::Escape::uri_unescape($self->get_uri($self->get_primary_oc))
};
Basically, it takes the "source" image object, locally changes its
element type and its filename to match the "target," and then it simply
calls get_uri(). Awesome!
Here's my question: why is this a hack? To my ignorant eye, it just
seems clever. What about this code makes its author queasy enough to add
the comment?
Curious and grateful for explanations,
Bret
On Sun, 2011-04-10 at 16:19 -0400, Bret Dawson wrote:
> Hi everybody,
>
> I just figured this out. The inital media document had a mixed-case
> filename, but a lowercase URI format.
>
> So the regex that was trying to change the initial image's URI into the
> thumbnail's URI was failing to find the filename, which left the
> "target" URI the same as the starter one. Then the lookup found the
> image with the desired URI and returned the inital picture.
>
> I've committed a patch and sent a pull request.
>
>
> Cheers,
>
> Bret
>
>
> On Sun, 2011-04-10 at 19:51 +0200, Rolf Schaufelberger wrote:
> > Hi,
> >
> > I just started using this function two days ago and it works fine.
> > May be something wrong with $element_names_and_sizes{$size} ?
> >
> > Am 09.04.2011 um 19:18 schrieb Bret Dawson:
> >
> > > Hi David,
> > >
> > >> Is $pic a auto_generated_image already?
> > >
> > > No, it's just an "image". Do they need to be the same type?
> > >
> > >
> > > Thanks,
> > >
> > > Bret
> > >
> > >
> > >
> > > On Sat, 2011-04-09 at 09:06 -0700, David E. Wheeler wrote:
> > >> On Apr 8, 2011, at 3:10 PM, Bret Dawson wrote:
> > >>
> > >>> Hi everybody,
> > >>>
> > >>> I'm just starting to use the find_or_create_alternate() method to make
> > >>> thumbnails, and I must be doing something wrong. When I try something
> > >>> like this:
> > >>>
> > >>> my $size = 240;
> > >>> my $thumb = $pic->find_or_create_alternate({
> > >>> title_prefix => '',
> > >>> title_suffix => '-' . $size,
> > >>> file_suffix => '_sized_nicely,
> > >>> et_key_name => 'auto_generated_image',
> > >>> relate => 0,
> > >>> transformer => sub {
> > >>> shift->scale( xpixels => $element_names_and_sizes{$size} );
> > >>> },
> > >>> });
> > >>>
> > >>> ...no new media document is created, and the $thumb returned is the
> > >>> original $pic. Any idea what I'm doing incorrectly?
> > >>
> > >
> > >>
> > >> Best,
> > >>
> > >> David
> > >>
> > >>
> > >
> > > --
> > > Bret Dawson
> > > Producer
> > > Pectopah Productions Inc.
> > > (416) 895-7635
> > > bret@pectopah.com
> > > www.pectopah.com
> > >
> >
> > Mit freundlichen Grüßen
> > Rolf Schaufelberger
> > Geschäftsführer
> >
> > plusW GmbH
> > Vorstadtstr. 61 -67 Tel. 07181 47 47 305
> > 73614 Schorndorf Fax. 07181 47 45 344
> >
> > www.plusw.de
> > www.mypixler.com
> > www.calendrino.de
> >
> >
> >
> >
> >
> >
> >
>
--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com
I posted to the users list on Friday about trouble I was having with
automatic thumbnailing. Long story short, mixed-case filenames were
colliding with lowercase URI formats.
Here's that thread, if you're interested:
http://www.gossamer-threads.com/lists/bricolage/users/39880
David came up with a better fix for this bug than the one I submitted,
which is awesome. It raised a question for me, though. This is more a
matter of programming practice than an actual Bricolage question, but I
wonder if somebody could shed a little light.
The code in find_or_create_alternate goes like this:
1. Figure out the URI of the desired thumbnail
2. Search for an existing document with that URI (return it if found)
3. No existing document with that URI. Make a new one and return it
Here's the bit of code that figures out the URI of the desired
thumbnail:
my $uri = do {
# We need to use the same element type and the new file name, so that
# the URI is correct. So we trick get_element_type() to return the
# element type and file name we need. Yeah, it's a hack, but it's the
# cleanest way to do it without creating unnecesary pain.
local $self->{_element_type_object} = $et;
local $self->{file_name} = $alt_fn;
URI::Escape::uri_unescape($self->get_uri($self->get_primary_oc))
};
Basically, it takes the "source" image object, locally changes its
element type and its filename to match the "target," and then it simply
calls get_uri(). Awesome!
Here's my question: why is this a hack? To my ignorant eye, it just
seems clever. What about this code makes its author queasy enough to add
the comment?
Curious and grateful for explanations,
Bret
On Sun, 2011-04-10 at 16:19 -0400, Bret Dawson wrote:
> Hi everybody,
>
> I just figured this out. The inital media document had a mixed-case
> filename, but a lowercase URI format.
>
> So the regex that was trying to change the initial image's URI into the
> thumbnail's URI was failing to find the filename, which left the
> "target" URI the same as the starter one. Then the lookup found the
> image with the desired URI and returned the inital picture.
>
> I've committed a patch and sent a pull request.
>
>
> Cheers,
>
> Bret
>
>
> On Sun, 2011-04-10 at 19:51 +0200, Rolf Schaufelberger wrote:
> > Hi,
> >
> > I just started using this function two days ago and it works fine.
> > May be something wrong with $element_names_and_sizes{$size} ?
> >
> > Am 09.04.2011 um 19:18 schrieb Bret Dawson:
> >
> > > Hi David,
> > >
> > >> Is $pic a auto_generated_image already?
> > >
> > > No, it's just an "image". Do they need to be the same type?
> > >
> > >
> > > Thanks,
> > >
> > > Bret
> > >
> > >
> > >
> > > On Sat, 2011-04-09 at 09:06 -0700, David E. Wheeler wrote:
> > >> On Apr 8, 2011, at 3:10 PM, Bret Dawson wrote:
> > >>
> > >>> Hi everybody,
> > >>>
> > >>> I'm just starting to use the find_or_create_alternate() method to make
> > >>> thumbnails, and I must be doing something wrong. When I try something
> > >>> like this:
> > >>>
> > >>> my $size = 240;
> > >>> my $thumb = $pic->find_or_create_alternate({
> > >>> title_prefix => '',
> > >>> title_suffix => '-' . $size,
> > >>> file_suffix => '_sized_nicely,
> > >>> et_key_name => 'auto_generated_image',
> > >>> relate => 0,
> > >>> transformer => sub {
> > >>> shift->scale( xpixels => $element_names_and_sizes{$size} );
> > >>> },
> > >>> });
> > >>>
> > >>> ...no new media document is created, and the $thumb returned is the
> > >>> original $pic. Any idea what I'm doing incorrectly?
> > >>
> > >
> > >>
> > >> Best,
> > >>
> > >> David
> > >>
> > >>
> > >
> > > --
> > > Bret Dawson
> > > Producer
> > > Pectopah Productions Inc.
> > > (416) 895-7635
> > > bret@pectopah.com
> > > www.pectopah.com
> > >
> >
> > Mit freundlichen Grüßen
> > Rolf Schaufelberger
> > Geschäftsführer
> >
> > plusW GmbH
> > Vorstadtstr. 61 -67 Tel. 07181 47 47 305
> > 73614 Schorndorf Fax. 07181 47 45 344
> >
> > www.plusw.de
> > www.mypixler.com
> > www.calendrino.de
> >
> >
> >
> >
> >
> >
> >
>
--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com