mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-08 02:03:42 +08:00
feat: delete the old avatar image file when update the user profile
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"chatplus/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LocalStorageService struct {
|
||||
@@ -20,8 +22,8 @@ func NewLocalStorageService(config *types.AppConfig) LocalStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
func (s LocalStorageService) PutFile(ctx *gin.Context) (string, error) {
|
||||
file, err := ctx.FormFile("file")
|
||||
func (s LocalStorageService) PutFile(ctx *gin.Context, name string) (string, error) {
|
||||
file, err := ctx.FormFile(name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error with get form: %v", err)
|
||||
}
|
||||
@@ -54,4 +56,9 @@ func (s LocalStorageService) PutImg(imageURL string) (string, error) {
|
||||
return utils.GenUploadUrl(s.config.BasePath, s.config.BaseURL, filePath), nil
|
||||
}
|
||||
|
||||
func (s LocalStorageService) Delete(fileURL string) error {
|
||||
filePath := strings.Replace(fileURL, s.config.BaseURL, s.config.BasePath, 1)
|
||||
return os.Remove(filePath)
|
||||
}
|
||||
|
||||
var _ Uploader = LocalStorageService{}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (s MinioService) PutImg(imageURL string) (string, error) {
|
||||
return "", fmt.Errorf("error with download image: %v", err)
|
||||
}
|
||||
fileExt := filepath.Ext(filepath.Base(imageURL))
|
||||
filename := fmt.Sprintf("%d%s", time.Now().UnixNano(), fileExt)
|
||||
filename := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt)
|
||||
info, err := s.client.PutObject(
|
||||
context.Background(),
|
||||
s.config.Bucket,
|
||||
@@ -51,8 +51,8 @@ func (s MinioService) PutImg(imageURL string) (string, error) {
|
||||
return fmt.Sprintf("%s/%s/%s", s.config.Domain, s.config.Bucket, info.Key), nil
|
||||
}
|
||||
|
||||
func (s MinioService) PutFile(ctx *gin.Context) (string, error) {
|
||||
file, err := ctx.FormFile("file")
|
||||
func (s MinioService) PutFile(ctx *gin.Context, name string) (string, error) {
|
||||
file, err := ctx.FormFile(name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error with get form: %v", err)
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func (s MinioService) PutFile(ctx *gin.Context) (string, error) {
|
||||
defer fileReader.Close()
|
||||
|
||||
fileExt := filepath.Ext(file.Filename)
|
||||
filename := fmt.Sprintf("%d%s", time.Now().UnixNano(), fileExt)
|
||||
filename := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt)
|
||||
info, err := s.client.PutObject(ctx, s.config.Bucket, filename, fileReader, file.Size, minio.PutObjectOptions{
|
||||
ContentType: file.Header.Get("Content-Type"),
|
||||
})
|
||||
@@ -75,4 +75,9 @@ func (s MinioService) PutFile(ctx *gin.Context) (string, error) {
|
||||
return fmt.Sprintf("%s/%s/%s", s.config.Domain, s.config.Bucket, info.Key), nil
|
||||
}
|
||||
|
||||
func (s MinioService) Delete(fileURL string) error {
|
||||
objectName := filepath.Base(fileURL)
|
||||
return s.client.RemoveObject(context.Background(), s.config.Bucket, objectName, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
var _ Uploader = MinioService{}
|
||||
|
||||
@@ -3,6 +3,7 @@ package oss
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type Uploader interface {
|
||||
PutFile(ctx *gin.Context) (string, error)
|
||||
PutFile(ctx *gin.Context, name string) (string, error)
|
||||
PutImg(imageURL string) (string, error)
|
||||
Delete(fileURL string) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user