mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-16 16:07:33 +08:00
add transaction picture upload api
This commit is contained in:
@@ -2,12 +2,14 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/datastore"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/mail"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/storage"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/uuid"
|
||||
)
|
||||
|
||||
@@ -115,6 +117,30 @@ func (s *ServiceUsingStorage) DeleteAvatar(uid int64, fileExtension string) erro
|
||||
return s.container.DeleteAvatar(s.getUserAvatarPath(uid, fileExtension))
|
||||
}
|
||||
|
||||
// ExistsTransactionPicture returns whether the transaction picture exists from the current transaction picture object storage
|
||||
func (s *ServiceUsingStorage) ExistsTransactionPicture(uid int64, pictureId int64, fileExtension string) (bool, error) {
|
||||
return s.container.ExistsTransactionPicture(s.getTransactionPicturePath(uid, pictureId, fileExtension))
|
||||
}
|
||||
|
||||
// ReadTransactionPicture returns the transaction picture from the current transaction picture object storage
|
||||
func (s *ServiceUsingStorage) ReadTransactionPicture(uid int64, pictureId int64, fileExtension string) (storage.ObjectInStorage, error) {
|
||||
return s.container.ReadTransactionPicture(s.getTransactionPicturePath(uid, pictureId, fileExtension))
|
||||
}
|
||||
|
||||
// SaveTransactionPicture returns whether save the transaction picture into the current transaction picture object storage successfully
|
||||
func (s *ServiceUsingStorage) SaveTransactionPicture(uid int64, pictureId int64, object storage.ObjectInStorage, fileExtension string) error {
|
||||
return s.container.SaveTransactionPicture(s.getTransactionPicturePath(uid, pictureId, fileExtension), object)
|
||||
}
|
||||
|
||||
// DeleteTransactionPicture returns whether delete the transaction picture from the current transaction picture object storage successfully
|
||||
func (s *ServiceUsingStorage) DeleteTransactionPicture(uid int64, pictureId int64, fileExtension string) error {
|
||||
return s.container.DeleteTransactionPicture(s.getTransactionPicturePath(uid, pictureId, fileExtension))
|
||||
}
|
||||
|
||||
func (s *ServiceUsingStorage) getUserAvatarPath(uid int64, fileExtension string) string {
|
||||
return fmt.Sprintf("%d.%s", uid, fileExtension)
|
||||
}
|
||||
|
||||
func (s *ServiceUsingStorage) getTransactionPicturePath(uid int64, pictureId int64, fileExtension string) string {
|
||||
return filepath.Join(utils.Int64ToString(uid), fmt.Sprintf("%d.%s", pictureId, fileExtension))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/datastore"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/storage"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/uuid"
|
||||
)
|
||||
|
||||
// TransactionPictureService represents transaction picture service
|
||||
type TransactionPictureService struct {
|
||||
ServiceUsingDB
|
||||
ServiceUsingUuid
|
||||
ServiceUsingStorage
|
||||
}
|
||||
|
||||
// Initialize a transaction picture service singleton instance
|
||||
var (
|
||||
TransactionPictures = &TransactionPictureService{
|
||||
ServiceUsingDB: ServiceUsingDB{
|
||||
container: datastore.Container,
|
||||
},
|
||||
ServiceUsingUuid: ServiceUsingUuid{
|
||||
container: uuid.Container,
|
||||
},
|
||||
ServiceUsingStorage: ServiceUsingStorage{
|
||||
container: storage.Container,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// GetPictureInfoByPictureId returns a transaction picture info model according to transaction picture id
|
||||
func (s *TransactionPictureService) GetPictureInfoByPictureId(c core.Context, uid int64, pictureId int64) (*models.TransactionPictureInfo, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
if pictureId <= 0 {
|
||||
return nil, errs.ErrTransactionPictureIdInvalid
|
||||
}
|
||||
|
||||
pictureInfo := &models.TransactionPictureInfo{}
|
||||
has, err := s.UserDataDB(uid).NewSession(c).ID(pictureId).Where("uid=? AND deleted=?", uid, false).Get(pictureInfo)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, errs.ErrTransactionPictureNotFound
|
||||
}
|
||||
|
||||
return pictureInfo, nil
|
||||
}
|
||||
|
||||
// GetPictureByPictureId returns the transaction picture data according to transaction picture id
|
||||
func (s *TransactionPictureService) GetPictureByPictureId(c core.Context, uid int64, pictureId int64, fileExtension string) ([]byte, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
if pictureId <= 0 {
|
||||
return nil, errs.ErrTransactionPictureIdInvalid
|
||||
}
|
||||
|
||||
pictureInfo := &models.TransactionPictureInfo{}
|
||||
has, err := s.UserDataDB(uid).NewSession(c).ID(pictureId).Where("uid=? AND deleted=?", uid, false).Get(pictureInfo)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, errs.ErrTransactionPictureNotFound
|
||||
}
|
||||
|
||||
if pictureInfo.PictureExtension == "" {
|
||||
return nil, errs.ErrTransactionPictureNotFound
|
||||
}
|
||||
|
||||
if pictureInfo.PictureExtension != fileExtension {
|
||||
return nil, errs.ErrTransactionPictureExtensionInvalid
|
||||
}
|
||||
|
||||
pictureFile, err := s.ReadTransactionPicture(pictureInfo.Uid, pictureInfo.PictureId, pictureInfo.PictureExtension)
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return nil, errs.ErrTransactionPictureNoExists
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer pictureFile.Close()
|
||||
|
||||
pictureData, err := io.ReadAll(pictureFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pictureData, nil
|
||||
}
|
||||
|
||||
// UploadPicture uploads the transaction picture for specified user
|
||||
func (s *TransactionPictureService) UploadPicture(c core.Context, pictureInfo *models.TransactionPictureInfo, pictureFile multipart.File) error {
|
||||
if pictureInfo.Uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
defer pictureFile.Close()
|
||||
|
||||
pictureInfo.PictureId = s.GenerateUuid(uuid.UUID_TYPE_USER)
|
||||
|
||||
if pictureInfo.PictureId < 1 {
|
||||
return errs.ErrSystemIsBusy
|
||||
}
|
||||
|
||||
pictureInfo.TransactionId = 0
|
||||
pictureInfo.Deleted = false
|
||||
pictureInfo.CreatedUnixTime = time.Now().Unix()
|
||||
pictureInfo.UpdatedUnixTime = time.Now().Unix()
|
||||
|
||||
err := s.SaveTransactionPicture(pictureInfo.Uid, pictureInfo.PictureId, pictureFile, pictureInfo.PictureExtension)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.UserDataDB(pictureInfo.Uid).DoTransaction(c, func(sess *xorm.Session) error {
|
||||
_, err := sess.Insert(pictureInfo)
|
||||
return err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user