-
Notifications
You must be signed in to change notification settings - Fork 111
/
repos.go
135 lines (110 loc) · 4.24 KB
/
repos.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package server
import (
"context"
"fmt"
"net/http"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/server/auth"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
)
// ListFiles implements RuntimeService.
func (s *Server) ListFiles(ctx context.Context, req *runtimev1.ListFilesRequest) (*runtimev1.ListFilesResponse, error) {
if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.ReadRepo) {
return nil, ErrForbidden
}
glob := req.Glob
if glob == "" {
glob = "**"
}
paths, err := s.runtime.ListFiles(ctx, req.InstanceId, glob)
if err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return &runtimev1.ListFilesResponse{Paths: paths}, nil
}
// GetFile implements RuntimeService.
func (s *Server) GetFile(ctx context.Context, req *runtimev1.GetFileRequest) (*runtimev1.GetFileResponse, error) {
if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.ReadRepo) {
return nil, ErrForbidden
}
blob, lastUpdated, err := s.runtime.GetFile(ctx, req.InstanceId, req.Path)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &runtimev1.GetFileResponse{Blob: blob, UpdatedOn: timestamppb.New(lastUpdated)}, nil
}
// PutFile implements RuntimeService.
func (s *Server) PutFile(ctx context.Context, req *runtimev1.PutFileRequest) (*runtimev1.PutFileResponse, error) {
if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.EditRepo) {
return nil, ErrForbidden
}
err := s.runtime.PutFile(ctx, req.InstanceId, req.Path, strings.NewReader(req.Blob), req.Create, req.CreateOnly)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &runtimev1.PutFileResponse{}, nil
}
// DeleteFile implements RuntimeService.
func (s *Server) DeleteFile(ctx context.Context, req *runtimev1.DeleteFileRequest) (*runtimev1.DeleteFileResponse, error) {
if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.EditRepo) {
return nil, ErrForbidden
}
err := s.runtime.DeleteFile(ctx, req.InstanceId, req.Path)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &runtimev1.DeleteFileResponse{}, nil
}
// RenameFile implements RuntimeService.
func (s *Server) RenameFile(ctx context.Context, req *runtimev1.RenameFileRequest) (*runtimev1.RenameFileResponse, error) {
if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.EditRepo) {
return nil, ErrForbidden
}
err := s.runtime.RenameFile(ctx, req.InstanceId, req.FromPath, req.ToPath)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
return &runtimev1.RenameFileResponse{}, nil
}
// UploadMultipartFile implements the same functionality as PutFile, but for multipart HTTP upload.
// It's mounted only on as a REST API and enables upload of large files (such as data files).
func (s *Server) UploadMultipartFile(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
if !auth.GetClaims(req.Context()).CanInstance(pathParams["instance_id"], auth.EditRepo) {
http.Error(w, "action not allowed", http.StatusUnauthorized)
return
}
ctx := context.Background()
if err := req.ParseForm(); err != nil {
http.Error(w, fmt.Sprintf("failed to parse request: %s", err), http.StatusBadRequest)
return
}
f, _, err := req.FormFile("file")
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse file in request: %s", err), http.StatusBadRequest)
return
}
if pathParams["path"] == "" {
http.Error(w, "must have a path to file", http.StatusBadRequest)
return
}
err = s.runtime.PutFile(ctx, pathParams["instance_id"], pathParams["path"], f, true, false)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write file: %s", err), http.StatusBadRequest)
return
}
res, err := protojson.Marshal(&runtimev1.PutFileResponse{})
if err != nil {
http.Error(w, fmt.Sprintf("failed to serialize response: %s", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(res)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write response data: %s", err), http.StatusInternalServerError)
return
}
}