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.tschlatsc...@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



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to