Skip to content

Commit

Permalink
Fix character encoding for OCR
Browse files Browse the repository at this point in the history
The addition of the OCR feature revealed character encoding bugs that
might have been invisible previously. This commit fixes the character
encoding in multiple places to ensure the correct displaying of special
characters in the web frontend.
  • Loading branch information
clanig committed Sep 14, 2023
1 parent c8103e1 commit 3a4e3e1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/OpenQA/Schema/Result/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ sub append_log ($self, $log, $file_name) {
my $path = $self->worker->get_property('WORKER_TMPDIR');
return unless -d $path; # we can't help
$path .= "/$file_name";
if (open(my $fd, '>>', $path)) {
if (open(my $fd, '>>:utf8', $path)) {
print $fd $log->{data};
close($fd);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/OpenQA/WebAPI/Controller/Running.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ sub streamtext ($self, $file_name, $start_hook = undef, $close_hook = undef) {
# if the open fails, continue, well check later
my $log;
my ($ino, $size);
if (open($log, '<', $logfile)) {
if (open($log, '<:utf8', $logfile)) {
# Send the last 10KB of data from the logfile, so that
# the client sees some data immediately
$ino = (stat $logfile)[1];
Expand Down Expand Up @@ -122,7 +122,7 @@ sub streamtext ($self, $file_name, $start_hook = undef, $close_hook = undef) {
TEXT_STREAMING_INTERVAL() => sub {
if (!$ino) {
# log file was not yet opened
return unless open($log, '<', $logfile);
return unless open($log, '<:utf8', $logfile);
$ino = (stat $logfile)[1];
$size = -s $logfile;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/OpenQA/Worker/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use Try::Tiny;
use Scalar::Util 'looks_like_number';
use File::Map 'map_file';
use List::Util 'max';
use Encode 'decode';

# define attributes for public properties
has 'worker';
Expand Down Expand Up @@ -1238,7 +1239,7 @@ sub _log_snippet {
sysseek($fd, $offset, Fcntl::SEEK_SET); # FIXME: handle error?
if (defined sysread($fd, my $buf = '', 100000)) {
$ret{offset} = $offset;
$ret{data} = $buf;
$ret{data} = decode('utf-8', $buf);
}
if (my $new_offset = sysseek($fd, 0, Fcntl::SEEK_CUR)) {
$self->{"_$offset_name"} = $new_offset;
Expand Down
6 changes: 3 additions & 3 deletions templates/webapi/step/viewimg.html.ep
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% use Mojo::JSON 'encode_json';
% use Mojo::JSON 'to_json';
<div class="aligncenter" data-image="<%= url_for('test_img', filename => $screenshot) %>" id="step_view">
<span class="candidate_needles">
Candidate needles and tags:
Expand Down Expand Up @@ -37,8 +37,8 @@
<tbody>
% for my $needle (@{$needles_by_tag->{$tag}}) {
<tr data-image="<%= $needle->{image} %>"
data-areas="<%= encode_json($needle->{areas})%>"
data-matches="<%= encode_json($needle->{matches}) %>"
data-areas="<%= to_json($needle->{areas}) %>"
data-matches="<%= to_json($needle->{matches}) %>"
data-label="<%= $needle->{label} %>"
% if (!$has_selection && $needle->{selected}) {
% $has_selection = 1;
Expand Down

0 comments on commit 3a4e3e1

Please sign in to comment.