forked from AliyunContainerService/pouch
/
copy_bridge.go
91 lines (73 loc) · 2.21 KB
/
copy_bridge.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package server
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/alibaba/pouch/pkg/httputils"
"github.com/gorilla/mux"
)
func (s *Server) putContainersArchive(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
path := req.FormValue("path")
if name == "" {
return httputils.NewHTTPError(errors.New("name can't be empty"), http.StatusBadRequest)
}
if path == "" {
return httputils.NewHTTPError(errors.New("path can't be empty"), http.StatusBadRequest)
}
noOverwriteDirNonDir := httputils.BoolValue(req, "noOverwriteDirNonDir")
copyUIDGID := httputils.BoolValue(req, "copyUIDGID")
return s.ContainerMgr.ExtractToDir(ctx, name, path, copyUIDGID, noOverwriteDirNonDir, req.Body)
}
func (s *Server) headContainersArchive(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
path := req.FormValue("path")
if name == "" {
return httputils.NewHTTPError(errors.New("name can't be empty"), http.StatusBadRequest)
}
if path == "" {
return httputils.NewHTTPError(errors.New("path can't be empty"), http.StatusBadRequest)
}
stat, err := s.ContainerMgr.StatPath(ctx, name, path)
if err != nil {
return err
}
statJSON, err := json.Marshal(stat)
if err != nil {
return err
}
rw.Header().Set(
"X-Docker-Container-Path-Stat",
base64.StdEncoding.EncodeToString(statJSON),
)
return nil
}
func (s *Server) getContainersArchive(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
path := req.FormValue("path")
if name == "" {
return httputils.NewHTTPError(errors.New("name can't be empty"), http.StatusBadRequest)
}
if path == "" {
return httputils.NewHTTPError(errors.New("path can't be empty"), http.StatusBadRequest)
}
tarArchive, stat, err := s.ContainerMgr.ArchivePath(ctx, name, path)
if err != nil {
return err
}
defer tarArchive.Close()
statJSON, err := json.Marshal(stat)
if err != nil {
return err
}
rw.Header().Set(
"X-Docker-Container-Path-Stat",
base64.StdEncoding.EncodeToString(statJSON),
)
rw.Header().Set("Content-Type", "application/x-tar")
_, err = io.Copy(rw, tarArchive)
return err
}