Mailing List Archive

find_or_create_alternate apparently not finding an alternate it has already created
-----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-----
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
Re: find_or_create_alternate apparently not finding an alternate it has already created [ In reply to ]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 24/06/13 19:25, Bret Dawson wrote:
- -----------------8<---------------
> Specifically, the issue appears when media filenames have mixed case
> names, and expecially when they contain characters such as spaces and
> parentheses.
- -----------------8<---------------
> ... 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.
- -----------------8<---------------

Hi Bret,

Thanks, super helpful to know that others have hit this. The pictures do
have indeed have spaces (and sometimes %20s) in their names, so I guess
it is that. Will have a go at creating some test cases.

I ended up wrapping the find_or_create_alternate in an unless block that
looks for anything with the same title_prefix . title and returns that
if it finds anything.

Cheers,



>
> On 06/24/2013 12:02 PM, Charlie Harvey wrote:
> 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/

iQIcBAEBCgAGBQJRyvSJAAoJEPytMqUZaqlzUPUP/2WoTBa4rU66tGwyF5hFfS/k
OYAp4yNyRwdNGLVakISpJHIKUa8z7NCt45KXAd9w2tTPi3T82UIpSKKpKLTKbhXX
vc8XMc398eDNetbaGicd516FGKM5+OFesaHwDe3tvSCNqJzeTjY8ms9pmuUlRf2V
fYRgRjvPlYrh+rpJ+IAbLlW1kSsyQOX3/d/oDTIDMdccUfsp7DdDvSfOwBQdVb+o
ucySztAOBxE49R5WB1NHdSVy34caDubtkJdpu7tzZ3nmm9Ntz7A9+4pg3EzdDGZ6
81rLnfDJoc1ucIa8Yv/WfAuqmpF/muebHUP1C/pxgJ5w7Q6dsPnWMiiuZ99fgzm3
LN9Giutgr48X4K/xzAlu4qs480GP07T8W7/59GtZHH65VftVy9G5ojwCKbaikoFp
3iygk41R19dNaoVJW2ELtPBggwgaSeePdNK4m/kR8yZHcYs/hJliVIwafqDUiZiz
k17VOty84Tl+Ph/pru3VBtAbwOelmggXFdbMIM1SV58mLLmlTfjfQK8wDbT6A5wb
RdDYs6gKFgzIpvkrOnfK23QZPfaZR5G28EqwYUFb7J+x+bVQvyvSvrcwVuH+Gd11
Srpi9kemRs+rRJcHVM5vnyxlCvDpsk632zPLSaToeVAt1X5CIx0Is4rz1cn4GjtZ
qdG1OZW4iCStKUW+JiMJ
=OQe8
-----END PGP SIGNATURE-----
Re: find_or_create_alternate apparently not finding an alternate it has already created [ In reply to ]
On 2013-06-26, at 9:02 AM, Charlie Harvey <charlie@newint.org> wrote:

> On 24/06/13 19:25, Bret Dawson wrote:
> - -----------------8<---------------
> > Specifically, the issue appears when media filenames have mixed case
> > names, and expecially when they contain characters such as spaces and
> > parentheses.
> - -----------------8<---------------
> > ... 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.
> - -----------------8<---------------
>
> Hi Bret,
>
> Thanks, super helpful to know that others have hit this. The pictures do
> have indeed have spaces (and sometimes %20s) in their names, so I guess
> it is that. Will have a go at creating some test cases.
>
> I ended up wrapping the find_or_create_alternate in an unless block that
> looks for anything with the same title_prefix . title and returns that
> if it finds anything.


Care to share? ;)

--
Phillip Smith
http://phillipadsmith.com
Re: find_or_create_alternate apparently not finding an alternate it has already created [ In reply to ]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 03/07/13 00:09, Phillip Smith wrote:
>
> On 2013-06-26, at 9:02 AM, Charlie Harvey <charlie@newint.org> wrote:
>
>> On 24/06/13 19:25, Bret Dawson wrote:
>> - -----------------8<---------------
>>> Specifically, the issue appears when media filenames have mixed case
>>> names, and expecially when they contain characters such as spaces and
>>> parentheses.
>> - -----------------8<---------------
>>> ... 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.
>> - -----------------8<---------------
>>
>> Hi Bret,
>>
>> Thanks, super helpful to know that others have hit this. The pictures do
>> have indeed have spaces (and sometimes %20s) in their names, so I guess
>> it is that. Will have a go at creating some test cases.
>>
>> I ended up wrapping the find_or_create_alternate in an unless block that
>> looks for anything with the same title_prefix . title and returns that
>> if it finds anything.
>
>
> Care to share? ;)

Hi,

Sure. This is my whole template now. Obviously it can break when titles
are the same.

<%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 =~ /$_$/i} qw(jpg jpeg png
gif)); # sometimes videos are images.

# find_or_create_alternate doesn't find our images. So we find them
ourselves
# and /then/ call find or create. TODO: could this be to do with spaces
in filenames and URL encoding?
my $thumb_pretest =
Bric::Biz::Asset::Business::Media->list({'title'=>$prefix .
$for->get_title});
my $thumb;

