Pārlūkot izejas kodu

add local upload image api

animeic 2 gadi atpakaļ
vecāks
revīzija
da8d4a0c13
1 mainītis faili ar 35 papildinājumiem un 0 dzēšanām
  1. 35 0
      boxcost/api/signature.go

+ 35 - 0
boxcost/api/signature.go

@@ -6,6 +6,10 @@ import (
 	"box-cost/log"
 	"errors"
 	"fmt"
+	"io"
+	"os"
+	"path"
+	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -30,6 +34,8 @@ func Signature(r *GinRouter) {
 
 	// 删除签名
 	r.POST("/signature/delete/:id", SignatureDel)
+
+	r.POST("/signature/upload/image", SignatureUpload)
 }
 
 // 创建签名
@@ -115,3 +121,32 @@ func SignatureDel(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signatureId)
 }
+
+// 本地上传图片
+func SignatureUpload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	handle, err := c.FormFile("image")
+	// 上传图像接口
+	if err != nil {
+		return nil, err
+	}
+	uploadFile, err := handle.Open()
+	if err != nil {
+		return nil, err
+	}
+
+	ext := strings.ToLower(path.Ext(handle.Filename))
+	if ext != ".jpg" && ext != ".png" {
+		return nil, errors.New("只支持jpg/png图片上传")
+	}
+	// 保存图片
+	os.Mkdir("./signature/", 0777)
+	saveFile, err := os.OpenFile("./signature/"+handle.Filename, os.O_WRONLY|os.O_CREATE, 0666)
+	if err != nil {
+		return nil, err
+	}
+	io.Copy(saveFile, uploadFile)
+	defer uploadFile.Close()
+	defer saveFile.Close()
+
+	return "signature/" + handle.Filename, nil
+}