gostart/icons.go

55 lines
896 B
Go
Raw Permalink Normal View History

2023-07-25 07:20:09 -06:00
package main
import (
2024-05-11 14:24:49 -06:00
"context"
2023-07-25 07:20:09 -06:00
"fmt"
"io"
"log"
"net/http"
"suah.dev/gostart/data"
)
func updateIcons() {
2024-05-11 14:24:49 -06:00
ctx := context.Background()
links, err := app.queries.GetAllLinks(ctx)
2023-07-25 07:20:09 -06:00
if err != nil {
log.Println("can't get links: ", err)
}
for _, link := range links {
fmt.Println(link.LogoUrl)
if link.LogoUrl == "" {
continue
}
resp, err := http.Get(link.LogoUrl)
if err != nil {
log.Println(err)
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
continue
}
err = resp.Body.Close()
if err != nil {
log.Println(err)
continue
}
contentType := resp.Header.Get("Content-Type")
2024-05-11 14:24:49 -06:00
err = app.queries.AddIcon(ctx, data.AddIconParams{
2023-07-25 07:20:09 -06:00
OwnerID: link.OwnerID,
LinkID: link.ID,
ContentType: contentType,
Data: body,
})
if err != nil {
log.Println("can't add icon: ", err)
}
}
}