|
@@ -7,14 +7,17 @@ import (
|
|
|
setting "icloudapp.cn/tools/config"
|
|
|
"icloudapp.cn/tools/entity"
|
|
|
"icloudapp.cn/tools/model"
|
|
|
+ "icloudapp.cn/tools/util"
|
|
|
"icloudapp.cn/tools/util/image"
|
|
|
+ utString "icloudapp.cn/tools/util/string"
|
|
|
"os"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
func Preview(ctx *gin.Context) {
|
|
|
file := ctx.Query("file")
|
|
|
if file == "" {
|
|
|
- ResponseError(ctx, entity.CodePageNotFound)
|
|
|
+ entity.ResponseError(ctx, entity.CodePageNotFound)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -26,14 +29,14 @@ func Preview(ctx *gin.Context) {
|
|
|
fileWm, err := thumb.Thumb()
|
|
|
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
defer fileWm.Destroy()
|
|
|
//不写入文件,直接返回给浏览器
|
|
|
_, err = ctx.Writer.Write(fileWm.GetImageBlob())
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -41,14 +44,14 @@ func Preview(ctx *gin.Context) {
|
|
|
func Image(ctx *gin.Context) {
|
|
|
jsonByte, err := os.ReadFile("data.json")
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
+ entity.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{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -56,12 +59,12 @@ func Image(ctx *gin.Context) {
|
|
|
|
|
|
file, err := poster.Create(posterJson)
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
|
|
|
+ entity.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{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
defer file.Destroy()
|
|
@@ -69,31 +72,22 @@ func Image(ctx *gin.Context) {
|
|
|
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{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
//不写入文件,直接返回给浏览器
|
|
|
_, err = ctx.Writer.Write(file.GetImageBlob())
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
+ entity.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))
|
|
|
- */
|
|
|
}
|
|
|
|
|
|
+// Cut 裁切指定的图片
|
|
|
func Cut(ctx *gin.Context) {
|
|
|
file := ctx.Query("file")
|
|
|
if file == "" {
|
|
|
- ResponseError(ctx, entity.CodePageNotFound)
|
|
|
+ entity.ResponseError(ctx, entity.CodePageNotFound)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -105,14 +99,14 @@ func Cut(ctx *gin.Context) {
|
|
|
fileWm, err := thumb.Cut("center")
|
|
|
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
defer fileWm.Destroy()
|
|
|
//不写入文件,直接返回给浏览器
|
|
|
_, err = ctx.Writer.Write(fileWm.GetImageBlob())
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -121,17 +115,18 @@ func SavePoster(ctx *gin.Context) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+// PosterInfo 获取指定posterID的海报信息
|
|
|
func PosterInfo(ctx *gin.Context) {
|
|
|
var request entity.PosterInfoRequest
|
|
|
if err := ctx.ShouldBind(&request); err != nil {
|
|
|
- ResponseError(ctx, entity.CodeInvalidParam)
|
|
|
+ entity.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)
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
|
|
|
return
|
|
|
}
|
|
|
var api entity.UserPosterInfoForApi
|
|
@@ -139,57 +134,85 @@ func PosterInfo(ctx *gin.Context) {
|
|
|
api.Msg = info.Msg
|
|
|
api.Body.Poster = info.Body
|
|
|
|
|
|
- ResponseNormal(ctx, api.Code, api.Msg, api.Body)
|
|
|
+ entity.ResponseNormal(ctx, api.Code, api.Msg, api.Body)
|
|
|
}
|
|
|
|
|
|
+// DrawImage 生成指定posterID的海报
|
|
|
func DrawImage(ctx *gin.Context) {
|
|
|
+ sinceStart := time.Now()
|
|
|
var request entity.PosterInfoRequest
|
|
|
if err := ctx.ShouldBind(&request); err != nil {
|
|
|
- ResponseError(ctx, entity.CodeInvalidParam)
|
|
|
+ entity.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)
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
|
|
|
return
|
|
|
}
|
|
|
+ spendTime := time.Since(sinceStart)
|
|
|
+ fmt.Println("查询数据耗时:", spendTime)
|
|
|
|
|
|
+ startTime := time.Now()
|
|
|
var posterJson image.PosterJson
|
|
|
err = json.Unmarshal([]byte(info.Body.PosterJson), &posterJson)
|
|
|
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
+ endTime := time.Since(startTime)
|
|
|
+ fmt.Println("解压数据耗时:", endTime)
|
|
|
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{}{})
|
|
|
+ entity.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{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
defer file.Destroy()
|
|
|
+ defer util.TimeCost("渲染数据消耗")()
|
|
|
if posterJson.Quality == 0 {
|
|
|
posterJson.Quality = 50
|
|
|
}
|
|
|
- posterJson.Quality = 30
|
|
|
if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//不写入文件,直接返回给浏览器
|
|
|
_, err = ctx.Writer.Write(file.GetImageBlob())
|
|
|
if err != nil {
|
|
|
- ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Posters 通过用户ID获取用户名下的所有海报
|
|
|
+func Posters(ctx *gin.Context) {
|
|
|
+ uid := utString.ConvertInt64(ctx.GetString("uid"))
|
|
|
+
|
|
|
+ if uid == 0 {
|
|
|
+ entity.ResponseNormal(ctx, entity.CodeDenied, "请登录后再试", []interface{}{})
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ mUserPoster := model.NewUserPoster(ctx)
|
|
|
+
|
|
|
+ simpleLists := mUserPoster.SimpleLists(uid)
|
|
|
+ userPostersForApi := &entity.UserPostersForApi{
|
|
|
+ Code: 0,
|
|
|
+ Msg: "success",
|
|
|
+ Body: map[string][]entity.PostersListForApi{
|
|
|
+ "posters": simpleLists,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ entity.ResponseSuccess(ctx, userPostersForApi)
|
|
|
}
|