Skip to content

Commit 1bfd45d

Browse files
committed
set storage
1 parent 227e513 commit 1bfd45d

7 files changed

Lines changed: 73 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.DS_Store
22
tmp
33
dist
4-
repo.png
4+
repo.png

external/storage.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package external
2+
3+
import (
4+
"image"
5+
"image/png"
6+
"os"
7+
"strings"
8+
9+
"github.com/aws/aws-sdk-go/aws"
10+
"github.com/aws/aws-sdk-go/aws/endpoints"
11+
"github.com/aws/aws-sdk-go/aws/session"
12+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
13+
"github.com/pkg/errors"
14+
)
15+
16+
// GetS3Uploader set s3 config.
17+
func GetS3Uploader() *s3manager.Uploader {
18+
conf := &aws.Config{
19+
Region: aws.String(endpoints.ApNortheast1RegionID),
20+
}
21+
sess := session.New(conf)
22+
svc := s3manager.NewUploader(sess)
23+
return svc
24+
}
25+
26+
// UploadImg upload image to s3.
27+
func (r *Repository) UploadImg(img image.Image, Name string) (string, error) {
28+
fileName := strings.Split(Name, "/")[1]
29+
filePath := fileName + ".png"
30+
file, err := os.Create(filePath)
31+
if err != nil {
32+
return "", errors.Wrap(err, "failed to create file")
33+
}
34+
defer file.Close()
35+
36+
err = png.Encode(file, img)
37+
if err != nil {
38+
return "", errors.Wrap(err, "failed to encode to file")
39+
}
40+
file, err = os.Open(filePath)
41+
if err != nil {
42+
return "", errors.Wrap(err, "failed to open file")
43+
}
44+
45+
svc := GetS3Uploader()
46+
result, err := svc.Upload(&s3manager.UploadInput{
47+
Bucket: aws.String("github-link-card"),
48+
Key: aws.String(Name + ".png"),
49+
Body: file,
50+
})
51+
if err != nil {
52+
return "", errors.Wrap(err, "failed to upload file")
53+
}
54+
err = os.Remove(fileName + ".png")
55+
if err != nil {
56+
return "", errors.Wrap(err, "failed to delete file")
57+
}
58+
return result.Location, nil
59+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/po3rin/github_link_creator
22

33
require (
4+
github.com/aws/aws-sdk-go v1.16.26
45
github.com/gin-contrib/sse v0.0.0-20190125020943-a7658810eb74 // indirect
56
github.com/gin-gonic/gin v1.3.0
67
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/aws/aws-sdk-go v1.16.26 h1:GWkl3rkRO/JGRTWoLLIqwf7AWC4/W/1hMOUZqmX0js4=
2+
github.com/aws/aws-sdk-go v1.16.26/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -9,6 +11,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
911
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
1012
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
1113
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
14+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
15+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
1216
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1317
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
1418
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=

handler/image.go renamed to handler/handler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package handler
33
import (
44
"context"
55
"fmt"
6-
"image/png"
76
"net/http"
87
"time"
98

@@ -14,7 +13,7 @@ import (
1413
)
1514

1615
// GetImage create GitHub Card Image.
17-
func (h *Handler) GetImage(c *gin.Context) {
16+
func (h *Handler) GetCode(c *gin.Context) {
1817
ctx, cancel := context.WithTimeout(c.Request.Context(), env.Timeout*time.Second)
1918
defer cancel()
2019
doneCh := make(chan struct{})
@@ -28,11 +27,15 @@ func (h *Handler) GetImage(c *gin.Context) {
2827
c.JSON(http.StatusInternalServerError, err.Error())
2928
return
3029
}
31-
err = png.Encode(c.Writer, img)
30+
location, err := h.Repo.UploadImg(img, userName+"/"+repoName)
3231
if err != nil {
3332
c.JSON(http.StatusInternalServerError, err.Error())
3433
return
3534
}
35+
result := fmt.Sprintf(`<a href="https://github.com/%v/%v"><img src="%v" width="460px"></a>`, userName, repoName, location)
36+
c.JSON(http.StatusOK, gin.H{
37+
"value": result,
38+
})
3639
doneCh <- struct{}{}
3740
}()
3841

handler/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type Repo interface {
1717
GetRepoData(ctx context.Context, userName string, repoName string) (*entity.Repo, error)
1818
GetUserData(ctx context.Context, userName string) (*entity.User, error)
1919
GetUserImage(ctx context.Context, avatarURL string) (image.Image, error)
20+
UploadImg(img image.Image, Name string) (string, error)
2021
}

infrastructure/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ func NewRouter() *Router {
2121
func (r *Router) InitRouter() *gin.Engine {
2222
router := gin.Default()
2323
router.GET("/v1/health", r.Handler.HealthCheck)
24-
router.GET("/v1/images/:user/:repo", r.Handler.GetImage)
24+
router.GET("/v1/images/:user/:repo", r.Handler.GetCode)
2525
return router
2626
}

0 commit comments

Comments
 (0)