| 18 | ) |
| 19 | |
| 20 | func CDNDownscaler(c *fiber.Ctx) error { |
| 21 | imageURL := c.Query("url") |
| 22 | widthStr := c.Query("width") |
| 23 | heightStr := c.Query("height") |
| 24 | resizeOption := c.Query("resize") |
| 25 | maintainAspect := false |
| 26 | |
| 27 | // Handle URL unescaping first if it's not a direct DID request |
| 28 | if c.Params("did") != "" { |
| 29 | did := c.Params("did") |
| 30 | link := c.Params("link") |
| 31 | size := c.Params("size") |
| 32 | |
| 33 | // Remove any file extension from link |
| 34 | link = strings.TrimSuffix(link, filepath.Ext(link)) |
| 35 | |
| 36 | imageURL = "https://cdn.bsky.app/img/feed_thumbnail/plain/" + did + "/" + link + "@jpeg" |
| 37 | |
| 38 | // If size is provided as a path parameter, treat it as a suffix |
| 39 | if size != "" { |
| 40 | if strings.HasPrefix(size, "mobile") || strings.HasPrefix(size, "web") || strings.HasPrefix(size, "ipad") { |
| 41 | size = "/" + size |
| 42 | } else { |
| 43 | size = ":" + size |
| 44 | } |
| 45 | // This will be processed by the suffix handling below |
| 46 | imageURL = imageURL + size |
| 47 | } |
| 48 | } else { |
| 49 | unescapedURL, err := url.QueryUnescape(imageURL) |
| 50 | if err != nil { |
| 51 | return c.Status(fiber.StatusBadRequest).SendString("Invalid URL") |
| 52 | } |
| 53 | imageURL = unescapedURL |
| 54 | } |
| 55 | |
| 56 | // Check suffixes and set dimensions before any other URL validation |
| 57 | suffixes := map[string]struct { |
| 58 | width string |
| 59 | height string |
| 60 | aspect bool |
| 61 | }{ |
| 62 | ":large": {"", "", true}, |
| 63 | ":small": {"340", "480", true}, |
| 64 | ":medium": {"600", "1200", true}, |
| 65 | ":thumb": {"150", "150", false}, |
| 66 | ":profile_bigger": {"128", "128", false}, |
| 67 | ":profile_normal": {"48", "48", false}, |
| 68 | ":profile_mini": {"24", "24", false}, |
| 69 | "/mobile_retina": {"620", "320", false}, |
| 70 | "/mobile": {"320", "160", false}, |
| 71 | "/ipad": {"626", "313", false}, |
| 72 | "/ipad_retina": {"1252", "626", false}, |
| 73 | "/web": {"520", "260", false}, |
| 74 | "/web_retina": {"1040", "520", false}, |
| 75 | } |
| 76 | |
| 77 | // Check for suffixes and apply dimensions |