Skip to content

Commit

Permalink
WIP redirect to new report when uploading photo on front page
Browse files Browse the repository at this point in the history
  • Loading branch information
davea committed Sep 19, 2024
1 parent 65b55d9 commit 8865350
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
16 changes: 16 additions & 0 deletions perllib/FixMyStreet/App/Controller/Photo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ sub upload : Local {
$out = { id => $fileid };
}

if ($c->get_param('get_latlon') && $c->stash->{photo_gps}) {
$out = {
%$out,
%{ $c->stash->{photo_gps} },
};
}

if ($c->get_param('start_report') && $c->stash->{photo_gps}) {
my $url = $c->uri_for( "/report/new", {
lat => $c->stash->{photo_gps}->{lat},
lon => $c->stash->{photo_gps}->{lon},
photo_id => $fileid,
} );
return $c->res->redirect($url);
}

$c->res->content_type('application/json; charset=utf-8');
$c->res->body(encode_json($out));
}
Expand Down
4 changes: 4 additions & 0 deletions perllib/FixMyStreet/App/Controller/Report/New.pm
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ sub initialize_report : Private {
$report = $c->model('DB::Problem')->new( {} );
}

if (!$c->stash->{upload_fileid} && $c->get_param('photo_id')) {
$c->stash->{upload_fileid} = $c->get_param('photo_id');
}

# If we have a user logged in let's prefill some values for them.
if (!$report->user && $c->user) {
my $user = $c->user->obj;
Expand Down
30 changes: 30 additions & 0 deletions perllib/FixMyStreet/App/Model/PhotoSet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ has ids => ( # Arrayref of $fileid tuples (always, so post upload/raw data proc
return ();
}

if ($type eq 'jpeg' && !$self->c->stash->{photo_gps}) {
# only store GPS for the first uploaded photo
$self->stash_gps_info($upload->tempname);
}

# Convert all images to JPEGs
my %params = ( magick => 'JPEG' );

Expand Down Expand Up @@ -206,6 +211,31 @@ has ids => ( # Arrayref of $fileid tuples (always, so post upload/raw data proc
},
);

sub stash_gps_info {
my ($self, $filename) = @_;

return unless can_run('jhead');

eval {
# run jhead on $filename and store in $stdout
my $stdout;
my $pid = open3(undef, $stdout, undef, 'jhead', $filename);
# parse lines like "GPS Latitude : N 51d 36m 52.32s
# GPS Longitude: W 0d 42m 27.24s"
my ($lat, $lon);
while (<$stdout>) {
if (/GPS Latitude : ([NS])\s+(\d+)d\s+(\d+)m\s+(\d+\.\d+)s/) {
$lat = $2 + $3/60 + $4/3600;
$lat = -$lat if $1 eq 'S';
} elsif (/GPS Longitude: ([EW])\s+(\d+)d\s+(\d+)m\s+(\d+\.\d+)s/) {
$lon = $2 + $3/60 + $4/3600;
$lon = -$lon if $1 eq 'W';
}
}
$self->c->stash->{photo_gps} = { lat => $lat, lon => $lon };
};
}

sub get_image_type {
my ($self, $index) = @_;
my $filename = $self->get_id($index);
Expand Down
8 changes: 8 additions & 0 deletions templates/web/base/around/postcode_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<p>To continue draft<span class="js-draft-name"></span> enter its location.<br>
<a href="/" class="btn">Cancel</a>
</div>
<form action="[% c.uri_for('/photo/upload') %]" method="post" name="photoForm" id="photoForm" class="postcode-form-box js-geolocate" enctype="multipart/form-data">
<label for="photo">[% loc('Upload a photo') %]:</label>
<input type="file" name="photo" id="photo" accept="image/*" required>
<input type="hidden" name="lat">
<input type="hidden" name="lon">
<input type="hidden" name="start_report" value="1">
<input type="submit" value="[% loc('Start report from photo') %]" class="btn btn--final">
</form>
<form action="[% c.uri_for('/around') %]" method="get" name="postcodeForm" id="postcodeForm" class="postcode-form-box js-geolocate">
<label for="pc">[% question %]:</label>
[% INCLUDE 'around/_postcode_form_examples.html' %]
Expand Down
8 changes: 4 additions & 4 deletions web/cobrands/fixmystreet/fixmystreet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,15 +1017,15 @@ $.extend(fixmystreet.set_up, {
}
var prevFile;
var photodrop = new Dropzone($dropzone[0], {
url: '/photo/upload',
url: '/photo/upload?get_latlon=1',
paramName: 'photo',
maxFiles: max_photos,
addRemoveLinks: true,
thumbnailHeight: 150,
thumbnailWidth: 150,
resizeWidth: 2048,
resizeHeight: 2048,
resizeQuality: 0.6,
// resizeWidth: 2048,
// resizeHeight: 2048,
// resizeQuality: 0.6,
acceptedFiles: 'image/jpeg,image/pjpeg,image/gif,image/tiff,image/png,.png,.tiff,.tif,.gif,.jpeg,.jpg',
dictDefaultMessage: default_message,
dictCancelUploadConfirmation: translation_strings.upload_cancel_confirmation,
Expand Down

0 comments on commit 8865350

Please sign in to comment.