Skip to content

Commit

Permalink
Add user image signature to URL and inform browsers to cache it
Browse files Browse the repository at this point in the history
As URL contains image signature now, it's quite safe to cache it for a long
time, here we cache it for 1 month.
  • Loading branch information
sunnavy authored and cbrandtbuffalo committed Dec 30, 2024
1 parent 92c5801 commit 5b49505
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/RT/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,11 @@ sub Image {
return;
}

sub ImageSignature {
my $self = shift;
return Digest::SHA::sha1_hex($self->_Value('Image'));
}

=head2 Create PARAMHASH
Create takes a hash of values and creates a row in the database:
Expand Down Expand Up @@ -2966,6 +2971,12 @@ Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
=cut

=head2 ImageSignature
Returns the user's image SHA-1 signature.
=cut

=head2 Creator
Returns the current value of Creator.
Expand Down
2 changes: 1 addition & 1 deletion share/html/Elements/ShowUserImage
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ $Class => 'rt-user-image'
</%args>
<%init>
</%init>
<img class="<%$Class%> img-fluid rounded" alt="Image for user <%$Tooltip%>" title="<%$Tooltip%>" src="<% $session{CurrentUser}->Privileged ? '' : '/SelfService' %>/Helpers/UserImage/<%$User->Id%>" loading="lazy" />
<img class="<%$Class%> img-fluid rounded" alt="Image for user <%$Tooltip%>" title="<%$Tooltip%>" src="<% $session{CurrentUser}->Privileged ? '' : '/SelfService' %>/Helpers/UserImage/<%$User->Id%>-<% $User->ImageSignature %>" loading="lazy" />
1 change: 1 addition & 0 deletions share/html/Helpers/UserImage/dhandler
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
%#
%# END BPS TAGGED BLOCK }}}
<%perl>
$m->comp('/Elements/HttpResponseHeaders', MaxAgeSeconds => 24 * 3600 * 30);
my $user_id;
my $arg = $m->dhandler_arg; # get rest of path
if ( $arg =~ m{^(\d+)} ) {
Expand Down
6 changes: 6 additions & 0 deletions t/web/helpers-http-cache-headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ diag "set up expected date headers";
'Cache-Control' => 'max-age=2592000, public',
'Expires' => 'Sun, 05 May 2013 15:28:19 GMT',
},
UserImage => {
'Cache-Control' => 'max-age=2592000, private',
'Expires' => 'Sun, 05 May 2013 15:28:19 GMT',
},
default => {
'Cache-Control' => 'no-cache, max-age=0',
'Expires' => 'Fri, 05 Apr 2013 15:27:49 GMT',
Expand Down Expand Up @@ -143,6 +147,8 @@ foreach my $endpoint ( @endpoints ) {
$header_key = 'Autocomplete';
} elsif ( $endpoint =~ m/NoAuth|static/ ) {
$header_key = 'NoAuth';
} elsif ( $endpoint =~ m/UserImage/ ) {
$header_key = 'UserImage';
}
my $headers = $expected->{$header_key};

Expand Down

0 comments on commit 5b49505

Please sign in to comment.