-
Notifications
You must be signed in to change notification settings - Fork 887
feat: support template bundles as zip archives #11839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
11fd552
bc9a555
af8a0d5
db12fda
b20a483
fd86896
8f631ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 nice tests |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package coderd | ||
|
||
import ( | ||
"archive/tar" | ||
"archive/zip" | ||
"bytes" | ||
"errors" | ||
"io" | ||
"log" | ||
) | ||
|
||
func CreateTarFromZip(zipReader *zip.Reader) ([]byte, error) { | ||
var tarBuffer bytes.Buffer | ||
err := writeTarArchive(&tarBuffer, zipReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tarBuffer.Bytes(), nil | ||
} | ||
|
||
func writeTarArchive(w io.Writer, zipReader *zip.Reader) error { | ||
tarWriter := tar.NewWriter(w) | ||
defer tarWriter.Close() | ||
|
||
for _, file := range zipReader.File { | ||
err := processFileInZipArchive(file, tarWriter) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func processFileInZipArchive(file *zip.File, tarWriter *tar.Writer) error { | ||
fileReader, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer fileReader.Close() | ||
|
||
err = tarWriter.WriteHeader(&tar.Header{ | ||
Name: file.Name, | ||
Size: file.FileInfo().Size(), | ||
Mode: 0o644, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
n, err := io.CopyN(tarWriter, fileReader, httpFileMaxBytes) | ||
log.Println(file.Name, n, err) | ||
if errors.Is(err, io.EOF) { | ||
err = nil | ||
} | ||
return err | ||
} | ||
|
||
func CreateZipFromTar(tarReader *tar.Reader) ([]byte, error) { | ||
var zipBuffer bytes.Buffer | ||
err := WriteZipArchive(&zipBuffer, tarReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return zipBuffer.Bytes(), nil | ||
} | ||
|
||
func WriteZipArchive(w io.Writer, tarReader *tar.Reader) error { | ||
zipWriter := zip.NewWriter(w) | ||
defer zipWriter.Close() | ||
|
||
for { | ||
tarHeader, err := tarReader.Next() | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
zipHeader, err := zip.FileInfoHeader(tarHeader.FileInfo()) | ||
if err != nil { | ||
return err | ||
} | ||
zipHeader.Name = tarHeader.Name | ||
|
||
zipEntry, err := zipWriter.CreateHeader(zipHeader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = io.CopyN(zipEntry, tarReader, httpFileMaxBytes) | ||
if errors.Is(err, io.EOF) { | ||
err = nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil // don't need to flush as we call `writer.Close()` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe no harm to allow explicitly setting ContentTypeTar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but it would be another place to adjust if we decide to natively support other formats.