Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Photo-first reporting #5176

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
$out = { id => $fileid };
}

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

Check warning on line 139 in perllib/FixMyStreet/App/Controller/Photo.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Photo.pm#L139

Added line #L139 was not covered by tests
};
}

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,

Check warning on line 147 in perllib/FixMyStreet/App/Controller/Photo.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Photo.pm#L147

Added line #L147 was not covered by tests
} );
return $c->res->redirect($url);

Check warning on line 149 in perllib/FixMyStreet/App/Controller/Photo.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Photo.pm#L149

Added line #L149 was not covered by tests
}

$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 @@
$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');

Check warning on line 640 in perllib/FixMyStreet/App/Controller/Report/New.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Report/New.pm#L640

Added line #L640 was not covered by tests
}

# 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 @@
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 @@
},
);

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

return unless can_run('jhead');

eval {

Check warning on line 219 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L219

Added line #L219 was not covered by tests
# run jhead on $filename and store in $stdout
my $stdout;
my $pid = open3(undef, $stdout, undef, 'jhead', $filename);

Check warning on line 222 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L221-L222

Added lines #L221 - L222 were not covered by tests
# parse lines like "GPS Latitude : N 51d 36m 52.32s
# GPS Longitude: W 0d 42m 27.24s"
my ($lat, $lon);
while (<$stdout>) {

Check warning on line 226 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L225-L226

Added lines #L225 - L226 were not covered by tests
if (/GPS Latitude : ([NS])\s+(\d+)d\s+(\d+)m\s+(\d+\.\d+)s/) {
$lat = $2 + $3/60 + $4/3600;

Check warning on line 228 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L228

Added line #L228 was not covered by tests
$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;

Check warning on line 231 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L231

Added line #L231 was not covered by tests
$lon = -$lon if $1 eq 'W';
}
}
$self->c->stash->{photo_gps} = { lat => $lat, lon => $lon };

Check warning on line 235 in perllib/FixMyStreet/App/Model/PhotoSet.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Model/PhotoSet.pm#L235

Added line #L235 was not covered by tests
};
}

sub get_image_type {
my ($self, $index) = @_;
my $filename = $self->get_id($index);
Expand Down
11 changes: 11 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,17 @@
<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="js-geolocate" enctype="multipart/form-data">
<div id="photoFormPhoto" class="dropzone" >
<label for="photo">[% loc('Upload a photo') %]:</label>
<input type="file" name="photo" id="photo" accept="image/*" required>
</div>
<input type="hidden" name="lat">
<input type="hidden" name="lon">
<input type="hidden" name="start_report" value="1">
<input type="hidden" name="photo_id">
<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
2 changes: 2 additions & 0 deletions templates/web/base/common_scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
IF bodyclass.match('frontpage');
scripts.push(
version('/vendor/idb-keyval-iife.min.js'),
version('/vendor/jquery-3.6.0.min.js'),
version('/vendor/dropzone.min.js'),
version('/js/offline_draft.js'),
version('/js/front.js'),
version('/js/geolocation.js'),
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
5 changes: 5 additions & 0 deletions web/cobrands/sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3403,3 +3403,8 @@ $site-message-border: 1px solid #525252 !default;

@import "govuk-frontend/forms";
@import "_govuk"; // Adds some overrides to the default GOVUK behaviour

#front-main .dropzone {
background-color: rgba(255, 190, 0, 0.88);
border-color: #a28400;
}
46 changes: 45 additions & 1 deletion web/js/front.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
document.getElementById('pc').focus();
// document.getElementById('pc').focus();

(function(){

function dropzoneSetup() {
console.log('dropzoneSetup');
if ('Dropzone' in window) {
Dropzone.autoDiscover = false;
console.log('Dropzone', Dropzone);
} else {
console.log('Dropzone not found');
return;
}

var dz = new Dropzone('#photoFormPhoto', {
url: '/photo/upload?get_latlon=1',
paramName: 'photo',
maxFiles: 1,
addRemoveLinks: true,
thumbnailHeight: 256,
thumbnailWidth: 256,
// resizeHeight: 2048,
// resizeWidth: 2048,
// resizeQuality: 0.6,
acceptedFiles: 'image/jpeg,image/pjpeg,image/gif,image/tiff,image/png,.png,.tiff,.tif,.gif,.jpeg,.jpg',
dictDefaultMessage: "Upload a photo to start a new report",
// dictCancelUploadConfirmation: translation_strings.upload_cancel_confirmation,
// dictInvalidFileType: translation_strings.upload_invalid_file_type,
// dictMaxFilesExceeded: translation_strings.upload_max_files_exceeded,
init: function() {
console.log('init', this);
var $f = $("#photoForm");
$("#photoForm label, #photoForm input[type=file], #photoForm input[type=submit]").hide();
$f.attr("method", "get");
$f.attr("action", "/report/new");
$f.attr("enctype", "");
this.on("success", function(file, xhrResponse) {
console.log('success', file, xhrResponse);
$("#photoForm label, #photoForm input[type=file], #photoForm input[type=submit]").remove();
$f.find("input[name=photo_id]").val(xhrResponse.id);
$f.find("input[name=lat]").val(xhrResponse.lat);
$f.find("input[name=lon]").val(xhrResponse.lon);
$f.submit();
});
}
});
}
dropzoneSetup();
function set_up_mobile_nav() {
var html = document.documentElement;
if (!html.classList) {
Expand Down
Loading