Unverified Commit dbb2b493 authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Copy screenshot updated to db (#1234)

* fix(frontend): copy screenshot updated to db

necessary because of @eskumon changes

closes issue #1221
parent d855c6b8
......@@ -2533,16 +2533,10 @@ sub _cmd_copy_screenshot {
my $id_base = $domain->id_base;
my $base = $self->search_domain_by_id($id_base);
if (!$domain->file_screenshot) {
if (!$domain->screenshot) {
die "I don't have the screenshot of the domain ".$domain->name;
} else {
my $base_screenshot = $domain->file_screenshot();
$base_screenshot =~ s{(.*)/\d+\.(\w+)}{$1/$id_base.$2};
$base->_post_screenshot($base_screenshot);
copy($domain->file_screenshot, $base_screenshot);
$base->_data(screenshot => $domain->_data('screenshot'));
}
}
......
......@@ -460,12 +460,11 @@ sub list_volumes_info($self, $attribute=undef, $value=undef) {
sub screenshot {
my $self = shift;
my $DPI = 300; # 600;
my $image = Image::Magick->new(density => $DPI,width=>100, height=>100);
$image = Image::Magick->new;
$image->Set(size=>'100x100');
$image->ReadImage('canvas:white');
$image->Set('pixel[49,49]'=>'red');
$self->_data(screenshot => encode_base64($image));
my $image = Image::Magick->new(density => $DPI,width=>250, height=>188);
$image->Set(size=>'250x188');
$image->ReadImage('canvas:#'.int(rand(10)).int(rand(10)).int(rand(10)));
my @blobs = $image->ImageToBlob(magick => 'png');
$self->_data(screenshot => encode_base64($blobs[0]));
}
sub _file_screenshot {
......
......@@ -161,10 +161,12 @@ sub list_machines_user($self, $user, $access_data={}) {
if ($clone) {
$base{is_locked} = $clone->is_locked;
if ($clone->is_active && !$clone->is_locked && $user->can_screenshot) {
my $req = Ravada::Request->screenshot_domain(
id_domain => $clone->id
,filename => "$DIR_SCREENSHOTS/".$clone->id.".png"
);
if (!Ravada::Request::done_recently(undef,10,'screenshot')) {
my $req = Ravada::Request->screenshot(
id_domain => $clone->id
,_no_duplicate => 1
);
}
}
$base{name_clone} = $clone->name;
$base{screenshot} = ( $clone->_data('screenshot')
......
......@@ -65,9 +65,9 @@ our %VALID_ARG = (
,shutdown_domain => { name => 2, id_domain => 2, uid => 1, timeout => 2, at => 2
, id_vm => 2 }
,force_shutdown_domain => { id_domain => 1, uid => 1, at => 2, id_vm => 2 }
,screenshot_domain => { id_domain => 1, filename => 2 }
,screenshot => { id_domain => 1 }
,domain_autostart => { id_domain => 1 , uid => 1, value => 2 }
,copy_screenshot => { id_domain => 1, filename => 2 }
,copy_screenshot => { id_domain => 1 }
,start_domain => {%$args_manage, remote_ip => 2, name => 2, id_domain => 2 }
,start_clones => { id_domain => 1, uid => 1, remote_ip => 1 }
,rename_domain => { uid => 1, name => 1, id_domain => 1}
......@@ -153,6 +153,7 @@ our %COMMAND = (
,disk => {
limit => 1
,commands => ['prepare_base','remove_base','set_base_vm','rebase_volumes'
, 'screenshot'
, 'manage_pools']
,priority => 6
}
......@@ -418,7 +419,7 @@ sub _check_args {
my $args = { @_ };
my $valid_args = $VALID_ARG{$sub};
for (qw(at after_request retry)) {
for (qw(at after_request retry _no_duplicate)) {
$valid_args->{$_}=2 if !exists $valid_args->{$_};
}
......@@ -534,6 +535,7 @@ sub _new_request {
$args{domain_name} = $args{name};
delete $args{name};
}
my $no_duplicate = delete $args{_no_duplicate};
my $uid;
if ( ref $args{args} ) {
$args{args}->{uid} = $args{args}->{id_owner}
......@@ -556,7 +558,8 @@ sub _new_request {
$args{args} = encode_json($args{args});
}
_init_connector() if !$CONNECTOR || !$$CONNECTOR;
if ($args{command} =~ /^(clone|manage_pools|list_isos)$/) {
if ($args{command} =~ /^(clone|manage_pools|list_isos)$/
|| ($no_duplicate && $args{command} =~ /^(screenshot)$/)) {
if ( _duplicated_request($args{command}, $args{args})
|| ( $args{command} ne 'clone' && done_recently(undef, 60, $args{command}))) {
# warn "Warning: duplicated request for $args{command} $args{args}";
......@@ -845,34 +848,6 @@ sub defined_arg {
return $self->{args}->{$name};
}
=head2 screenshot_domain
Request the screenshot of a domain.
Arguments:
- optional filename , defaults to "storage_path/$id_domain.png"
Returns a Ravada::Request;
=cut
sub screenshot_domain {
my $proto = shift;
my $class=ref($proto) || $proto;
my $args = _check_args('screenshot_domain', @_ );
$args->{filename} = '' if !exists $args->{filename};
my $self = {};
bless($self,$class);
return $self->_new_request(command => 'screenshot' , id_domain => $args->{id_domain}
,args => $args);
}
=head2 copy_screenshot
Request to copy a screenshot from a domain to another
......@@ -885,8 +860,6 @@ sub copy_screenshot {
my $args = _check_args('copy_screenshot', @_ );
$args->{filename} = '' if !exists $args->{filename};
my $self = {};
bless($self,$class);
......
......@@ -289,37 +289,24 @@
$scope.reload_page_msg = false;
$scope.fail_page_msg = false;
$scope.screenshot = function(machineId, isActive){
if (isActive) {
$http.get('/machine/screenshot/'+machineId+'.json');
$scope.fail_page_msg = false;
$scope.screenshot = function(machineId) {
$scope.reload_page_msg = true;
setTimeout(function () {
window.location.reload(false);
}, 5000);
}
else {
$scope.reload_page_msg = false;
$scope.fail_page_msg = true;
}
$scope.reload_page_msg = false;
}, 2000);
$http.get('/machine/screenshot/'+machineId+'.json');
};
$scope.reload_page_copy_msg = false;
$scope.fail_page_copy_msg = false;
$scope.copy_done = false;
$scope.copy_screenshot = function(machineId, fileScreenshot){
if (fileScreenshot != '') {
$http.get('/machine/copy_screenshot/'+machineId+'.json');
$scope.fail_page_copy_msg = false;
$scope.copy_screenshot = function(machineId){
$scope.reload_page_msg = false;
$scope.reload_page_copy_msg = true;
setTimeout(function () {
$scope.reload_page_copy_msg = false;
}, 2000);
}
else {
$scope.reload_page_copy_msg = false;
$scope.fail_page_copy_msg = true;
}
$http.get('/machine/copy_screenshot/'+machineId+'.json');
};
subscribe_request = function(id_request, action) {
......@@ -689,7 +676,6 @@
$scope.pending_before = 10;
// $scope.getSingleMachine();
// $scope.updatePromise = $interval($scope.getSingleMachine,3000);
list_nodes();
$scope.access_attribute = [ ];
$scope.access_value = [ ];
$scope.access_allowed = [ ];
......
......@@ -2169,10 +2169,8 @@ sub screenshot_machine {
my $domain = _search_requested_machine($c);
my $file_screenshot = "$DOCUMENT_ROOT/img/screenshots/".$domain->id.".png";
my $req = Ravada::Request->screenshot_domain (
my $req = Ravada::Request->screenshot(
id_domain => $domain->id
,filename => $file_screenshot
);
$c->render(json => { request => $req->id});
}
......@@ -2183,10 +2181,8 @@ sub copy_screenshot {
my $domain = _search_requested_machine($c);
my $file_screenshot = "$DOCUMENT_ROOT/img/screenshots/".$domain->id.".png";
my $req = Ravada::Request->copy_screenshot (
id_domain => $domain->id
,filename => $file_screenshot
);
$c->render(json => { request => $req->id});
}
......@@ -2224,12 +2220,10 @@ sub prepare_machine {
return $c->render(json => { error => "Domain ".$domain->name." is locked" })
if $domain->is_locked();
my $file_screenshot = "$DOCUMENT_ROOT/img/screenshots/".$domain->id.".png";
if (! -e $file_screenshot && $domain->can_screenshot()
if (! $domain->_data('screenshot') && $domain->can_screenshot()
&& $domain->is_active) {
Ravada::Request->screenshot_domain (
Ravada::Request->screenshot(
id_domain => $domain->id
,filename => $file_screenshot
);
}
......
......@@ -7,30 +7,31 @@
<div class="form-group">
<div ng-hide= "<%= $domain->is_base %>" class="row" ng-cloak>
<div class="col-lg-4">
<button class="btn btn-success" ng-click="screenshot(<%= $domain->id %>, <%= $domain->is_active %>)">
<button class="btn btn-success" ng-click="screenshot(<%= $domain->id %>)"
ng-disabled="!showmachine.is_active"
>
<i class="far fa-image"></i>&nbsp;<%=l "Take screenshot" %></button>
</div>
% if ( $USER->is_admin && (defined $domain->id_base())) {
<div class="col-lg-4">
<button class="btn btn-primary" ng-click="copy_screenshot(<%= $domain->id %>, '<%= $domain->file_screenshot %>')">
<button class="btn btn-primary" ng-click="copy_screenshot(<%= $domain->id %>)">
<i class="fa fa-sign-in-alt" aria-hidden="true"></i>&nbsp;<%=l 'Set base picture' %></button>
</div>
% }
<div class="col-lg-12">
<i ng-show="reload_page_msg" ng-cloak><font color="green"><%=l 'Saving the screenshot, please wait a moment...' %></font></i>
<i ng-show="fail_page_msg" ng-cloak><font color="red"><%=l 'You need to run the machine in order to take a screenshot.' %></font></i>
<i ng-show="!showmachine.is_active" ng-cloak><font color="red"><%=l 'You need to run the machine in order to take a screenshot.' %></font></i>
<i ng-show="reload_page_copy_msg" ng-cloak><font color="green"><%=l 'Copying the screenshot, please wait a moment...' %></font></i>
<i ng-show="fail_page_copy_msg" ng-cloak><font color="red"><%=l 'You need a screenshot in order to copy it to the base machine.' %></font></i>
<i ng-show="!showmachine.screenshot" ng-cloak><font color="red"><%=l 'You need a screenshot in order to copy it to the base machine.' %></font></i>
</div>
</div>
<div class="row">
<div class="col-lg-6 mt-3">
<div class="card">
<div class="card-header">
% my $screenshot;
% ($screenshot) = $domain->file_screenshot =~ m{/var/www(.*)}
% if defined $domain->file_screenshot;
<img src="<%= $screenshot %>" width="260">
<img
ng-src="data:image/png;base64,{{showmachine.screenshot}}" alt="{{showmachine.description}}" class="img-thumbnail" width="260"
>
</div>
</div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment