forked from External/ergo
switch to redis pubsub for ipc
adjust commands to utilize channel names add new config variables fix mention race condition
This commit is contained in:
parent
64ebb1f480
commit
f4c03b6765
12 changed files with 220 additions and 195 deletions
|
|
@ -4,14 +4,6 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/cshum/imagor/imagorpath"
|
||||
"github.com/ergochat/irc-go/ircmsg"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -178,78 +170,3 @@ func BuildTokenLines(lineLen int, tokens []string, delim string) []string {
|
|||
}
|
||||
return tl.Lines()
|
||||
}
|
||||
|
||||
var urlRegex = regexp.MustCompile("https?:\\/\\/[\\w-]+(\\.[\\w-]+)+([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?")
|
||||
|
||||
func GenerateImagorSignaturesFromMessage(message *ircmsg.Message) string {
|
||||
line, err := message.Line()
|
||||
if err == nil {
|
||||
return GenerateImagorSignatures(line)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var secretKey = os.Getenv("IMAGOR_SECRET")
|
||||
var baseUrl = os.Getenv("IMAGOR_URL")
|
||||
|
||||
func GetUrlMime(url string) string {
|
||||
// hacky, should fix
|
||||
if !strings.Contains(url, "?") {
|
||||
url += "?"
|
||||
}
|
||||
params := imagorpath.Params{
|
||||
Image: url,
|
||||
Meta: true,
|
||||
}
|
||||
metaPath := imagorpath.Generate(params, imagorpath.NewHMACSigner(sha256.New, 0, secretKey))
|
||||
client := http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
resp, err := client.Get(baseUrl + metaPath)
|
||||
if err != nil {
|
||||
println("Failed on the initial get")
|
||||
println(err.Error())
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var meta map[string]interface{}
|
||||
err = json.NewDecoder(resp.Body).Decode(&meta)
|
||||
if err != nil {
|
||||
println("Failed on the JSON decode")
|
||||
return ""
|
||||
}
|
||||
contentType, valid := meta["format"].(string)
|
||||
fmt.Printf("%+v\n", meta)
|
||||
if !valid {
|
||||
println("No content type")
|
||||
return ""
|
||||
}
|
||||
return contentType
|
||||
}
|
||||
|
||||
// Process a message to add Imagor signatures
|
||||
func GenerateImagorSignatures(str string) string {
|
||||
urls := urlRegex.FindAllString(str, -1)
|
||||
var sigs []string
|
||||
for _, url := range urls {
|
||||
params := imagorpath.Params{
|
||||
Image: url,
|
||||
FitIn: true,
|
||||
Width: 600,
|
||||
Height: 600,
|
||||
}
|
||||
path := imagorpath.Generate(params, imagorpath.NewHMACSigner(sha256.New, 0, secretKey))
|
||||
signature := path[:strings.IndexByte(path, '/')]
|
||||
contentType := GetUrlMime(url)
|
||||
if contentType != "" {
|
||||
sigs = append(sigs, signature+"|"+strings.ReplaceAll(contentType, "/", "_"))
|
||||
} else {
|
||||
sigs = append(sigs, signature)
|
||||
}
|
||||
|
||||
}
|
||||
if len(sigs) > 0 {
|
||||
return strings.Join(sigs, ",")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue