forked from ahmdrz/goinsta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortid.go
31 lines (27 loc) · 810 Bytes
/
shortid.go
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
package goinsta
import (
"fmt"
"strconv"
"strings"
)
func leftPad2Len(s string, padStr string, overallLen int) string {
var padCountInt int
padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = strings.Repeat(padStr, padCountInt) + s
return retStr[(len(retStr) - overallLen):]
}
const base64UrlCharmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
func MediaIDFromShortID(code string) (string, error) {
strID := ""
for i := 0; i < len(code); i++ {
base64 := strings.Index(base64UrlCharmap, string(code[i]))
str2bin := strconv.FormatInt(int64(base64), 2)
sixbits := leftPad2Len(str2bin, "0", 6)
strID = strID + sixbits
}
result, err := strconv.ParseInt(strID, 2, 64)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", result), nil
}