123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package controller
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/gographics/gmagick"
- setting "icloudapp.cn/tools/config"
- "icloudapp.cn/tools/entity"
- "icloudapp.cn/tools/model"
- "icloudapp.cn/tools/util/image"
- "os"
- )
- func Preview(ctx *gin.Context) {
- file := ctx.Query("file")
- if file == "" {
- ResponseError(ctx, entity.CodePageNotFound)
- return
- }
- path, _ := os.Getwd()
- gmagick.Initialize()
- defer gmagick.Terminate()
- thumb := image.NewImage(setting.Conf.CacheDir)
- file, err := thumb.Thumb(fmt.Sprintf("%s/%s", path, file), image.NewSize(300, 300))
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- stream, err := os.ReadFile(file)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- ctx.Writer.WriteString(string(stream))
- }
- func Image(ctx *gin.Context) {
- jsonByte, err := os.ReadFile("data.json")
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- var posterJson image.PosterJson
- err = json.Unmarshal(jsonByte, &posterJson)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- poster := image.NewPoster(ctx)
- file, err := poster.Create(posterJson)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
- return
- }
- if err = file.SetImageFormat("PNG"); err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
- return
- }
- defer file.Destroy()
- if posterJson.Quality == 0 {
- posterJson.Quality = 50
- }
- if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
- return
- }
- //不写入文件,直接返回给浏览器
- _, err = ctx.Writer.Write(file.WriteImageBlob())
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
- return
- }
- /*
- stream, err := os.ReadFile(file)
- if err != nil {
- fmt.Println("read error")
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- ctx.Writer.WriteString(string(stream))
- */
- }
- func Cut(ctx *gin.Context) {
- file := ctx.Query("file")
- if file == "" {
- ResponseError(ctx, entity.CodePageNotFound)
- return
- }
- path, _ := os.Getwd()
- gmagick.Initialize()
- defer gmagick.Terminate()
- thumb := image.NewImage(setting.Conf.CacheDir)
- file, err := thumb.Cut(fmt.Sprintf("%s/%s", path, file), image.NewSize(400, 400), "center")
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- stream, err := os.ReadFile(file)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- ctx.Writer.WriteString(string(stream))
- }
- func SavePoster(ctx *gin.Context) {
- }
- func PosterInfo(ctx *gin.Context) {
- var request entity.PosterInfoRequest
- if err := ctx.ShouldBind(&request); err != nil {
- ResponseError(ctx, entity.CodeInvalidParam)
- return
- }
- sUserPoster := model.NewUserPoster(ctx)
- info, err := sUserPoster.Info(request.PosterID)
- if err != nil {
- ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
- return
- }
- var api entity.UserPosterInfoForApi
- api.Code = info.Code
- api.Msg = info.Msg
- api.Body.Poster = info.Body
- ResponseNormal(ctx, api.Code, api.Msg, api.Body)
- }
- func DrawImage(ctx *gin.Context) {
- var request entity.PosterInfoRequest
- if err := ctx.ShouldBind(&request); err != nil {
- ResponseError(ctx, entity.CodeInvalidParam)
- return
- }
- sUserPoster := model.NewUserPoster(ctx)
- info, err := sUserPoster.Info(request.PosterID)
- if err != nil {
- ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
- return
- }
- var posterJson image.PosterJson
- err = json.Unmarshal([]byte(info.Body.PosterJson), &posterJson)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- poster := image.NewPoster(ctx)
- file, err := poster.Create(posterJson)
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
- return
- }
- if err = file.SetImageFormat("PNG"); err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
- return
- }
- defer file.Destroy()
- if posterJson.Quality == 0 {
- posterJson.Quality = 50
- }
- if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
- return
- }
- //不写入文件,直接返回给浏览器
- _, err = ctx.Writer.Write(file.WriteImageBlob())
- if err != nil {
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
- return
- }
- }
|