-
Notifications
You must be signed in to change notification settings - Fork 3
/
read_pic_handler.go
226 lines (206 loc) · 5.69 KB
/
read_pic_handler.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package handlers
import (
"context"
"io"
"os"
"strconv"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"pixur.org/pixur/api"
"pixur.org/pixur/be/schema"
"pixur.org/pixur/be/status"
"pixur.org/pixur/be/tasks"
)
var picCacheTimeSeconds = 7 * 24 * time.Hour / time.Second
func apiFormatToSchemaMime(format api.PicFile_Format) (schema.Pic_File_Mime, status.S) {
apiName, present := api.PicFile_Format_name[int32(format)]
if !present {
return schema.Pic_File_UNKNOWN, status.InvalidArgument(nil, "unknown format")
}
schemaValue, present := schema.Pic_File_Mime_value[apiName]
if !present {
return schema.Pic_File_UNKNOWN, status.InvalidArgument(nil, "unknown name")
}
return schema.Pic_File_Mime(schemaValue), nil
}
// TODO: add tests
func (s *serv) handleLookupPicFile(ctx context.Context, req *api.LookupPicFileRequest) (
*api.LookupPicFileResponse, status.S) {
if sts := authReadPicRequest(ctx); sts != nil {
return nil, sts
}
var picId schema.Varint
var picDerivedIndex schema.Varint
n, err := picId.Decode(req.PicFileId)
if err != nil {
return nil, status.InvalidArgument(err, "can't decode pic id")
}
if len(req.PicFileId) != n {
if err := picDerivedIndex.DecodeAll(req.PicFileId[n:]); err != nil {
return nil, status.InvalidArgument(err, "can't decode pic index")
}
}
mime, sts := apiFormatToSchemaMime(req.Format)
if sts != nil {
return nil, sts
}
var path string
if len(req.PicFileId) != n {
path, sts = schema.PicFileDerivedPath(s.pixpath, int64(picId), int64(picDerivedIndex), mime)
} else {
path, sts = schema.PicFilePath(s.pixpath, int64(picId), mime)
}
if sts != nil {
return nil, sts
}
fi, err := os.Stat(path)
if err != nil {
return nil, status.NotFound(err, "can't open pic")
}
mts, err := ptypes.TimestampProto(fi.ModTime())
if err != nil {
return nil, status.Internal(err, "bad ts")
}
md, sts := readPicHeaders(ctx)
if sts != nil {
return nil, sts
}
grpc.SendHeader(ctx, md)
return &api.LookupPicFileResponse{
PicFile: &api.PicFile{
Id: req.PicFileId,
Format: req.Format,
// TODO: return right value
CreatedTime: mts,
ModifiedTime: mts,
Size: fi.Size(),
// TODO: include the rest of the values
},
}, nil
}
func authReadPicRequest(ctx context.Context) status.S {
if md, present := metadata.FromIncomingContext(ctx); present {
if tokens, ok := md[pixPwtHeaderKey]; !ok || len(tokens) == 0 {
conf, sts := tasks.GetConfiguration(ctx)
if sts != nil {
return sts
}
cs := schema.CapSetOf(conf.AnonymousCapability.Capability...)
_, _, missing := schema.CapIntersect(cs, schema.CapSetOf(schema.User_PIC_READ))
if missing.Size() != 0 {
return status.Unauthenticated(nil, "missing pix token")
}
} else if len(tokens) > 1 {
return status.Unauthenticated(nil, "too many tokens")
} else {
pixPayload, err := defaultPwtCoder.decode([]byte(tokens[0]))
if err != nil {
return status.Unauthenticated(err, err.Error())
}
if pixPayload.Type != api.PwtPayload_PIX {
return status.Unauthenticated(nil, "not pix token")
}
}
} else {
return status.Internal(nil, "missing MD")
}
return nil
}
func readPicHeaders(ctx context.Context) (metadata.MD, status.S) {
h1 := &api.HttpHeader{
Key: "Cache-Control",
}
conf, sts := tasks.GetConfiguration(ctx)
if sts != nil {
return nil, sts
}
h1.Value = "private"
for _, c := range conf.AnonymousCapability.Capability {
if c == schema.User_PIC_READ {
h1.Value = "public"
break
}
}
h1data, err := proto.Marshal(h1)
if err != nil {
return nil, status.Internal(err, "can't encode headers")
}
h2 := &api.HttpHeader{
Key: "Cache-Control",
Value: "max-age=" + strconv.Itoa(int(picCacheTimeSeconds)),
}
h2data, err := proto.Marshal(h2)
if err != nil {
return nil, status.Internal(err, "can't encode headers")
}
return metadata.Pairs(httpHeaderKey, string(h1data), httpHeaderKey, string(h2data)), nil
}
// TODO: add tests
func (s *serv) handleReadPicFile(rps api.PixurService_ReadPicFileServer) status.S {
if sts := authReadPicRequest(rps.Context()); sts != nil {
return sts
}
// ok, authed!
var f *os.File
for {
req, err := rps.Recv()
if err == io.EOF {
return nil
} else if err != nil {
return status.Internal(err, "can't recv")
}
if f == nil {
mime, sts := apiFormatToSchemaMime(req.Format)
if sts != nil {
return sts
}
var picId schema.Varint
var picDerivedIndex schema.Varint
n, err := picId.Decode(req.PicFileId)
if err != nil {
return status.InvalidArgument(err, "can't decode pic id")
}
if len(req.PicFileId) != n {
if err := picDerivedIndex.DecodeAll(req.PicFileId[n:]); err != nil {
return status.InvalidArgument(err, "can't decode pic index")
}
}
var path string
if len(req.PicFileId) != n {
path, sts = schema.PicFileDerivedPath(
s.pixpath, int64(picId), int64(picDerivedIndex), mime)
} else {
path, sts = schema.PicFilePath(s.pixpath, int64(picId), mime)
}
if sts != nil {
return sts
}
f, err = os.Open(path)
if err != nil {
return status.NotFound(err, "can't open pic")
}
defer f.Close()
}
resp := &api.ReadPicFileResponse{}
if req.Limit > 1048576 || req.Limit == 0 {
resp.Data = make([]byte, 1048576)
} else if req.Limit < 0 {
return status.InvalidArgument(nil, "bad limit")
} else {
resp.Data = make([]byte, int(req.Limit))
}
n, err := f.ReadAt(resp.Data, req.Offset)
if err == io.EOF {
resp.Eof = true
} else if err != nil {
return status.Internal(err, "can't read")
}
resp.Data = resp.Data[:n]
if err := rps.Send(resp); err != nil {
return status.Internal(err, "can't send")
}
}
}