summaryrefslogtreecommitdiff
path: root/app/util.go
blob: 205ed7a8a0735b16f90230f58aa81a17cb42a0a6 (plain) (blame)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package app

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"skunkyart/static"
	"strconv"
	"strings"
	"text/template"
	"time"

	"github.com/krazywarez/devianter"
	"golang.org/x/net/html"
)

/* INTERNAL */

// wr writes s to w. A write error here means the client went away mid-response,
// which a handler cannot act on, so it is deliberately discarded.
func wr(w io.Writer, s string) {
	_, _ = io.WriteString(w, s)
}

func exit(msg string, code int) {
	println(msg)
	os.Exit(code)
}
func try(e error) {
	if e != nil {
		println(e.Error())
	}
}
func tryWithExitStatus(err error, code int) {
	if err != nil {
		exit(err.Error(), code)
	}
}

// restore swallows a panic in the calling goroutine so that one bad parse cannot
// take the whole process down. The panic is logged rather than dropped silently.
func restore() {
	if r := recover(); r != nil {
		println("recovered from panic:", fmt.Sprint(r))
	}
}

var instances []byte

// About is the instance list and settings shown in the frontend, refreshed by
// RefreshInstances.
var About instanceAbout

// RefreshInstances re-fetches the published instance list every hour, forever.
// Run it in its own goroutine; fetch failures are logged and retried next cycle.
func RefreshInstances() {
	for {
		func() {
			defer restore()
			instances = Download("https://raw.githubusercontent.com/krazywarez/skunky-art/main/instances.json").Body
			try(json.Unmarshal(instances, &About))
		}()
		time.Sleep(1 * time.Hour)
	}
}

// instanceAbout is the instance metadata exposed to the frontend and the API.
type instanceAbout struct {
	Proxy     bool       `json:"proxy"`
	Nsfw      bool       `json:"nsfw"`
	Instances []settings `json:"instances"`
}

type skunkyart struct {
	Writer http.ResponseWriter
	_pth   string

	Args url.Values
	Page int
	Type rune
	Atom bool

	// Host is the scheme and host this request arrived on, e.g.
	// "https://art.example.com". It is per-request rather than global because
	// concurrent requests can arrive on different hosts and ports.
	Host string

	BasePath, Endpoint string
	Query, QueryRaw    string

	API     API
	Version string

	Templates struct {
		About instanceAbout

		SomeList  string
		DDStrips  string
		Deviation struct {
			Post       devianter.Post
			Related    string
			StringTime string
			Tags       string
			Comments   string
		}

		GroupUser struct {
			GR           devianter.GRuser
			Admins       string
			Group        bool
			CreationDate string

			About struct {
				A devianter.About

				DescriptionFormatted string
				Interests, Social    string
				Comments             string
				BG                   string
				BGMeta               devianter.Deviation
			}

			Gallery struct {
				Folders string
				Pages   int
				List    string
			}
		}
		Search struct {
			Content devianter.Search
			List    string
		}
	}
}

// ExecuteTemplate renders the named template from dir with data, responding 500
// if the template cannot be parsed.
func (s skunkyart) ExecuteTemplate(file, dir string, data any) {
	var buf strings.Builder
	tmp := template.New(file)
	tmp, err := tmp.ParseFS(static.Templates, dir+"/*")
	if err != nil {
		s.Writer.WriteHeader(500)
		wr(s.Writer, err.Error())
		return
	}
	try(tmp.Execute(&buf, &data))
	wr(s.Writer, buf.String())
}

// URLBuilder joins strs into an absolute instance URL, prefixing host and the
// configured URI and inserting slashes between path segments but not before
// query separators. host is the request's own scheme and host: passing the
// wrong one emits links to another origin, which the instance's own
// Content-Security-Policy then blocks.
func URLBuilder(host string, strs ...string) string {
	var str strings.Builder
	l := len(strs)
	str.WriteString(host)
	str.WriteString(CFG.URI)
	for n, x := range strs {
		str.WriteString(x)
		if n := n + 1; n < l && len(strs[n]) != 0 && (strs[n][0] != '?' && strs[n][0] != '&') && (x[0] != '?' && x[0] != '&') {
			str.WriteString("/")
		}
	}
	return str.String()
}

// Error responds 502 with the error DeviantArt reported upstream.
func (s skunkyart) Error(dAerr devianter.Error) {
	s.Writer.WriteHeader(502)

	var msg strings.Builder
	msg.WriteString(`<html><link rel="stylesheet" href="`)
	msg.WriteString(URLBuilder(s.Host, "stylesheet"))
	msg.WriteString(`" /><h3>DeviantArt error — '`)
	msg.WriteString(dAerr.Error)
	msg.WriteString("'</h3></html>")

	wr(s.Writer, msg.String())
}

// ReturnHTTPError responds with a styled error page for the given status.
func (s skunkyart) ReturnHTTPError(status int) {
	// A failed upstream fetch reports status 0, and WriteHeader panics on any
	// code outside 1xx-5xx. Treat anything unusable as a gateway failure.
	if status < 100 || status > 599 {
		status = http.StatusBadGateway
	}
	s.Writer.WriteHeader(status)

	var msg strings.Builder
	msg.WriteString(`<html><link rel="stylesheet" href="`)
	msg.WriteString(URLBuilder(s.Host, "stylesheet"))
	msg.WriteString(`" /><h1>`)
	msg.WriteString(strconv.Itoa(status))
	msg.WriteString(" - ")
	msg.WriteString(http.StatusText(status))
	msg.WriteString("</h1></html>")

	wr(s.Writer, msg.String())
}

