forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
66 lines (53 loc) · 1.58 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package artifactserver
import (
"io"
"net/http"
"strconv"
"github.com/concourse/baggageclaim"
"github.com/pf-qiu/concourse/v6/atc/db"
)
func (s *Server) GetArtifact(team db.Team) http.Handler {
logger := s.logger.Session("get-artifact")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
artifactID, err := strconv.Atoi(r.FormValue(":artifact_id"))
if err != nil {
logger.Error("failed-to-get-artifact-id", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
artifactVolume, found, err := team.FindVolumeForWorkerArtifact(artifactID)
if err != nil {
logger.Error("failed-to-get-artifact-volume", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Error("failed-to-find-artifact-volume", err)
w.WriteHeader(http.StatusNotFound)
return
}
workerVolume, found, err := s.workerClient.FindVolume(logger, team.ID(), artifactVolume.Handle())
if err != nil {
logger.Error("failed-to-get-worker-volume", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Error("failed-to-find-worker-volume", err)
w.WriteHeader(http.StatusNotFound)
return
}
reader, err := workerVolume.StreamOut(r.Context(), "/", baggageclaim.GzipEncoding)
if err != nil {
logger.Error("failed-to-stream-volume-contents", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer reader.Close()
_, err = io.Copy(w, reader)
if err != nil {
logger.Error("failed-to-encode-artifact", err)
}
})
}