Commit 39ff9bb770

39ff9bb77071facc2db9c98cff455a55de095025

parent: 745c7f3445

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 19:09 UTC

Add Qlty config; split UrlFromMedia

fitPath builds the resize path and shrinkToward replaces the closure.
Output matches the previous version on 200,000 random inputs.

Layout: unified · split

.qlty/.gitignore added +7
@@ -0,0 +1,7 @@
1*
2!configs
3!configs/**
4!hooks
5!hooks/**
6!qlty.toml
7!.gitignore
.qlty/qlty.toml added +44
@@ -0,0 +1,44 @@
1config_version = "0"
2
3exclude_patterns = [
4 "**/vendor/**",
5 "**/node_modules/**",
6 "**/testdata/**",
7 "**/dist/**",
8 "**/.build/**",
9 "**/build/**",
10 "**/*.min.*",
11 "**/generated/**",
12 ".claude/**",
13 "**/Pods/**",
14]
15
16test_patterns = [
17 "**/test/**",
18 "**/tests/**",
19 "**/Tests/**",
20 "**/*_test.*",
21 "**/test_*.*",
22 "**/*.test.*",
23 "**/*Tests.swift",
24 "e2e/**",
25]
26
27[[source]]
28name = "default"
29default = true
30
31[[plugin]]
32name = "osv-scanner"
33
34[[plugin]]
35name = "trufflehog"
36
37# Early returns are guard clauses, which keep the happy path unindented.
38[smells.return_statements]
39enabled = false
40
41# Total file complexity is the sum of its functions, which function_complexity
42# already checks.
43[smells.file_complexity]
44enabled = false
deviantion.go +35 −28
@@ -129,35 +129,18 @@ type Post struct {
129129func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename string) {
130130 var url strings.Builder
131131
132 subtractWidthHeight := func(to int, target ...*int) {
133 for i, l := 0, len(target); i < l; i++ {
134 for x := *target[i]; x > to; x -= to {
135 *target[i] = x
136 }
137 }
138 }
139
140132 for _, t := range m.Types {
141 if t.T == "fullview" {
142 url.WriteString(m.BaseUri)
143 if l := len(m.BaseUri); l != 0 && (m.BaseUri[l-3:] != "gif" && t.W*t.H < 33177600) {
144 if len(thumb) != 0 {
145 subtractWidthHeight(thumb[0], &t.W, &t.H)
146 }
147 wellFormattedFilename = m.Name + m.BaseUri[l-4:]
148
149 url.WriteString("/v1/fit/w_")
150 url.WriteString(strconv.Itoa(t.W))
151 url.WriteString(",h_")
152 url.WriteString(strconv.Itoa(t.H))
153 url.WriteString("/")
154 url.WriteString(wellFormattedFilename)
155
156 }
157 if len(m.Token) > 0 {
158 url.WriteString("?token=")
159 url.WriteString(m.Token[0])
160 }
133 if t.T != "fullview" {
134 continue
135 }
136 url.WriteString(m.BaseUri)
137 if path, name, ok := fitPath(m, t.W, t.H, thumb); ok {
138 wellFormattedFilename = name
139 url.WriteString(path)
140 }
141 if len(m.Token) > 0 {
142 url.WriteString("?token=")
143 url.WriteString(m.Token[0])
161144 }
162145 }
163146
@@ -166,6 +149,30 @@ func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename strin
166149 return
167150}
168151
152// fitPath returns the resize path for a w by h rendition of m and the filename
153// it is served under. ok is false for GIFs and for images DeviantArt's resizer
154// refuses.
155func fitPath(m Media, w, h int, thumb []int) (path, name string, ok bool) {
156 l := len(m.BaseUri)
157 if l == 0 || m.BaseUri[l-3:] == "gif" || w*h >= 33177600 {
158 return "", "", false
159 }
160 if len(thumb) != 0 {
161 w = shrinkToward(w, thumb[0])
162 h = shrinkToward(h, thumb[0])
163 }
164 name = m.Name + m.BaseUri[l-4:]
165 return "/v1/fit/w_" + strconv.Itoa(w) + ",h_" + strconv.Itoa(h) + "/" + name, name, true
166}
167
168// shrinkToward steps v down by to while it stays above to.
169func shrinkToward(v, to int) int {
170 for x := v; x > to; x -= to {
171 v = x
172 }
173 return v
174}
175
169176// GetDeviation retrieves a single deviation by its numeric ID and its author's
170177// username. Both are required: the endpoint will not resolve an ID alone. They
171178// appear in a deviation's page URL, which ends in a slug of the form