]> git.proxmox.com Git - pmg-api.git/commitdiff
PMG/API2/Backup.pm - allow to download backup files
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 9 Nov 2017 06:23:39 +0000 (07:23 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 9 Nov 2017 06:23:39 +0000 (07:23 +0100)
PMG/API2/Backup.pm
PMG/HTTPServer.pm

index f91d23a7f54b09af71cf9396f2c378b7e82dafeb..8fab71e2980cf4179d6b379a619b4dbe1f37d0e7 100644 (file)
@@ -42,7 +42,7 @@ __PACKAGE__->register_method ({
            node => get_standard_option('pve-node'),
        },
     },
-   returns => {
+    returns => {
        type => "array",
        items => {
            type => "object",
@@ -58,6 +58,7 @@ __PACKAGE__->register_method ({
                },
            },
        },
+       links => [ { rel => 'child', href => "{filename}" } ],
     },
     code => sub {
        my ($param) = @_;
@@ -104,7 +105,7 @@ __PACKAGE__->register_method ({
            statistic => $include_statistic_property,
        },
     },
-   returns => { type => "string" },
+    returns => { type => "string" },
     code => sub {
        my ($param) = @_;
 
@@ -130,6 +131,31 @@ __PACKAGE__->register_method ({
        return $rpcenv->fork_worker('backup', undef, $authuser, $worker);
     }});
 
+__PACKAGE__->register_method ({
+    name => 'download',
+    path => '{filename}',
+    method => 'GET',
+    description => "Download a backup file.",
+    permissions => { check => [ 'admin' ] },
+    proxyto => 'node',
+    protected => 1,
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           filename => $backup_filename_property,
+       },
+    },
+    download => 1,
+    returns => { type => "string" },
+    code => sub {
+       my ($param) = @_;
+
+       my $filename = "${backup_dir}/$param->{filename}";
+
+       return $filename;
+    }});
+
 __PACKAGE__->register_method ({
     name => 'delete',
     path => '{filename}',
index 73e24b181a2965c567506d2a4a1c37689122670b..6997dd5cc704da27b382b2b5dab169a1bd0360c3 100755 (executable)
@@ -27,7 +27,7 @@ sub new {
     my $class = ref($this) || $this;
 
     my $self = $class->SUPER::new(%args);
-    
+
     $self->{rpcenv} = PMG::RESTEnvironment->init(
        $self->{trusted_env} ? 'priv' : 'pub', atfork =>  sub { $self->atfork_handler() });
 
@@ -153,12 +153,22 @@ sub rest_handler {
            }
        }
 
+
+       my $result = $handler->handle($info, $uri_param);
+
        $resp = {
-           data => $handler->handle($info, $uri_param),
            info => $info, # useful to format output
            status => HTTP_OK,
        };
 
+       if ($info->{download}) {
+           die "download methods should have return type 'string' - internal error"
+               if ($info->{returns}->{type} ne 'string');
+           $resp->{download} = $result;
+       } else {
+           $resp->{data} = $result;
+       }
+
        if (my $count = $rpcenv->get_result_attrib('total')) {
            $resp->{total} = $count;
        }