1
0
Fork 0
mirror of https://github.com/Luzifer/tex-api.git synced 2024-11-09 08:40:02 +00:00

Support TAR output format

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-03-06 00:17:35 +01:00
parent 1d28bd2c1a
commit f2d676c7db
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

45
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"encoding/json"
@ -299,6 +300,47 @@ func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) {
return buf, w.Close()
}
func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) {
buf := new(bytes.Buffer)
w := tar.NewWriter(buf)
basePath := pathFromUUID(uid, filenameOutputDir)
err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !shouldPackFile(path.Ext(info.Name())) {
return nil
}
tarInfo, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\")
err = w.WriteHeader(tarInfo)
if err != nil {
return err
}
osFile, err := os.Open(p)
if err != nil {
return err
}
io.Copy(w, osFile)
osFile.Close()
return nil
})
if err != nil {
return nil, err
}
return buf, w.Close()
}
func downloadAssets(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
uid, err := uuid.FromString(vars["uid"])
@ -313,6 +355,9 @@ func downloadAssets(res http.ResponseWriter, r *http.Request) {
)
switch r.Header.Get("Accept") {
case "application/tar", "application/x-tar", "applicaton/x-gtar", "multipart/x-tar", "application/x-compress", "application/x-compressed":
content, err = buildAssetsTAR(uid)
filename = uid.String() + ".tar"
default:
content, err = buildAssetsZIP(uid)
filename = uid.String() + ".zip"