-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
57 lines (51 loc) · 1.12 KB
/
main.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
package main
import (
"bytes"
"context"
"fmt"
"image/jpeg"
"os"
"github.com/disintegration/imaging"
"github.com/machinebox/remoto/examples/files/server/files"
"github.com/machinebox/remoto/remototypes"
"github.com/pkg/errors"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func run() error {
addr := "0.0.0.0:8080"
fmt.Println("listening on", addr)
err := files.Run(addr, service{})
if err != nil {
return err
}
return nil
}
type service struct{}
func (service) Flip(ctx context.Context, req *files.FlipRequest) (*remototypes.FileResponse, error) {
f, err := req.Image.Open(ctx)
if err != nil {
return nil, err
}
defer f.Close()
img, err := imaging.Decode(f)
if err != nil {
return nil, err
}
rotated := imaging.Rotate180(img)
var buf bytes.Buffer
if err := jpeg.Encode(&buf, rotated, &jpeg.Options{Quality: 100}); err != nil {
return nil, errors.Wrap(err, "encode jpeg")
}
resp := &remototypes.FileResponse{
Filename: "flipped.jpg",
Data: &buf,
ContentLength: buf.Len(),
ContentType: "image/jpeg",
}
return resp, nil
}