[pve-devel] [PATCH common 1/1] fix #3971: Create log file stream for download

Daniel Tschlatscher d.tschlatscher at proxmox.com
Tue Apr 26 14:35:38 CEST 2022


Creating the filestream for the tasklog download is sourced in its own
function to avoid redundant implementations in pmg-api and pve-manager
.

Signed-off-by: Daniel Tschlatscher <d.tschlatscher at proxmox.com>
---
 src/PVE/Tools.pm | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index dac0a2b..ec8681a 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -2016,4 +2016,31 @@ sub get_file_hash {
     return lc($digest);
 }
 
+sub stream_logfile {
+    my ($filename, $compress, $suggested_name) = @_;
+
+    my $fh;
+    $suggested_name = $suggested_name // $filename;
+
+    if ($compress) {
+	my $cmd = ["/usr/bin/gzip", "-c", "$filename"];
+
+	open($fh, "-|", join(' ', @$cmd))
+	    or die "Could not create compressed file stream for $filename - $!";
+    } else {
+	open($fh, '<', $filename)
+	    or die "Could not open file $filename - $!";
+    }
+
+    return {
+	download => {
+	    fh => $fh,
+	    stream => 1,
+	    'content-encoding' => $compress ? 'gzip' : undef,
+	    'content-type' => 'text/plain',
+	    'content-disposition' => "attachment; filename=\"$suggested_name\"",
+	},
+    },
+}
+
 1;
-- 
2.30.2






More information about the pve-devel mailing list