summaryrefslogtreecommitdiff
path: root/app/util.go
blob: 3f69e47217bb38caf24ab5f723dd4ab7c9a92a59 (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
package app

import (
	"encoding/base64"
	"io"
	"net/http"
	u "net/url"
	"os"
	"strconv"
	"strings"
	"syscall"
	"text/template"
	"time"

	"git.macaw.me/skunky/devianter"
	"golang.org/x/net/html"
)

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

// some crap for frontend
func (s skunkyart) ExecuteTemplate(file string, data any) {
	var buf strings.Builder
	tmp := template.New(file)
	tmp, e := tmp.Parse(Templates[file])
	try(e)
	try(tmp.Execute(&buf, &data))
	wr(s.Writer, buf.String())
}

func UrlBuilder(strs ...string) string {
	var str strings.Builder
	l := len(strs)
	str.WriteString(Host)
	str.WriteString(CFG.BasePath)
	for n, x := range strs {
		str.WriteString(x)
		if n+1 < l && !(strs[n+1][0] == '?' || strs[n+1][0] == '&') && !(x[0] == '?' || x[0] == '&') {
			str.WriteString("/")
		}
	}
	return str.String()
}

func (s skunkyart) ReturnHTTPError(status int) {
	s.Writer.WriteHeader(status)

	var msg strings.Builder
	msg.WriteString(`<html><link rel="stylesheet" href="`)
	msg.WriteString(UrlBuilder("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())
}

type Downloaded struct {
	Headers http.Header
	Status  int
	Body    []byte
}

func Download(url string) (d Downloaded) {
	cli := &http.Client{}
	if CFG.DownloadProxy != "" {
		u, e := u.Parse(CFG.DownloadProxy)
		try(e)
		cli.Transport = &http.Transport{Proxy: http.ProxyURL(u)}
	}

	req, e := http.NewRequest("GET", url, nil)
	try(e)
	req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0.0")

	resp, e := cli.Do(req)
	try(e)
	defer resp.Body.Close()
	b, e := io.ReadAll(resp.Body)
	try(e)

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

// caching
func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
	var url strings.Builder
	url.WriteString("https://images-wixmp-")
	url.WriteString(subdomain)
	url.WriteString(".wixmp.com/")
	url.WriteString(path)
	url.WriteString("?token=")
	url.WriteString(s.Args.Get("token"))

	if CFG.Cache.Enabled {
		fname := CFG.Cache.Path + "/" + base64.StdEncoding.EncodeToString([]byte(subdomain+path))
		file, e := os.Open(fname)

		if e != nil {
			dwnld := Download(url.String())
			if dwnld.Status == 200 && dwnld.Headers["Content-Type"][0][:5] == "image" {
				try(os.WriteFile(fname, dwnld.Body, 0700))
				s.Writer.Write(dwnld.Body)
			}
		} else {
			file, e := io.ReadAll(file)
			try(e)
			s.Writer.Write(file)
		}
	} else if CFG.Proxy {
		dwnld := Download(url.String())
		s.Writer.Write(dwnld.Body)
	} else {
		s.Writer.WriteHeader(403)
		s.Writer.Write([]byte("Sorry, butt proxy on this instance are disabled."))
	}
}

func InitCacheSystem() {
	c := &CFG.Cache
	os.Mkdir(CFG.Cache.Path, 0700)
	for {
		dir, e := os.Open(c.Path)
		try(e)
		stat, e := dir.Stat()
		try(e)

		dirnames, e := dir.Readdirnames(-1)
		try(e)
		for _, a := range dirnames {
			a = c.Path + "/" + a
			if c.Lifetime != 0 {
				now := time.Now().UnixMilli()

				f, _ := os.Stat(a)
				stat := f.Sys().(*syscall.Stat_t)
				time := time.Unix(stat.Ctim.Unix()).UnixMilli()

				if time+c.Lifetime <= now {
					try(os.RemoveAll(a))
				}
			}
			if c.MaxSize != 0 && stat.Size() > c.MaxSize {
				try(os.RemoveAll(a))
			}
		}

		dir.Close()
		time.Sleep(time.Second * time.Duration(CFG.Cache.UpdateInterval))
	}
}

func CopyTemplatesToMemory() {
	for _, dirname := range CFG.Dirs {
		dir, e := os.ReadDir(dirname)
		try_with_exitstatus(e, 1)

		for _, x := range dir {
			file, e := os.ReadFile(dirname + "/" + x.Name())
			try_with_exitstatus(e, 1)
			Templates[x.Name()] = string(file)
		}
	}
}

/* PARSING HELPERS */
func ParseMedia(media devianter.Media) string {
	url := devianter.UrlFromMedia(media)
	if len(url) != 0 && CFG.Proxy {
		url = url[21:]
		dot := strings.Index(url, ".")

		return UrlBuilder("media", "file", url[:dot], url[dot+11:])
	}
	return url
}

func ConvertDeviantArtUrlToSkunkyArt(url string) (output string) {
	if len(url) > 32 && url[27:32] != "stash" {
		url = url[27:]
		toart := strings.Index(url, "/art/")
		if toart != -1 {
			output = UrlBuilder("post", url[:toart], url[toart+5:])
		}
	}
	return
}

func BuildUserPlate(name string) string {
	var htm strings.Builder
	htm.WriteString(`<div class="user-plate"><img src="`)
	htm.WriteString(UrlBuilder("media", "emojitar", name, "?type=a"))
	htm.WriteString(`"><a href="`)
	htm.WriteString(UrlBuilder("group_user", "?type=about&q=", name))
	htm.WriteString(`">`)
	htm.WriteString(name)
	htm.WriteString(`</a></div>`)
	return htm.String()
}

func GetValueOfTag(t *html.Tokenizer) string {
	for tt := t.Next(); ; {
		switch tt {
		default:
			return ""
		case html.TextToken:
			return string(t.Text())
		}
	}
}

// навигация по страницам
type DeviationList struct {
	Pages int
	More  bool
}

// FIXME: на некоротрых артах первая страница может вызывать полное отсутствие панели навигации.
func (s skunkyart) NavBase(c DeviationList) string {
	// TODO: сделать понятнее
	// навигация по страницам
	var list strings.Builder
	list.WriteString("<br>")
	p := s.Page

	// функция для генерации ссылок
	prevrev := func(msg string, page int, onpage bool) {
		if !onpage {
			list.WriteString(`<a href="?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(" ")
		}
	}

	// вперёд-назад
	if p > 1 {
		prevrev("<= Prev |", p-1, false)
	} else {
		p = 1
	}

	if c.Pages > 0 {
		// назад
		for x := p - 6; x < p && x > 0; x++ {
			prevrev(strconv.Itoa(x), x, false)
		}

		// вперёд
		for x := p; x <= p+6 && c.Pages > p+6; x++ {
			if x == p {
				prevrev("", x, true)
				x++
			}

			if x > p {
				prevrev(strconv.Itoa(x), x, false)
			}
		}
	}

	// вперёд-назад
	if c.More {
		prevrev("| Next =>", p+1, false)
	}

	return list.String()
}