MCPcopy Create free account
hub / github.com/Preloading/TwitterAPIBridge / CreateShortLink

Function CreateShortLink

twitterv1/imagelink.go:57–87  ·  view source on GitHub ↗
(originalPath string, prefix string)

Source from the content-addressed store, hash-verified

55}
56
57func CreateShortLink(originalPath string, prefix string) (string, error) {
58 // Check cache first
59 if code, ok := cache.Get(originalPath); ok {
60 return code, nil
61 }
62
63 // Create the URL somewhat predictably
64 h := fnv.New64a()
65 h.Write([]byte(originalPath))
66 hash := h.Sum64()
67
68 // Convert hash to base62
69 shortCode := toBase62(hash)
70
71 if len(shortCode) < 6 {
72 shortCode = string(base62Chars[0]) + shortCode
73 } else if len(shortCode) > 6 {
74 shortCode = shortCode[:6]
75 }
76
77 shortCode = prefix + shortCode
78
79 err := db_controller.StoreShortLink(shortCode, originalPath)
80 if err != nil {
81 return "", err
82 }
83
84 // Cache the result
85 cache.Set(originalPath, shortCode)
86 return shortCode, nil
87}
88
89func RedirectToLink(c *fiber.Ctx) error {
90

Callers 1

TranslatePostToTweetFunction · 0.85

Calls 4

StoreShortLinkFunction · 0.92
toBase62Function · 0.85
GetMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected