feat: file upload and remove
This commit is contained in:
+22
-1
@@ -1,8 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"crm/service"
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -21,4 +23,23 @@ func NewCommonApi() *CommonApi {
|
||||
func (c *CommonApi) InitDatabase(context *gin.Context) {
|
||||
errCode := c.commonService.InitDatabase()
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
}
|
||||
|
||||
// 文件上传
|
||||
func (c *CommonApi) FileUpload(context *gin.Context) {
|
||||
file, _ := context.FormFile("file")
|
||||
fileInfo, errCode := c.commonService.FileUpload(file)
|
||||
response.Result(errCode, fileInfo, context)
|
||||
}
|
||||
|
||||
// 文件移除
|
||||
func (c *CommonApi) FileRemove(context *gin.Context) {
|
||||
var param models.FileParam
|
||||
if err := context.ShouldBind(¶m); err != nil {
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
errCode := c.commonService.FileRemove(¶m)
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
|
||||
+36
-2
@@ -2,10 +2,14 @@ package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crm/common"
|
||||
"crm/global"
|
||||
"crm/models"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -56,8 +60,6 @@ func NewCommonDao() *CommonDao {
|
||||
func (c *CommonDao) InitDatabase() error {
|
||||
env := global.Config.Server.Runenv
|
||||
if env == Prod {
|
||||
// fn := "/home/ubuntu/crmapi/crm.sql"
|
||||
// fn := "/Users/xuzxc/Downloads/crm.sql"
|
||||
dbFile := global.Config.Mysql.DbFile
|
||||
sql, err := os.ReadFile(dbFile)
|
||||
if err != nil {
|
||||
@@ -79,3 +81,35 @@ func (c *CommonDao) InitDatabase() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CommonDao) FileUpload(file *multipart.FileHeader) (*models.FileInfo, error) {
|
||||
dist := global.Config.File.Path
|
||||
name := common.GenUUID() + path.Ext(file.Filename)
|
||||
dn := dist + name
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
out, err := os.Create(dn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
flieInfo := models.FileInfo{
|
||||
Url: dn,
|
||||
Name: name,
|
||||
}
|
||||
return &flieInfo, err
|
||||
}
|
||||
|
||||
func (c *CommonDao) FileRemove(fileName string) error {
|
||||
file := global.Config.File.Path + fileName
|
||||
return os.Remove(file)
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ func Router() {
|
||||
|
||||
// 通用接口
|
||||
route.POST("/common/database/init", api.NewCommonApi().InitDatabase)
|
||||
route.POST("/common/file/upload", api.NewCommonApi().FileUpload)
|
||||
route.DELETE("/common/file/remove", api.NewCommonApi().FileRemove)
|
||||
|
||||
// Jwt中间件
|
||||
route.Use(middleware.JwtAuth())
|
||||
|
||||
+19
-7
@@ -8,11 +8,23 @@ type Page struct {
|
||||
|
||||
// 发送邮件参数模型
|
||||
type MailParam struct {
|
||||
Smtp string
|
||||
Port int
|
||||
AuthCode string
|
||||
Sender string
|
||||
Subject string
|
||||
Content string
|
||||
Receiver string
|
||||
Smtp string
|
||||
Port int
|
||||
AuthCode string
|
||||
Sender string
|
||||
Subject string
|
||||
Content string
|
||||
Attachment string
|
||||
Receiver string
|
||||
}
|
||||
|
||||
// 文件参数模型
|
||||
type FileParam struct {
|
||||
Name string `json:"name" binding:"required,gt=0"`
|
||||
}
|
||||
|
||||
// 文件信息
|
||||
type FileInfo struct {
|
||||
Url string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
@@ -7,15 +7,15 @@ const (
|
||||
ErrCodeNoLogin = 3 // 未登录或非法访问
|
||||
ErrCodeTokenExpire = 4 // Token过期
|
||||
|
||||
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
||||
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
||||
ErrCodeFileUploadFailed = 11 // 文件上传失败
|
||||
ErrCodeFileRemoveFailed = 12 // 文件移除失败
|
||||
|
||||
ErrCodeUserHasExist = 10001 // 用户已经存在
|
||||
ErrCodeUserNotExist = 10002 // 用户不存在
|
||||
ErrCOdeUserEmailOrPass = 10003 // 用户邮箱或密码错误
|
||||
ErrCodeVerityCodeSendFailed = 10004 // 验证码发送失败
|
||||
ErrCodeVerityCodeInvalid = 10005 // 验证码无效
|
||||
ErrCodeCompanyCreateFailed = 10006 // 企业创建失败
|
||||
ErrCodeCompanyIdNotExist = 10007 // 企业编号不存在
|
||||
ErrCodeEmailFormatInvalid = 10008 // 邮箱格式无效
|
||||
ErrCodeUserPassResetFailed = 10009 // 用户密码重置失败
|
||||
|
||||
@@ -40,12 +40,12 @@ var msg = map[int]string{
|
||||
ErrCodeNoLogin: "no login",
|
||||
ErrCodeTokenExpire: "token expire",
|
||||
ErrCodeInitDataFailed: "data init failed",
|
||||
ErrCodeFileUploadFailed: "file upload failed",
|
||||
ErrCodeUserHasExist: "user has exist",
|
||||
ErrCodeUserNotExist: "user not exist",
|
||||
ErrCOdeUserEmailOrPass: "user email or password error",
|
||||
ErrCodeVerityCodeSendFailed: "verify code send failed",
|
||||
ErrCodeVerityCodeInvalid: "verify code invalid",
|
||||
ErrCodeCompanyCreateFailed: "company create failed",
|
||||
ErrCodeEmailFormatInvalid: "email format invalid",
|
||||
ErrCodeUserPassResetFailed: "user password reset failed",
|
||||
ErrCodeFileExportFailed: "file export failed",
|
||||
|
||||
@@ -2,7 +2,9 @@ package service
|
||||
|
||||
import (
|
||||
"crm/dao"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -50,3 +52,20 @@ func (c *CommonService) InitDatabase() int {
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 文件上传
|
||||
func (c *CommonService) FileUpload(file *multipart.FileHeader) (*models.FileInfo, int) {
|
||||
fileInfo, err := c.commonDao.FileUpload(file)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFileUploadFailed
|
||||
}
|
||||
return fileInfo, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 文件移除
|
||||
func (c *CommonService) FileRemove(param *models.FileParam) int {
|
||||
if err := c.commonDao.FileRemove(param.Name); err != nil {
|
||||
return response.ErrCodeFileUploadFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user