backend.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "bytes"
20 "errors"
21 "net"
22 "net/http"
23 "net/rpc"
24 "os"
25 "os/exec"
26 "strings"
27
28 "humungus.tedunangst.com/r/webs/gate"
29 "humungus.tedunangst.com/r/webs/image"
30)
31
32type Shrinker struct {
33}
34
35type ShrinkerArgs struct {
36 Buf []byte
37 Params image.Params
38}
39
40type ShrinkerResult struct {
41 Image *image.Image
42}
43
44var shrinkgate = gate.NewLimiter(4)
45
46func (s *Shrinker) Shrink(args *ShrinkerArgs, res *ShrinkerResult) error {
47 shrinkgate.Start()
48 defer shrinkgate.Finish()
49 img, err := image.Vacuum(bytes.NewReader(args.Buf), args.Params)
50 if err != nil {
51 return err
52 }
53 res.Image = img
54 return nil
55}
56
57func backendSockname() string {
58 return dataDir + "/backend.sock"
59}
60
61func isSVG(data []byte) bool {
62 ct := http.DetectContentType(data)
63 if strings.HasPrefix(ct, "text/xml") {
64 return strings.Index(string(data), "<!DOCTYPE svg PUBLIC") != -1
65 }
66 if strings.HasPrefix(ct, "text/plain") {
67 return bytes.HasPrefix(data, []byte("<svg "))
68 }
69 return ct == "image/svg+xml"
70}
71
72func imageFromSVG(data []byte) (*image.Image, error) {
73 if len(data) > 100000 {
74 return nil, errors.New("my svg is too big")
75 }
76 svg := &image.Image{
77 Data: data,
78 Format: "svg+xml",
79 }
80 return svg, nil
81}
82
83func shrinkit(data []byte) (*image.Image, error) {
84 if isSVG(data) {
85 return imageFromSVG(data)
86 }
87 cl, err := rpc.Dial("unix", backendSockname())
88 if err != nil {
89 return nil, err
90 }
91 defer cl.Close()
92 var res ShrinkerResult
93 err = cl.Call("Shrinker.Shrink", &ShrinkerArgs{
94 Buf: data,
95 Params: image.Params{LimitSize: 4200 * 4200, MaxWidth: 2048, MaxHeight: 2048},
96 }, &res)
97 if err != nil {
98 return nil, err
99 }
100 return res.Image, nil
101}
102
103var backendhooks []func()
104
105func orphancheck() {
106 var b [1]byte
107 os.Stdin.Read(b[:])
108 dlog.Printf("backend shutting down")
109 os.Exit(0)
110}
111
112func backendServer() {
113 dlog.Printf("backend server running")
114 go orphancheck()
115 shrinker := new(Shrinker)
116 srv := rpc.NewServer()
117 err := srv.Register(shrinker)
118 if err != nil {
119 elog.Panicf("unable to register shrinker: %s", err)
120 }
121
122 sockname := backendSockname()
123 err = os.Remove(sockname)
124 if err != nil && !os.IsNotExist(err) {
125 elog.Panicf("unable to unlink socket: %s", err)
126 }
127
128 lis, err := net.Listen("unix", sockname)
129 if err != nil {
130 elog.Panicf("unable to register shrinker: %s", err)
131 }
132 err = setLimits()
133 if err != nil {
134 elog.Printf("error setting backend limits: %s", err)
135 }
136 for _, h := range backendhooks {
137 h()
138 }
139 srv.Accept(lis)
140}
141
142func runBackendServer() {
143 r, w, err := os.Pipe()
144 if err != nil {
145 elog.Panicf("can't pipe: %s", err)
146 }
147 proc := exec.Command(os.Args[0], reexecArgs("backend")...)
148 proc.Stdout = os.Stdout
149 proc.Stderr = os.Stderr
150 proc.Stdin = r
151 err = proc.Start()
152 if err != nil {
153 elog.Panicf("can't exec backend: %s", err)
154 }
155 go func() {
156 proc.Wait()
157 elog.Printf("lost the backend: %s", err)
158 w.Close()
159 }()
160}