Skip to content

Commit

Permalink
feat: add attachment model
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 21, 2024
1 parent f7f5fc3 commit b2d6762
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
6 changes: 3 additions & 3 deletions cmd/srv/controller/api_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (v *apiV1) serve() {
Redirect: fmt.Sprintf("%s/login", singleton.Conf.Site.BaseURL),
}))

r.POST("/attchment/upload", v.upload) // upload file
r.POST("/attachment/upload", v.upload) // upload file

r.POST("/post", v.postPost) // create post
r.GET("/post/:id", v.getPost) // get post
Expand All @@ -46,12 +46,12 @@ func (v *apiV1) serve() {
var authModel = model.Auth{}

func (v *apiV1) upload(c *gin.Context) {
result, err := singleton.AttchmentUpload.Upload(c)
attachment, err := gogin.AttachmentUpload(c)
if err != nil {
mygin.ResponseJSON(c, 400, gin.H{}, err.Error())
return
}
mygin.ResponseJSON(c, 200, result, "upload success")
mygin.ResponseJSON(c, 200, attachment, "upload success")
}

func (v *apiV1) logout(c *gin.Context) {
Expand Down
10 changes: 10 additions & 0 deletions internal/gogin/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,13 @@ func UserLogout(c *gin.Context) {
c.SetCookie(singleton.Conf.JWT.AccessTokenCookieName, "", -1, "/", "", false, true)
c.SetCookie(singleton.Conf.JWT.RefreshTokenCookieName, "", -1, "/", "", false, true)
}

func AttachmentUpload(c *gin.Context) (*model.Attachment, error) {
result, err := singleton.AttachmentUpload.Upload(c)
if err != nil {
return nil, err
}
var attachmentModel = model.Attachment{}
attachmentModel.CreateByAttachment(singleton.DB, *result)
return &attachmentModel, nil
}
18 changes: 15 additions & 3 deletions model/attachment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"go-gin/pkg/mygin"
"go-gin/pkg/utils"

"gorm.io/gorm"
Expand All @@ -24,9 +25,20 @@ type Attachment struct {
}

func (a *Attachment) Create(db *gorm.DB) (err error) {
if a.Num == "" {
a.Num = utils.GenHexStr(32)
}
err = db.Model(&Attachment{}).Create(&a).Error
return err
}

func (a *Attachment) CreateByAttachment(db *gorm.DB, uploadedFile mygin.AttachmentUploadedFile) (err error) {
a.Num = utils.GenHexStr(32)
a.Name = uploadedFile.OriginalName
a.SavePath = uploadedFile.SavePath
a.MD5 = uploadedFile.MD5
a.Url = uploadedFile.Url
a.Size = uploadedFile.Size
a.MiMe = uploadedFile.MiMe
a.Width = uploadedFile.Width
a.Height = uploadedFile.Height
err = db.Model(&Attachment{}).Create(&a).Error
return err
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/mygin/attchment_upload.go → pkg/mygin/attachment_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/gin-gonic/gin"
)

type AttchmentUpload struct {
type AttachmentUpload struct {
BaseURL string // BaseURL is the base url for the uploaded file
MaxSize int64 // MaxSize is the max file size, default is 2MB
AllowTypes []string // AllowTypes is the allowed file types
Expand All @@ -24,21 +24,21 @@ type AttchmentUpload struct {
KeepOriginalName bool // KeepOriginalName is the flag to keep the original file name
}

type AttchmentUploadResult struct {
type AttachmentUploadedFile struct {
Url string `json:"url"`
Name string `json:"name"`
OriginalName string `json:"original_name"`
Size int64 `json:"size"`
MiMe string `json:"mime"`
With int `json:"width"`
Hei int `json:"height"`
Ext string `json:"ext"`
MD5 string `json:"md5"`
SavePath string `json:"save_path"`
Width int `json:"width"` // Width is the width of the image
Height int `json:"height"` // Height is the height of the image
Ext string `json:"ext"` // Ext is the file extension, eg: .jpg
MD5 string `json:"md5"` // MD5 is the md5 of the file
SavePath string `json:"save_path"` // SavePath is the path to save the file
}

func (a *AttchmentUpload) Upload(c *gin.Context) (*AttchmentUploadResult, error) {
result := &AttchmentUploadResult{}
func (a *AttachmentUpload) Upload(c *gin.Context) (*AttachmentUploadedFile, error) {
result := &AttachmentUploadedFile{}
form_file, err := c.FormFile(a.FormName)
if err != nil {
return result, err
Expand Down Expand Up @@ -89,14 +89,14 @@ func (a *AttchmentUpload) Upload(c *gin.Context) (*AttchmentUploadResult, error)
result.MiMe = form_file_mime
result.Ext = form_file_ext
result.MD5 = md5
result.With = w
result.Hei = h
result.Width = w
result.Height = h
result.SavePath = savePath
return result, nil
}

func NewAttchmentUpload() *AttchmentUpload {
return &AttchmentUpload{
func NewAttachmentUpload() *AttachmentUpload {
return &AttachmentUpload{
BaseURL: "/upload",
MaxSize: 1024 * 1024 * 2,
AllowTypes: []string{"image/jpeg", "image/png", "image/gif", "image/jpg"},
Expand Down
2 changes: 1 addition & 1 deletion service/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func InitDBFromPath(dbpath string) {
if Conf.Debug {
DB = DB.Debug()
}
err = DB.AutoMigrate(&model.User{}, &model.Post{})
err = DB.AutoMigrate(&model.User{}, &model.Post{}, &model.Attachment{})
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions service/singleton/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

var (
AttchmentUpload *mygin.AttchmentUpload
AttachmentUpload *mygin.AttachmentUpload
)

func LoadUpload() {
AttchmentUpload = &mygin.AttchmentUpload{
AttachmentUpload = &mygin.AttachmentUpload{
BaseURL: Conf.Site.BaseURL + Conf.Upload.VirtualPath,
MaxSize: Conf.Upload.MaxSize,
AllowTypes: Conf.Upload.AllowTypes,
Expand Down

0 comments on commit b2d6762

Please sign in to comment.