// SetFilename sets the Content-Disposition filename for the response.
func (s skunkyart) SetFilename(name string) {
	var filename strings.Builder
	filename.WriteString(`filename="`)
	filename.WriteString(name)
	filename.WriteString(`"`)
	s.Writer.Header().Add("Content-Disposition", filename.String())
}

// Downloaded is the result of a Download. A Status of 0 means the request never
// completed, in which case Body and Headers are empty.
type Downloaded struct {
	Headers http.Header
	Status  int
	Body    []byte
}

// Download fetches urlString with the configured User-Agent, routing through
// download-proxy when one is set. Every failure path returns the zero
// Downloaded, so callers must check Status before trusting Body or Headers.
func Download(urlString string) (d Downloaded) {
	cli := &http.Client{}
	if CFG.DownloadProxy != "" {
		u, err := url.Parse(CFG.DownloadProxy)
		if err != nil {
			try(err)
			return
		}
		cli.Transport = ProxiedTransport(u)
	}

	ctx, cancel := context.WithTimeout(context.Background(), downloadTimeout)
	defer cancel()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlString, nil)
	if err != nil {
		try(err)
		return
	}
	req.Header.Set("User-Agent", CFG.UserAgent)

	resp, err := cli.Do(req)
	if err != nil {
		try(err)
		return
	}
	defer func() { try(resp.Body.Close()) }()

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		try(err)
		return
	}

	d.Body = b
	d.Status = resp.StatusCode
	d.Headers = resp.Header
	return
}

/* PARSING HELPERS */

// ParseMedia returns the URL to serve for media: a link back through this
// instance's media proxy when proxying is on, or DeviantArt's own URL when it is
// off. An optional thumb width selects a thumbnail instead of the full image.
// host is the request's scheme and host, as taken by URLBuilder.
func ParseMedia(host string, media devianter.Media, thumb ...int) string {
	mediaURL, filename := devianter.UrlFromMedia(media, thumb...)
	if len(mediaURL) != 0 && CFG.Proxy {
		mediaURL = mediaURL[21:]
		dot := strings.Index(mediaURL, ".")
		if filename == "" {
			filename = "image.gif"
		}
		return URLBuilder(host, "media", "file", mediaURL[:dot], mediaURL[dot+11:], "&filename=", filename)
	} else if !CFG.Proxy {
		return mediaURL
	}
	return ""
}

// ConvertDeviantArtURLToSkunkyArt rewrites a deviantart.com post link into the
// equivalent link on this instance. It returns an empty string for URLs it does
// not handle, including sta.sh links. host is the request's scheme and host, as
// taken by URLBuilder.
func ConvertDeviantArtURLToSkunkyArt(host, url string) (output string) {
	if len(url) > 32 && url[27:32] != "stash" {
		url = url[27:]
		firstshash := strings.Index(url, "/")
		lastshash := firstshash + strings.Index(url[firstshash+1:], "/")
		if lastshash != -1 {
			output = URLBuilder(host, "post", url[:firstshash], url[lastshash+2:])
		}
	}
	return
}

// BuildUserPlate renders the small avatar-and-username block linking to a user's
// about page. host is the request's scheme and host, as taken by URLBuilder.
func BuildUserPlate(host, name string) string {
	var htm strings.Builder
	htm.WriteString(`<div class="user-plate"><img src="`)
	htm.WriteString(URLBuilder(host, "media", "emojitar", name, "?type=a"))
	htm.WriteString(`"><a href="`)
	htm.WriteString(URLBuilder(host, "group_user", "?type=about&q=", name))
	htm.WriteString(`">`)
	htm.WriteString(name)
	htm.WriteString(`</a></div>`)
	return htm.String()
}

// GetValueOfTag returns the text of the tokenizer's next token, or an empty
// string if that token is not text.
func GetValueOfTag(t *html.Tokenizer) string {
	for tt := t.Next(); ; {
		if tt == html.TextToken {
			return string(t.Text())
		} else {
			return ""
		}
	}
}

// DeviationList describes the pagination state of a list of artworks: how many
// pages exist, and whether another page follows the current one.
type DeviationList struct {
	Pages int
	More  bool
}

// NavBase renders the page navigation bar for a list.
//
// FIXME: on some artworks the first page can make the navigation panel disappear
// entirely.
func (s skunkyart) NavBase(c DeviationList) string {
	var list strings.Builder

	list.WriteString("<br>")
	prevrev := func(msg string, page int, onpage bool) {
		if !onpage {
			list.WriteString(`<a href="`)
			list.WriteString(s._pth)
			list.WriteString(`?p=`)
			list.WriteString(strconv.Itoa(page))
			if s.Type != 0 {
				list.WriteString("&type=")
				list.WriteRune(s.Type)
			}
			if s.Query != "" {
				list.WriteString("&q=")
				list.WriteString(s.Query)
			}
			if f := s.Args.Get("folder"); f != "" {
				list.WriteString("&folder=")
				list.WriteString(f)
			}
			list.WriteString(`">`)
			list.WriteString(msg)
			list.WriteString("</a> ")
		} else {
			list.WriteString(strconv.Itoa(page))
			list.WriteString(" ")
		}
	}

	p := s.Page

	if p > 1 {
		prevrev("<= Prev |", p-1, false)
	} else {
		p = 1
	}

	for i, x := p-6, 0; (i <= c.Pages && i <= p+6) && x < 12; i++ {
		if i > 0 {
			var onPage bool
			if i == p {
				onPage = true
			}

			prevrev(strconv.Itoa(i), i, onPage)
			x++
		}
	}

	if c.More {
		prevrev("| Next =>", p+1, false)
	}

	return list.String()
}