Skip to content

Commit

Permalink
add article poster feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Jul 3, 2018
1 parent 5c96763 commit 8a7581f
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
.DS_Store

/runtime
runtime/*
!runtime/qrcode
!runtime/qrcode/bg.jpg
1 change: 1 addition & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ImageMaxSize = 5
ImageAllowExts = .jpg,.jpeg,.png

ExportSavePath = export/
QrCodeSavePath = qrcode/

LogSavePath = logs/
LogSaveName = log
Expand Down
1 change: 1 addition & 0 deletions pkg/e/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
ERROR_COUNT_ARTICLE_FAIL = 10016
ERROR_GET_ARTICLES_FAIL = 10017
ERROR_GET_ARTICLE_FAIL = 10018
ERROR_GEN_ARTICLE_POSTER_FAIL = 10019

ERROR_AUTH_CHECK_TOKEN_FAIL = 20001
ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002
Expand Down
1 change: 1 addition & 0 deletions pkg/e/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var MsgFlags = map[int]string{
ERROR_COUNT_ARTICLE_FAIL: "统计文章失败",
ERROR_GET_ARTICLES_FAIL: "获取多个文章失败",
ERROR_GET_ARTICLE_FAIL: "获取单个文章失败",
ERROR_GEN_ARTICLE_POSTER_FAIL: "生成文章海报失败",
ERROR_AUTH_CHECK_TOKEN_FAIL: "Token鉴权失败",
ERROR_AUTH_CHECK_TOKEN_TIMEOUT: "Token已超时",
ERROR_AUTH_TOKEN: "Token生成失败",
Expand Down
26 changes: 26 additions & 0 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"mime/multipart"
"os"
"path"
"fmt"
)

func GetSize(f multipart.File) (int, error) {
Expand Down Expand Up @@ -56,3 +57,28 @@ func Open(name string, flag int, perm os.FileMode) (*os.File, error) {

return f, nil
}

func MustOpen(fileName, filePath string) (*os.File, error) {
dir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("os.Getwd err: %v", err)
}

src := dir + "/" + filePath
perm := CheckPermission(src)
if perm == true {
return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
}

err = IsNotExistMkDir(src)
if err != nil {
return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
}

f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("Fail to OpenFile :%v", err)
}

return f, nil
}
50 changes: 24 additions & 26 deletions pkg/logging/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package logging

import (
"fmt"
"os"
"time"

"github.com/EDDYCJY/go-gin-example/pkg/file"
"github.com/EDDYCJY/go-gin-example/pkg/setting"
)

Expand All @@ -21,27 +19,27 @@ func getLogFileName() string {
)
}

func openLogFile(fileName, filePath string) (*os.File, error) {
dir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("os.Getwd err: %v", err)
}

src := dir + "/" + filePath
perm := file.CheckPermission(src)
if perm == true {
return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
}

err = file.IsNotExistMkDir(src)
if err != nil {
return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
}

f, err := file.Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, fmt.Errorf("Fail to OpenFile :%v", err)
}

return f, nil
}
//func openLogFile(fileName, filePath string) (*os.File, error) {
// dir, err := os.Getwd()
// if err != nil {
// return nil, fmt.Errorf("os.Getwd err: %v", err)
// }
//
// src := dir + "/" + filePath
// perm := file.CheckPermission(src)
// if perm == true {
// return nil, fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
// }
//
// err = file.IsNotExistMkDir(src)
// if err != nil {
// return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
// }
//
// f, err := file.Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
// if err != nil {
// return nil, fmt.Errorf("Fail to OpenFile :%v", err)
// }
//
// return f, nil
//}
3 changes: 2 additions & 1 deletion pkg/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"github.com/EDDYCJY/go-gin-example/pkg/file"
)

type Level int
Expand Down Expand Up @@ -33,7 +34,7 @@ func Setup() {
var err error
filePath := getLogFilePath()
fileName := getLogFileName()
F, err = openLogFile(fileName, filePath)
F, err = file.MustOpen(fileName, filePath)
if err != nil {
log.Fatalln(err)
}
Expand Down
94 changes: 94 additions & 0 deletions pkg/qrcode/qrcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package qrcode

import (
"image/jpeg"

"github.com/boombuler/barcode/qr"
"github.com/boombuler/barcode"

"github.com/EDDYCJY/go-gin-example/pkg/util"
"github.com/EDDYCJY/go-gin-example/pkg/setting"
"github.com/EDDYCJY/go-gin-example/pkg/file"
)

type QrCode struct {
URL string
Width int
Height int
Ext string
Level qr.ErrorCorrectionLevel
Mode qr.Encoding
}

const (
EXT_JPG = ".jpg"
)

func NewQrCode(url string, width, height int, level qr.ErrorCorrectionLevel, mode qr.Encoding) *QrCode {
return &QrCode{
URL: url,
Width: width,
Height: height,
Level: level,
Mode: mode,
Ext: EXT_JPG,
}
}

func GetQrCodePath() string {
return setting.AppSetting.QrCodeSavePath
}

func GetQrCodeFullPath() string {
return setting.AppSetting.RuntimeRootPath + setting.AppSetting.QrCodeSavePath
}

func GetQrCodeFullUrl(name string) string {
return setting.AppSetting.PrefixUrl + "/" + GetQrCodePath() + name
}

func GetQrCodeFileName(value string) string {
return util.EncodeMD5(value)
}

func (q *QrCode) GetQrCodeExt() string {
return q.Ext
}

func (q *QrCode) CheckEncode(path string) bool {
src := path + GetQrCodeFileName(q.URL) + q.GetQrCodeExt()
if file.CheckNotExist(src) == true {
return false
}

return true
}

func (q *QrCode) Encode(path string) (string, string, error) {
name := GetQrCodeFileName(q.URL) + q.GetQrCodeExt()
src := path + name
if file.CheckNotExist(src) == true {
code, err := qr.Encode(q.URL, q.Level, q.Mode)
if err != nil {
return "", "", err
}

code, err = barcode.Scale(code, q.Width, q.Height)
if err != nil {
return "", "", err
}

f, err := file.MustOpen(name, path)
if err != nil {
return "", "", err
}
defer f.Close()

err = jpeg.Encode(f, code, nil)
if err != nil {
return "", "", err
}
}

return name, path, nil
}
1 change: 1 addition & 0 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type App struct {
ImageAllowExts []string

ExportSavePath string
QrCodeSavePath string

LogSavePath string
LogSaveName string
Expand Down
39 changes: 39 additions & 0 deletions routers/api/v1/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/util"
"github.com/EDDYCJY/go-gin-example/service/article_service"
"github.com/EDDYCJY/go-gin-example/service/tag_service"
"github.com/EDDYCJY/go-gin-example/pkg/qrcode"
"github.com/boombuler/barcode/qr"
)

// @Summary 获取单个文章
Expand Down Expand Up @@ -293,3 +295,40 @@ func DeleteArticle(c *gin.Context) {

appG.Response(http.StatusOK, e.SUCCESS, nil)
}

const (
QRCODE_URL = "https://github.com/EDDYCJY/blog#gin%E7%B3%BB%E5%88%97%E7%9B%AE%E5%BD%95"
)

func GenerateArticlePoster(c *gin.Context) {
appG := app.Gin{c}
article := &article_service.Article{}
qr := qrcode.NewQrCode(QRCODE_URL, 300, 300, qr.M, qr.Auto) // 目前写死 gin 系列路径,可自行增加业务逻辑
posterName := article_service.GetPosterFlag() + "-" + qrcode.GetQrCodeFileName(qr.URL) + qr.GetQrCodeExt()
articlePoster := article_service.NewArticlePoster(posterName, article, qr)
articlePosterBgService := article_service.NewArticlePosterBg(
"bg.jpg",
articlePoster,
&article_service.Rect{
X0: 0,
Y0: 0,
X1: 550,
Y1: 700,
},
&article_service.Pt{
X: 125,
Y: 298,
},
)

_, filePath, err := articlePosterBgService.Generate()
if err != nil {
appG.Response(http.StatusOK, e.ERROR_GEN_ARTICLE_POSTER_FAIL, nil)
return
}

appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
"poster_url" : qrcode.GetQrCodeFullUrl(posterName),
"poster_save_url" : filePath + posterName,
})
}
4 changes: 4 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/upload"
"github.com/EDDYCJY/go-gin-example/routers/api"
"github.com/EDDYCJY/go-gin-example/routers/api/v1"
"github.com/EDDYCJY/go-gin-example/pkg/qrcode"
)

func InitRouter() *gin.Engine {
Expand All @@ -27,6 +28,7 @@ func InitRouter() *gin.Engine {

r.StaticFS("/export", http.Dir(export.GetExcelFullPath()))
r.StaticFS("/upload/images", http.Dir(upload.GetImageFullPath()))
r.StaticFS("/qrcode", http.Dir(qrcode.GetQrCodeFullPath()))

r.GET("/auth", api.GetAuth)
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
Expand Down Expand Up @@ -58,6 +60,8 @@ func InitRouter() *gin.Engine {
apiv1.PUT("/articles/:id", v1.EditArticle)
//删除指定文章
apiv1.DELETE("/articles/:id", v1.DeleteArticle)
//生成文章海报
apiv1.POST("/articles/poster/generate", v1.GenerateArticlePoster)
}

return r
Expand Down
Binary file added runtime/qrcode/bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8a7581f

Please sign in to comment.