[pmg-devel] [PATCH pmg-api 1/1] Quarantine API: extend download call to download whole mails

Dominik Csapak d.csapak at proxmox.com
Wed Oct 23 09:36:34 CEST 2019


this makes the attachmentid parameter optional and if it is not given,
open the whole mail for download

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/PMG/API2/Quarantine.pm | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/PMG/API2/Quarantine.pm b/src/PMG/API2/Quarantine.pm
index 1869093..5cb0f8e 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -1053,8 +1053,8 @@ __PACKAGE__->register_method ({
     name => 'download',
     path => 'download',
     method => 'GET',
-    permissions => { check => [ 'admin', 'qmanager', 'audit'] },
-    description => "Download Attachment for E-Mail in Quarantine.",
+    permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
+    description => "Download E-Mail or Attachment from Quarantine.",
     download => 1,
     parameters => {
 	additionalProperties => 0,
@@ -1068,6 +1068,7 @@ __PACKAGE__->register_method ({
 	    attachmentid => {
 		description => "The Attachment ID for the mail.",
 		type => 'integer',
+		optional => 1,
 	    },
 	},
     },
@@ -1081,17 +1082,29 @@ __PACKAGE__->register_method ({
 	my $attachmentid = $param->{attachmentid};
 
 	my $dumpdir = "/run/pmgproxy/pmg-$mailid-$$/";
-	my $attachments = $get_attachments->($mailid, $dumpdir, 1);
+	my $res;
 
-	my $res = $attachments->[$attachmentid];
-	if (!$res) {
-	    raise_param_exc({ attachmentid => "Invalid Attachment ID for Mail."});
+	if ($attachmentid) {
+	    my $attachments = $get_attachments->($mailid, $dumpdir, 1);
+	    $res = $attachments->[$attachmentid];
+	    if (!$res) {
+		raise_param_exc({ attachmentid => "Invalid Attachment ID for Mail."});
+	    }
+	} else {
+	    my $rpcenv = PMG::RESTEnvironment->get();
+	    my $ref = $get_and_check_mail->($mailid, $rpcenv);
+	    my $spooldir = $PMG::MailQueue::spooldir;
+
+	    $res = {
+		'content-type' => 'message/rfc822',
+		path => "$spooldir/$ref->{file}",
+	    };
 	}
 
 	$res->{fh} = IO::File->new($res->{path}, '<') ||
 	    die "unable to open file '$res->{path}' - $!\n";
 
-	rmtree $dumpdir;
+	rmtree $dumpdir if -e $dumpdir;
 
 	return $res;
 
-- 
2.20.1




More information about the pmg-devel mailing list