1
1
package handlers
2
2
3
3
import (
4
+ "archive/tar"
4
5
"fmt"
5
6
"io"
6
7
"net/http"
7
8
"os"
8
9
"path/filepath"
10
+ "time"
9
11
12
+ "github.com/klauspost/pgzip"
10
13
"github.com/pkg/errors"
11
14
"github.com/replicatedhq/kots/pkg/embeddedcluster"
12
15
"github.com/replicatedhq/kots/pkg/logger"
@@ -72,10 +75,10 @@ func (h *Handler) GetEmbeddedClusterBinary(w http.ResponseWriter, r *http.Reques
72
75
return
73
76
}
74
77
75
- // Set response headers for binary file
76
- w . Header (). Set ( "Content-Disposition" , fmt .Sprintf ("attachment; filename=%s " , binaryName ) )
77
- w .Header ().Set ("Content-Type " , "application/octet-stream" )
78
- w .Header ().Set ("Content-Length " , fmt . Sprintf ( "%d" , binaryStat . Size ()) )
78
+ // Set response headers for the .tgz file
79
+ filename := fmt .Sprintf ("%s.tgz " , binaryName )
80
+ w .Header ().Set ("Content-Disposition " , fmt . Sprintf ( "attachment; filename=%s" , filename ) )
81
+ w .Header ().Set ("Content-Type " , "application/gzip" )
79
82
80
83
// Open binary file
81
84
binaryFile , err := os .Open (binaryPath )
@@ -86,9 +89,31 @@ func (h *Handler) GetEmbeddedClusterBinary(w http.ResponseWriter, r *http.Reques
86
89
}
87
90
defer binaryFile .Close ()
88
91
89
- // Stream the binary directly to the response
90
- if _ , err := io .Copy (w , binaryFile ); err != nil {
91
- logger .Error (errors .Wrap (err , "failed to write binary to response" ))
92
+ // Create pgzip writer
93
+ gzipWriter := pgzip .NewWriter (w )
94
+ defer gzipWriter .Close ()
95
+
96
+ // Create tar writer
97
+ tarWriter := tar .NewWriter (gzipWriter )
98
+ defer tarWriter .Close ()
99
+
100
+ // Add binary file to tar archive
101
+ header := & tar.Header {
102
+ Name : binaryName ,
103
+ Mode : 0755 , // Executable permission
104
+ Size : binaryStat .Size (),
105
+ ModTime : time .Now (),
106
+ Format : tar .FormatGNU ,
107
+ }
108
+
109
+ if err := tarWriter .WriteHeader (header ); err != nil {
110
+ logger .Error (errors .Wrap (err , "failed to write tar header" ))
111
+ return
112
+ }
113
+
114
+ // Copy binary content to tar archive
115
+ if _ , err := io .Copy (tarWriter , binaryFile ); err != nil {
116
+ logger .Error (errors .Wrap (err , "failed to write binary to tar archive" ))
92
117
return
93
118
}
94
119
}
0 commit comments