Commit 1629812b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Pass content-length from gitlab-zip-cat

parent 1ccba44f
...@@ -30,14 +30,14 @@ func main() { ...@@ -30,14 +30,14 @@ func main() {
archive, err := zip.OpenReader(archiveFileName) archive, err := zip.OpenReader(archiveFileName)
if err != nil { if err != nil {
printError(fmt.Errorf("open %q: %v", archiveFileName, err)) printError(fmt.Errorf("open %q: %v", archiveFileName, err))
os.Exit(notFound) exitNotFound()
} }
defer archive.Close() defer archive.Close()
file := findFileInZip(fileName, &archive.Reader) file := findFileInZip(fileName, &archive.Reader)
if file == nil { if file == nil {
printError(fmt.Errorf("find %q in %q: not found", fileName, archiveFileName)) printError(fmt.Errorf("find %q in %q: not found", fileName, archiveFileName))
os.Exit(notFound) exitNotFound()
} }
// Start decompressing the file // Start decompressing the file
reader, err := file.Open() reader, err := file.Open()
...@@ -45,10 +45,14 @@ func main() { ...@@ -45,10 +45,14 @@ func main() {
fatalError(fmt.Errorf("open %q in %q: %v", fileName, archiveFileName, err)) fatalError(fmt.Errorf("open %q in %q: %v", fileName, archiveFileName, err))
} }
defer reader.Close() defer reader.Close()
if _, err := fmt.Printf("%d\n", file.UncompressedSize64); err != nil {
fatalError(fmt.Errorf("write file size: %v", err))
}
if _, err := io.Copy(os.Stdout, reader); err != nil { if _, err := io.Copy(os.Stdout, reader); err != nil {
fatalError(fmt.Errorf("write %q from %q to stdout: %v", fileName, archiveFileName, err)) fatalError(fmt.Errorf("write %q from %q to stdout: %v", fileName, archiveFileName, err))
} }
} }
func findFileInZip(fileName string, archive *zip.Reader) *zip.File { func findFileInZip(fileName string, archive *zip.Reader) *zip.File {
...@@ -68,3 +72,8 @@ func fatalError(err error) { ...@@ -68,3 +72,8 @@ func fatalError(err error) {
printError(err) printError(err)
os.Exit(1) os.Exit(1)
} }
func exitNotFound() {
fmt.Printf("%d\n", -notFound) // for the content-length reader
os.Exit(notFound)
}
...@@ -3,6 +3,7 @@ package artifacts ...@@ -3,6 +3,7 @@ package artifacts
import ( import (
"../api" "../api"
"../helper" "../helper"
"bufio"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
...@@ -12,11 +13,14 @@ import ( ...@@ -12,11 +13,14 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"syscall" "syscall"
) )
const exitStatusNotFound = 2 const exitStatusNotFound = 2
var notFoundString = fmt.Sprintf("%d", -exitStatusNotFound)
func decodeFileEntry(entry string) (string, error) { func decodeFileEntry(entry string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(entry) decoded, err := base64.StdEncoding.DecodeString(entry)
if err != nil { if err != nil {
...@@ -45,14 +49,24 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou ...@@ -45,14 +49,24 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou
return fmt.Errorf("start %v: %v", catFile.Args, err) return fmt.Errorf("start %v: %v", catFile.Args, err)
} }
defer helper.CleanUpProcessGroup(catFile) defer helper.CleanUpProcessGroup(catFile)
basename := filepath.Base(fileName) basename := filepath.Base(fileName)
reader := bufio.NewReader(stdout)
contentLength, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("read content-length: %v", err)
}
contentLength = strings.TrimSuffix(contentLength, "\n")
if contentLength == notFoundString {
return os.ErrNotExist
}
// Write http headers about the file // Write http headers about the file
headers.Set("Content-Length", contentLength)
headers.Set("Content-Type", detectFileContentType(fileName)) headers.Set("Content-Type", detectFileContentType(fileName))
headers.Set("Content-Disposition", "attachment; filename=\""+escapeQuotes(basename)+"\"") headers.Set("Content-Disposition", "attachment; filename=\""+escapeQuotes(basename)+"\"")
// Copy file body to client // Copy file body to client
if _, err := io.Copy(output, stdout); err != nil { if _, err := io.Copy(output, reader); err != nil {
return fmt.Errorf("copy %v stdout: %v", catFile.Args, err) return fmt.Errorf("copy %v stdout: %v", catFile.Args, err)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment