-
Notifications
You must be signed in to change notification settings - Fork 3
/
upsert_pic_handler.go
67 lines (55 loc) · 1.25 KB
/
upsert_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
package handlers
import (
"bytes"
"context"
"crypto/md5"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"pixur.org/pixur/api"
"pixur.org/pixur/be/status"
"pixur.org/pixur/be/tasks"
)
var _ multipart.File = memFile{}
type memFile struct {
*bytes.Reader
}
func (f memFile) Close() error {
return nil
}
func (s *serv) handleUpsertPic(ctx context.Context, req *api.UpsertPicRequest) (*api.UpsertPicResponse, status.S) {
var file multipart.File
if len(req.Data) != 0 {
// make sure this is non nil only if there is actually data.
file = memFile{bytes.NewReader(req.Data)}
}
switch len(req.Md5Hash) {
case 0:
case md5.Size:
default:
return nil, status.InvalidArgument(nil, "bad md5 hash")
}
var task = &tasks.UpsertPicTask{
PixPath: s.pixpath,
Beg: s.db,
HTTPClient: http.DefaultClient,
TempFile: ioutil.TempFile,
Rename: os.Rename,
MkdirAll: os.MkdirAll,
Now: s.now,
Remove: os.Remove,
FileURL: req.Url,
FileURLReferrer: req.Referrer,
File: file,
Md5Hash: req.Md5Hash,
FileName: req.Name,
Ext: req.Ext,
}
if sts := s.runner.Run(ctx, task); sts != nil {
return nil, sts
}
return &api.UpsertPicResponse{
Pic: apiPic(task.CreatedPic),
}, nil
}