[pbs-devel] [PATCH common v2 3/7] stream file method for PMG and PVE

Daniel Tschlatscher d.tschlatscher at proxmox.com
Wed Sep 7 10:56:29 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 | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index eb81b96..5639c32 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -2056,4 +2056,32 @@ sub get_file_hash {
     return lc($digest);
 }
 
+sub stream_file {
+    my ($filename, $suggested_name, $compress, $content_type) = @_;
+
+    my $fh;
+    $content_type = $content_type // "text/plain";
+    $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' => $content_type,
+	    'content-disposition' => "attachment; filename=\"$suggested_name\"",
+	},
+    },
+}
+
 1;
-- 
2.30.2






More information about the pbs-devel mailing list