
bret at pectopah
Jun 24, 2013, 11:25 AM
Post #2 of 6
(179 views)
Permalink
|
|
Re: find_or_create_alternate apparently not finding an alternate it has already created
[In reply to]
|
|
Hi Charlie, I've seen this before, and I'm afraid I don't have an answer, only some hints. Specifically, the issue appears when media filenames have mixed case names, and expecially when they contain characters such as spaces and parentheses. Basically, anything that URI escaping will turn into a three character string beginning with a percent sign (e.g. "%28") seems to cause trouble. find_or_create_alternate builds the URI it expects to create by looking at a number of pieces, including the filename of the original image and the URI format of the target element type, and the case rules of the target output channel. I think there may be a bug someplace where the stored file name is mixed case, but the URI of the media document is lowercase, where the attempt to find a match uses the mixed case filename in the search for a match, which doesn't find one, but then on the attempt to make a new thumbnail, the URI collision happens and you get that error. Something similar seems to be at work with escaped characters. These get escaped and unescaped a lot in a variety of places, and I'm pretty sure the issue is that find_or_create_alternate is searching for existing URIs using escaped strings ("%28") when it should be using unsecaped ones ("("), or vice versa. So "file name.jpg" doesn't match "file%20name.jpg" on the search, and the collision only reveals itself on the attempt to make a new media document. I've tried and failed to come up with a reliable way of reproducing the error, but if you have one, a bug report would be very helpful. Hope this helps, Bret On 06/24/2013 12:02 PM, Charlie Harvey wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA512 > > Hi Bricolage folk, > > Bit of a head scratcher this. I am trying to make thumbnails with the > following code: > > <%args> > $for > $user > </%args> > <%perl>; > my ($suffix, $prefix, $width, $height) = ( '_thumb', 'Thumb for ', 200, > 120 ); > > return unless($for->isa('Bric::Biz::Asset::Business::Media::Image')); > # no point in dealing with non-images > return unless(grep {$for->get_file_name =~ /$_$/} qw(jpg jpeg png gif)); > # sometimes videos are images. who knew? > > my $thumb = eval { > $for->find_or_create_alternate({ > title_prefix => $prefix, > file_suffix => $suffix, > et_key_name => 'thumbnail', > relate => 1, > user => $user, > checkin => 1, > transformer => sub { > shift->scale( xpixels => $width )->crop( height => $height ); > } > }) > }; > > if($@) { > my $e = "Something went wrong. You definitely uploaded the related > media as a photograph, right?\nHere's the debug info: \n"; > $burner->throw_error(ref $@ ? $e . $@->error : $e . $@); > } > > $burner->blaze_another($thumb, $for->get_cover_date, 1) unless > $thumb->get_publish_status; > return $thumb unless $burner->get_mode == PUBLISH_MODE; > </%perl> > > > I call it from within a loop on a cover page (i.e. once per story). > Here's a truncated version of that: > % my $acount=0; > % foreach my $article ( @stories ) { > - -----------------8<--------------- > % my $thumb = ''; > % if (defined $media) { > % $thumb = $m->comp('/util/get_thumb.mc', for=>$media, > user=>Bric::App::Session::get_user_object()); > % } > > > <div class="hentry"> > % if ($thumb && $acount ) { > <div class="image-left"> > <img src="<% $thumb->get_primary_uri %>" alt="<% $thumb->get_title > %>" /> > </div> > % } > - -----------------8<--------------- > > When a thumbnail has previously been created (by another user) my > thumbnail code throws this error: > > "The URI '/category/2013/06/24/the_image_thumb.jpg' is not unique." > > Well, that is true enough, I suppose. There is indeed an element with > that URI already in existence. However, the way I read the docs: > > "The first thing this method does is see if a likely alternate already > exists by looking for a media document in the same site, with the > appropriate URI (as formed by the current media document's category, > cover date, and file name as modified by the file_prefix and file_suffix > parameters), and based on the appropriate element type. If such a media > document is found, it is simply returned and no further actions are taken." > > makes me think that if the URI was not unique, the already existing > thumb should be returned (and if it weren't unique, then the error about > its non-uniqueness is misleading). > > Have I misunderstood what the doc is trying to say? And, if not, how can > I make find_or_create_alternate do the find_ bit of its job? > > > Cheers, > - -- > Charlie Harvey > IT Manager > New Internationalist > > t: +44 (0)1865 811402 > f: +44 (0)1865 793152 > w: http://www.newint.org/ > k: http://ox4.li/gpgkey/ > > ** Celebrating 40 years of doing the right thing: New Internationalist, > est. 1973** > > *Amnesty Media Award winner 2012* > > New Internationalist is an independent not-for-profit communications > cooperative. Our multi-award winning magazine, New Internationalist, > brings to life the people, the ideas and the action in the fight for > global justice. > > New Internationalist Publications Ltd. is incorporated in England > under no.1005239. Registered Office: > New Internationalist, 55 Rectory Road, Oxford, OX4 1BW, UK > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iQIcBAEBCgAGBQJRyG2BAAoJEPytMqUZaqlzRhgP/i+d1b8idzeTA+0AygRQVgYZ > mgnCtwolVAz3YZiAOuuYzyCO9N/HTReXpTCbs71DnOyAF2UPFq2UsIZSz1c8qP8I > 9tDqn2NuZTVmyTmkN7ECKEk7srDZxky8gSxYmXnlBC7WoPGeyszs+wexJGylQyt6 > IxDLWkv01Q46SqClmZD/oaf7ygIJanlPvdyT0pKWknVpRTgprKS+E1SVGauujXJv > +TKKgqCjkPeRBvunE8z2MUUfJPn/Cjidz99EpQeMvagFFuQaPjnJ2fP1eRsqqbbN > mwZre64GDM3lH/Nb3ag6K2pSoDaiMrET4OtoHOOnd9tJF2GUM3nX1PB5os2zgHJr > Pvue68PxWmNnFGgioKoyNcShUoWlgUQhZdNIvDG4HzplB9o+yBw3Pehsz1KH7tx2 > 4f2J1V+O2D2223YhDWgI0h2yJMt3kOmQFgMnD3MlRcPNmq4KQDDSY7qGE488UBnK > v0dEw5zujJNw/8jjoczlfZSj7N3LlxPD3XtSsL9l71rP9OPJz/Rc0tcTzBAc/YJW > U1KfUf/BcIZVieclCRO6+GUnVnYOhll1zPyKHdFfWgx7IFt2QbPxsV/3YPT5B/8+ > J8dyaHe+LF5ltiAYEBXINveFrCEFtsv6n5KCg39oU0oddtlJdiN6L6mE2UJWET6Q > EM2Nbl/KWfTR01M24eKI > =sHYb > -----END PGP SIGNATURE----- > -- Bret Dawson Producer Pectopah Productions +1-416-895-7635
|