feat: add unit tests for COS object name parsing and enhance error handling
This commit introduces unit tests for the `parseCosObjectName` method in the `cosFileService`, ensuring it correctly rejects local scheme URLs and properly parses COS scheme URLs. Additionally, the `parseCosObjectName` method has been updated to return an error for unsupported schemes, improving error handling in the `GetFile` and `DeleteFile` methods. This enhancement ensures more robust handling of file paths in the application.
This commit is contained in:
@@ -123,7 +123,10 @@ func (s *cosFileService) SaveFile(ctx context.Context,
|
||||
|
||||
// GetFile retrieves a file from COS storage by its path URL
|
||||
func (s *cosFileService) GetFile(ctx context.Context, filePathUrl string) (io.ReadCloser, error) {
|
||||
objectName := s.parseCosObjectName(filePathUrl)
|
||||
objectName, err := s.parseCosObjectName(filePathUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return nil, fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
@@ -136,11 +139,14 @@ func (s *cosFileService) GetFile(ctx context.Context, filePathUrl string) (io.Re
|
||||
|
||||
// DeleteFile removes a file from COS storage
|
||||
func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error {
|
||||
objectName := s.parseCosObjectName(filePath)
|
||||
objectName, err := s.parseCosObjectName(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
_, err := s.client.Object.Delete(ctx, objectName)
|
||||
_, err = s.client.Object.Delete(ctx, objectName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete file: %w", err)
|
||||
}
|
||||
@@ -150,18 +156,23 @@ func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error
|
||||
// parseCosObjectName extracts the object name from:
|
||||
// - provider scheme: cos://{bucket}/{region}/{objectKey}
|
||||
// - legacy URL: https://bucket.cos.region.myqcloud.com/{objectKey}
|
||||
func (s *cosFileService) parseCosObjectName(filePath string) string {
|
||||
func (s *cosFileService) parseCosObjectName(filePath string) (string, error) {
|
||||
for _, other := range []string{"local://", "minio://", "s3://", "tos://", "oss://", "ks3://", "obs://"} {
|
||||
if strings.HasPrefix(filePath, other) {
|
||||
return "", fmt.Errorf("cos file service cannot resolve %s path", strings.Split(other, "://")[0])
|
||||
}
|
||||
}
|
||||
// Provider scheme format: cos://{bucket}/{region}/{objectKey}
|
||||
if strings.HasPrefix(filePath, cosScheme) {
|
||||
rest := strings.TrimPrefix(filePath, cosScheme)
|
||||
parts := strings.SplitN(rest, "/", 3)
|
||||
if len(parts) == 3 {
|
||||
return parts[2]
|
||||
return parts[2], nil
|
||||
}
|
||||
return rest
|
||||
return rest, nil
|
||||
}
|
||||
// Legacy format: https://bucket.cos.region.myqcloud.com/{objectKey}
|
||||
return strings.TrimPrefix(filePath, s.bucketURL)
|
||||
return strings.TrimPrefix(filePath, s.bucketURL), nil
|
||||
}
|
||||
|
||||
// SaveBytes saves bytes data to COS
|
||||
@@ -212,7 +223,10 @@ func (s *cosFileService) GetFileURL(ctx context.Context, filePath string) (strin
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
objectName := s.parseCosObjectName(filePath)
|
||||
objectName, err := s.parseCosObjectName(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := utils.SafeObjectKey(objectName); err != nil {
|
||||
return "", fmt.Errorf("invalid file path: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseCosObjectName_RejectsLocalScheme(t *testing.T) {
|
||||
svc := &cosFileService{bucketURL: "https://b.cos.ap-shanghai.myqcloud.com/"}
|
||||
_, err := svc.parseCosObjectName("local://10000/exports/img.png")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "local")
|
||||
}
|
||||
|
||||
func TestParseCosObjectName_CosScheme(t *testing.T) {
|
||||
svc := &cosFileService{bucketURL: "https://b.cos.ap-shanghai.myqcloud.com/"}
|
||||
key, err := svc.parseCosObjectName("cos://bucket/ap-shanghai/weknora/10000/exports/a.png")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "weknora/10000/exports/a.png", key)
|
||||
}
|
||||
Reference in New Issue
Block a user