| @@ -129,35 +129,18 @@ type Post struct { |
| 129 | func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename string) { |
129 | func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename string) { |
| 130 | var url strings.Builder |
130 | var url strings.Builder |
| 131 | |
131 | |
| 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 | |
| |
| 140 | for _, t := range m.Types { |
132 | for _, t := range m.Types { |
| 141 | if t.T == "fullview" { |
133 | if t.T != "fullview" { |
| 142 | url.WriteString(m.BaseUri) |
134 | continue |
| 143 | if l := len(m.BaseUri); l != 0 && (m.BaseUri[l-3:] != "gif" && t.W*t.H < 33177600) { |
135 | } |
| 144 | if len(thumb) != 0 { |
136 | url.WriteString(m.BaseUri) |
| 145 | subtractWidthHeight(thumb[0], &t.W, &t.H) |
137 | if path, name, ok := fitPath(m, t.W, t.H, thumb); ok { |
| 146 | } |
138 | wellFormattedFilename = name |
| 147 | wellFormattedFilename = m.Name + m.BaseUri[l-4:] |
139 | url.WriteString(path) |
| 148 | |
140 | } |
| 149 | url.WriteString("/v1/fit/w_") |
141 | if len(m.Token) > 0 { |
| 150 | url.WriteString(strconv.Itoa(t.W)) |
142 | url.WriteString("?token=") |
| 151 | url.WriteString(",h_") |
143 | url.WriteString(m.Token[0]) |
| 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 | } |
| |
| 161 | } |
144 | } |
| 162 | } |
145 | } |
| 163 | |
146 | |
| @@ -166,6 +149,30 @@ func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename strin |
| 166 | return |
149 | return |
| 167 | } |
150 | } |
| 168 | |
151 | |
| |
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. |
| |
155 | func 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. |
| |
169 | func shrinkToward(v, to int) int { |
| |
170 | for x := v; x > to; x -= to { |
| |
171 | v = x |
| |
172 | } |
| |
173 | return v |
| |
174 | } |
| |
175 | |
| 169 | // GetDeviation retrieves a single deviation by its numeric ID and its author's |
176 | // GetDeviation retrieves a single deviation by its numeric ID and its author's |
| 170 | // username. Both are required: the endpoint will not resolve an ID alone. They |
177 | // username. Both are required: the endpoint will not resolve an ID alone. They |
| 171 | // appear in a deviation's page URL, which ends in a slug of the form |
178 | // appear in a deviation's page URL, which ends in a slug of the form |