fix: fixed bug for generating the upload file path

This commit is contained in:
RockYang 2023-10-05 18:09:42 +08:00
parent cc7271aa73
commit d83019cbe4
5 changed files with 31 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/url"
"path/filepath" "path/filepath"
"time" "time"
) )
@ -74,7 +75,11 @@ func (s AliYunOss) PutImg(imageURL string, useProxy bool) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("error with download image: %v", err) return "", fmt.Errorf("error with download image: %v", err)
} }
fileExt := filepath.Ext(filepath.Base(imageURL)) parse, err := url.Parse(imageURL)
if err != nil {
return "", fmt.Errorf("error with parse image URL: %v", err)
}
fileExt := filepath.Ext(parse.Path)
objectKey := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt) objectKey := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt)
// 上传文件字节数据 // 上传文件字节数据
err = s.bucket.PutObject(objectKey, bytes.NewReader(imageData)) err = s.bucket.PutObject(objectKey, bytes.NewReader(imageData))

View File

@ -5,6 +5,7 @@ import (
"chatplus/utils" "chatplus/utils"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -42,7 +43,11 @@ func (s LocalStorage) PutFile(ctx *gin.Context, name string) (string, error) {
} }
func (s LocalStorage) PutImg(imageURL string, useProxy bool) (string, error) { func (s LocalStorage) PutImg(imageURL string, useProxy bool) (string, error) {
filename := filepath.Base(imageURL) parse, err := url.Parse(imageURL)
if err != nil {
return "", fmt.Errorf("error with parse image URL: %v", err)
}
filename := filepath.Base(parse.Path)
filePath, err := utils.GenUploadPath(s.config.BasePath, filename) filePath, err := utils.GenUploadPath(s.config.BasePath, filename)
if err != nil { if err != nil {
return "", fmt.Errorf("error with generate image dir: %v", err) return "", fmt.Errorf("error with generate image dir: %v", err)

View File

@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -42,7 +43,11 @@ func (s MiniOss) PutImg(imageURL string, useProxy bool) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("error with download image: %v", err) return "", fmt.Errorf("error with download image: %v", err)
} }
fileExt := filepath.Ext(filepath.Base(imageURL)) parse, err := url.Parse(imageURL)
if err != nil {
return "", fmt.Errorf("error with parse image URL: %v", err)
}
fileExt := filepath.Ext(parse.Path)
filename := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt) filename := fmt.Sprintf("%d%s", time.Now().UnixMicro(), fileExt)
info, err := s.client.PutObject( info, err := s.client.PutObject(
context.Background(), context.Background(),

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage" "github.com/qiniu/go-sdk/v7/storage"
"net/url"
"path/filepath" "path/filepath"
"time" "time"
) )
@ -83,7 +84,11 @@ func (s QinNiuOss) PutImg(imageURL string, useProxy bool) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("error with download image: %v", err) return "", fmt.Errorf("error with download image: %v", err)
} }
fileExt := filepath.Ext(filepath.Base(imageURL)) parse, err := url.Parse(imageURL)
if err != nil {
return "", fmt.Errorf("error with parse image URL: %v", err)
}
fileExt := filepath.Ext(parse.Path)
key := fmt.Sprintf("%s/%d%s", s.dir, time.Now().UnixMicro(), fileExt) key := fmt.Sprintf("%s/%d%s", s.dir, time.Now().UnixMicro(), fileExt)
ret := storage.PutRet{} ret := storage.PutRet{}
extra := storage.PutExtra{} extra := storage.PutExtra{}

View File

@ -3,11 +3,15 @@ package main
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"path"
) )
func main() { func main() {
parse, _ := url.Parse("http://localhost:5678/static") imgURL := "https://www.baidu.com/static/upload/2023/10/1696497571220711277.png?ex=6530f4a2&is=651e7fa28hmFd709d069ca05d7855ebdae42e5aa436883a36f9310d546"
parse, err := url.Parse(imgURL)
if err != nil {
panic(err)
}
imgURLPrefix := fmt.Sprintf("%s://%s", parse.Scheme, parse.Host) fmt.Println(path.Ext(parse.Path))
fmt.Println(imgURLPrefix)
} }