diff --git a/app/server/achievements_server.go b/app/server/achievements_server.go index 2e4532ae..e8691631 100644 --- a/app/server/achievements_server.go +++ b/app/server/achievements_server.go @@ -3,8 +3,11 @@ package server import ( "context" "database/sql" + "io/ioutil" + "net/http" "time" + "github.com/dyatlov/go-opengraph/opengraph" "github.com/gogo/protobuf/types" "github.com/golang/protobuf/ptypes/empty" "github.com/izumin5210/grapi/pkg/grapiserver" @@ -133,6 +136,59 @@ func (s *achievementServiceServerImpl) UpdateAchievement(ctx context.Context, re } ach := req.GetAchievement() + + // ここの間かな + if ach.ImageUrl == "" { + // 非同期処理 + go func() { + resp, err := http.Get(ach.Url) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + + defer func() { _ = resp.Body.Close() }() + + og := opengraph.NewOpenGraph() + err = og.ProcessHTML(resp.Body) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + + images := og.Images + if len(images) == 0 { + return + } + + image := images[0] + imgResp, err := http.Get(image.SecureURL) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + + imageByte, err := ioutil.ReadAll(imgResp.Body) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + + is := s.ImageStore(context.Background()) + name, err := is.CreateImage(imageByte) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + + _, _, err = s.AchievementStore(context.Background()).UpdateAchievementImage(int64(req.Achievement.AchievementId), name) + if err != nil { + grpclog.Errorf("ogImage: %+v", err) + return + } + }() + } + rec := &record.Achievement{ ID: int64(ach.AchievementId), Title: ach.GetTitle(), diff --git a/go.mod b/go.mod index 34e2cdf8..f491a571 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/PuerkitoBio/goquery v1.5.0 // indirect + github.com/dyatlov/go-opengraph v0.0.0-20180429202543-816b6608b3c8 github.com/go-redis/redis v6.14.1+incompatible github.com/gobuffalo/envy v1.7.0 // indirect github.com/gobuffalo/packr/v2 v2.1.0 diff --git a/go.sum b/go.sum index e8142384..f83ba3f4 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dyatlov/go-opengraph v0.0.0-20180429202543-816b6608b3c8 h1:6muCmMJat6z7qptVrIf/+OWPxsjAfvhw5/6t+FwEkgg= +github.com/dyatlov/go-opengraph v0.0.0-20180429202543-816b6608b3c8/go.mod h1:nYia/MIs9OyvXXYboPmNOj0gVWo97Wx0sde+ZuKkoM4= github.com/elazarl/goproxy v0.0.0-20181003060214-f58a169a71a5/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=