$thumb = $thumb_pretest->[0] if($thumb_pretest);

unless($thumb) {
$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 really interesting went wrong.\nHere's the
debug info: \nThumb==" . Dumper $thumb "\t";
$burner->throw_error(ref $@ ? $e . $@->error : $e . $@);
}
}

if($thumb) {
$burner->blaze_another($thumb, $for->get_cover_date, 1) unless
$thumb->get_publish_status;
}
return $thumb;

</%perl>

Cheers,



> --
> Phillip Smith
> http://phillipadsmith.com
>


- --
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/

iQIcBAEBCgAGBQJR1AOvAAoJEPytMqUZaqlzNZUQAIkVrYqwXgjBbkeofkkWSOxH
Uvswl2xrvbYe0e54WfvL8VuDUT1elBWqlKDF/8N6tF0oqz/arMfEcH8lJFwonZtn
hflP1//EgQUDCliLZC2Wzq4Esorb+LgIJ36nayKrf5BS91Gy2kQ0aUsIGxSTVhQU
lh55gv5BMQ3S95hKho5JOyzE0Jcnk5vnhoBWudTOvQCMtskNODxE+OPIe6j9C5TE
T3CZN2yPGDy/B3m6N3HFyeIjESzoyRiqzcw69A9/tXv3yItvu0P8TW2K+hxljb6o
W+TtEQc/p5r18I3RGFRzkxnGgoW2lfKo8xYlbAlqudrwXLAlHiuEsfagSI2MKvUR
PM4Ul9alkv+fjXndNJM0MwMzCEgBM0SV/duv+nSQ8xZZoK7NZLl3/0aI5Uk054pk
7fEQhBe8/rj6mA0HkuS2BKRXtzE8BHtUV+0fkKYWb1PBEsEKYY3ma4NpQ61YPhBt
Zv7wWKXhnCGiaStYB4wUaUkCvMb3ysLKRJvYuuyXOKBjwKdSnkjLkzYpn+nG+MLo
3Qlcf3LH0RxmM1ocnh8AOwct19B1KS5rm5CkHNR/6XfnYB6bLU4oAUJicXDWa9US
EPa8LtGyozdcwoq05P1SjMsvFGn1AGkNUf4K/RU+pQD+44inw8v8DqeFrDevYKtd
yxr49JM0mOT5VNDDEfUu
=1vlU
-----END PGP SIGNATURE-----
Re: find_or_create_alternate apparently not finding an alternate it has already created [ In reply to ]
On 2013-07-03, at 5:57 AM, Charlie Harvey <charlie@newint.org> wrote:

> On 03/07/13 00:09, Phillip Smith wrote:
> >
> > On 2013-06-26, at 9:02 AM, Charlie Harvey <charlie@newint.org> wrote:
> >
> >> On 24/06/13 19:25, Bret Dawson wrote:
> >> - -----------------8<---------------
> >>> Specifically, the issue appears when media filenames have mixed case
> >>> names, and expecially when they contain characters such as spaces and
> >>> parentheses.
> >> - -----------------8<---------------
> >>> ... 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.
> >> - -----------------8<---------------
> >>
> >> Hi Bret,
> >>
> >> Thanks, super helpful to know that others have hit this. The pictures do
> >> have indeed have spaces (and sometimes %20s) in their names, so I guess
> >> it is that. Will have a go at creating some test cases.
> >>
> >> I ended up wrapping the find_or_create_alternate in an unless block that
> >> looks for anything with the same title_prefix . title and returns that
> >> if it finds anything.
> >
> >
> > Care to share? ;)
>
> Hi,
>
> Sure. This is my whole template now. Obviously it can break when titles
> are the same.

Thanks! :)


>
> <%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 =~ /$_$/i} qw(jpg jpeg png
> gif)); # sometimes videos are images.
>
> # find_or_create_alternate doesn't find our images. So we find them
> ourselves
> # and /then/ call find or create. TODO: could this be to do with spaces
> in filenames and URL encoding?
> my $thumb_pretest =
> Bric::Biz::Asset::Business::Media->list({'title'=>$prefix .
> $for->get_title});
> my $thumb;
>
> $thumb = $thumb_pretest->[0] if($thumb_pretest);
>
> unless($thumb) {
> $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 really interesting went wrong.\nHere's the
> debug info: \nThumb==" . Dumper $thumb "\t";
> $burner->throw_error(ref $@ ? $e . $@->error : $e . $@);
> }
> }
>
> if($thumb) {
> $burner->blaze_another($thumb, $for->get_cover_date, 1) unless
> $thumb->get_publish_status;
> }
> return $thumb;
>
> </%perl>
>
> Cheers,
>
>
>
> > --
> > Phillip Smith
> > http://phillipadsmith.com
> >
>
>
> - --
> 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
>

--
Phillip Smith
http://phillipadsmith.com
http://twitter.com/phillipadsmith
http://linkedin.com/in/phillipadsmith

Oaxaca: (011 52 1) 951 154 4620
Toronto: (647) 361-8248
Toll-free: (888) 418-1868

If your email inbox is out of control, check out http://sanebox.com/t/s0q7m

Save our in-boxes! http://emailcharter.org