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() {
archive, err := zip.OpenReader(archiveFileName)
if err != nil {
printError(fmt.Errorf("open %q: %v", archiveFileName, err))
os.Exit(notFound)
exitNotFound()
}
defer archive.Close()
file := findFileInZip(fileName, &archive.Reader)
if file == nil {
printError(fmt.Errorf("find %q in %q: not found", fileName, archiveFileName))
os.Exit(notFound)
exitNotFound()
}
// Start decompressing the file
reader, err := file.Open()
......@@ -45,10 +45,14 @@ func main() {
fatalError(fmt.Errorf("open %q in %q: %v", fileName, archiveFileName, err))
}
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 {
fatalError(fmt.Errorf("write %q from %q to stdout: %v", fileName, archiveFileName, err))
}
}
func findFileInZip(fileName string, archive *zip.Reader) *zip.File {
......@@ -68,3 +72,8 @@ func fatalError(err error) {
printError(err)
os.Exit(1)
}
func exitNotFound() {
fmt.Printf("%d\n", -notFound) // for the content-length reader
os.Exit(notFound)
}
......@@ -3,6 +3,7 @@ package artifacts
import (
"../api"
"../helper"
"bufio"
"encoding/base64"
"errors"
"fmt"
......@@ -12,11 +13,14 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
const exitStatusNotFound = 2
var notFoundString = fmt.Sprintf("%d", -exitStatusNotFound)
func decodeFileEntry(entry string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(entry)
if err != nil {
......@@ -45,14 +49,24 @@ func unpackFileFromZip(archiveFileName, fileName string, headers http.Header, ou
return fmt.Errorf("start %v: %v", catFile.Args, err)
}
defer helper.CleanUpProcessGroup(catFile)
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
headers.Set("Content-Length", contentLength)
headers.Set("Content-Type", detectFileContentType(fileName))
headers.Set("Content-Disposition", "attachment; filename=\""+escapeQuotes(basename)+"\"")
// 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)
}
......
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