-
Notifications
You must be signed in to change notification settings - Fork 338
/
api_content.go
71 lines (54 loc) · 1.64 KB
/
api_content.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
package server
import (
"context"
"errors"
"net/http"
"github.com/gorilla/mux"
"github.com/kopia/kopia/internal/serverapi"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
func (s *Server) handleContentGet(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
dr, ok := s.rep.(*repo.DirectRepository)
if !ok {
return nil, notFoundError("content not found")
}
cid := content.ID(mux.Vars(r)["contentID"])
data, err := dr.Content.GetContent(ctx, cid)
if errors.Is(err, content.ErrContentNotFound) {
return nil, notFoundError("content not found")
}
return data, nil
}
func (s *Server) handleContentInfo(ctx context.Context, r *http.Request, body []byte) (interface{}, *apiError) {
dr, ok := s.rep.(*repo.DirectRepository)
if !ok {
return nil, notFoundError("content not found")
}
cid := content.ID(mux.Vars(r)["contentID"])
ci, err := dr.Content.ContentInfo(ctx, cid)
switch err {
case nil:
return ci, nil
case content.ErrContentNotFound:
return nil, notFoundError("content not found")
default:
return nil, internalServerError(err)
}
}
func (s *Server) handleContentPut(ctx context.Context, r *http.Request, data []byte) (interface{}, *apiError) {
dr, ok := s.rep.(*repo.DirectRepository)
if !ok {
return nil, notFoundError("content not found")
}
cid := content.ID(mux.Vars(r)["contentID"])
prefix := cid.Prefix()
actualCID, err := dr.Content.WriteContent(ctx, data, prefix)
if err != nil {
return nil, internalServerError(err)
}
if actualCID != cid {
return nil, requestError(serverapi.ErrorMalformedRequest, "mismatched content ID")
}
return &serverapi.Empty{}, nil
}