123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package admin
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "gopkg.in/gographics/imagick.v3/imagick"
- setting "icloudapp.cn/tools/config"
- "icloudapp.cn/tools/entity"
- "icloudapp.cn/tools/model"
- sEntity "icloudapp.cn/tools/service/entity"
- utFile "icloudapp.cn/tools/util/file"
- utString "icloudapp.cn/tools/util/string"
- "time"
- )
- func Material(ctx *gin.Context) {
- file, err := ctx.FormFile("files")
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeFileNotExist, err.Error(), []interface{}{})
- return
- }
- upload := utFile.NewUpload(setting.Conf.Upload.Material)
- upload.SetAllowed(map[string]bool{
- ".jpg": true,
- ".png": true,
- ".gif": true,
- ".jpeg": true,
- })
- upload.SetHasHash(true)
- fileInfo, err := upload.Single(*file)
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeCreateDirFail, err.Error(), []interface{}{})
- }
- image := imagick.NewMagickWand()
- if err = image.ReadImage(fileInfo.File); err != nil {
- entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- }
- defer image.Destroy()
- size := fmt.Sprintf("%dx%d", image.GetImageWidth(), image.GetImageHeight())
- material := model.NewMMaterial(ctx)
- add, err := material.Add(&sEntity.Material{
- UID: utString.ConvertInt64(ctx.GetString("uid")),
- Name: fileInfo.Name,
- File: fileInfo.File,
- Size: size,
- FileSize: fileInfo.Size,
- Hash: fileInfo.Hash,
- Type: "material",
- CreateAt: time.Now(),
- })
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- entity.ResponseSuccess(ctx, add)
- }
- // Font 字体上传
- func Font(ctx *gin.Context) {
- file, err := ctx.FormFile("files")
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeFileNotExist, err.Error(), []interface{}{})
- return
- }
- upload := utFile.NewUpload(setting.Conf.Upload.Fonts)
- upload.SetAllowed(map[string]bool{
- ".ttf": true,
- ".otf": true,
- ".woff": true,
- ".fon": true,
- ".font": true,
- ".eot": true,
- ".ttc": true,
- })
- upload.SetHasHash(true)
- fileInfo, err := upload.Single(*file)
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeCreateDirFail, err.Error(), []interface{}{})
- }
- material := model.NewMFonts(ctx)
- add, err := material.Add(&sEntity.Font{
- Name: ctx.PostForm("name"),
- Code: fileInfo.Basename, //basename
- Path: fileInfo.File,
- FileName: fileInfo.Name,
- FileSize: fileInfo.Size,
- Hash: fileInfo.Hash,
- FileType: fileInfo.Mine,
- CreateAt: time.Now(),
- })
- if err != nil {
- entity.ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
- return
- }
- entity.ResponseSuccess(ctx, add)
- }
|