Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

画像の存在しない戦歴にはog:imageを適用 #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions app/server/achievements_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down