aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Roles/PhotoSet.pm
blob: 27a63bad55adac9b3f2b93b3bd4314949ba73383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package FixMyStreet::Roles::PhotoSet;
use Moo::Role;

=head1 NAME

FixMyStreet::Roles::Photoset - role for accessing photosets

=cut

=head2 get_photoset

Return a PhotoSet object for all photos attached to this field

    my $photoset = $obj->get_photoset;
    print $photoset->num_images;
    return $photoset->get_image_data(num => 0, size => 'full');

=cut

sub get_photoset {
    my ($self) = @_;
    require FixMyStreet::App::Model::PhotoSet;
    return FixMyStreet::App::Model::PhotoSet->new({
        db_data => $self->photo,
        object => $self,
    });
}

sub get_first_image_fp {
    my ($self) = @_;
    return $self->get_photoset->get_image_data( num => 0, size => 'fp' );
}

sub photos {
    my $self = shift;
    my $photoset = $self->get_photoset;
    my $i = 0;
    my $id = $self->id;
    my $typ = $self->result_source->name eq 'comment' ? 'c/' : '';

    my $non_public = $self->result_source->name eq 'comment'
        ? $self->problem->non_public : $self->non_public;

    my @photos = map {
        my $cachebust = substr($_, 0, 8);
        # Some Varnish configurations (e.g. on mySociety infra) strip cookies from
        # images, which means image requests will be redirected to the login page
        # if e.g. LOGIN_REQUIRED is set. To stop this happening, Varnish should be
        # configured to not strip cookies if the cookie_passthrough param is
        # present, which this line ensures will be if LOGIN_REQUIRED is set.
        my $extra = '';
        if (FixMyStreet->config('LOGIN_REQUIRED') || $non_public) {
            $cachebust .= '&cookie_passthrough=1';
            $extra = '?cookie_passthrough=1';
        }
        my ($hash, $format) = split /\./, $_;
        {
            id => $hash,
            url_temp => "/photo/temp.$hash.$format$extra",
            url_temp_full => "/photo/fulltemp.$hash.$format$extra",
            url => "/photo/$typ$id.$i.$format?$cachebust",
            url_full => "/photo/$typ$id.$i.full.$format?$cachebust",
            url_tn => "/photo/$typ$id.$i.tn.$format?$cachebust",
            url_fp => "/photo/$typ$id.$i.fp.$format?$cachebust",
            idx => $i++,
        }
    } $photoset->all_ids;
    return \@photos;
}

1;