fix: image must be url in create ads

This commit is contained in:
ericprd
2025-03-10 10:28:23 +08:00
parent 1ae192ccef
commit ef6b17a363
4 changed files with 59 additions and 128 deletions
+10 -39
View File
@@ -1,56 +1,27 @@
package adssvc
import (
"context"
"fmt"
adsdomain "legalgo-BE-go/internal/domain/ads"
"path"
"strings"
"github.com/google/uuid"
)
func (i *impl) Create(ctx context.Context, spec adsdomain.AdsReq) error {
id := uuid.NewString()
file := spec.Image.FileHeader
spec.Image.FileSize = file.Size
spec.Image.Ext = path.Ext(file.Filename)
if err := i.validate.Struct(spec); err != nil {
return err
func (i *impl) Create(spec adsdomain.AdsReq) error {
if !(strings.HasPrefix(spec.Image, "http")) {
return fmt.Errorf("image url is not started with http")
}
// Open the file and read its content
srcFile, err := file.Open()
if err != nil {
return err
}
defer srcFile.Close()
fileContent := make([]byte, file.Size)
_, err = srcFile.Read(fileContent)
if err != nil {
return err
}
filePath := fmt.Sprintf("%v/%v%v", spec.Image.FolderName, id, spec.Image.Ext)
fileUrl, err := i.ossRepo.UploadFile(ctx, filePath, fileContent)
if err != nil {
return err
if !(strings.HasPrefix(spec.URL, "http")) {
return fmt.Errorf("url is not started with http")
}
newSpec := adsdomain.Ads{
ID: id,
ImageUrl: fileUrl,
Url: spec.Url,
ID: uuid.NewString(),
ImageUrl: spec.Image,
Url: spec.URL,
}
if err := i.adsRepo.Create(newSpec); err != nil {
if err := i.ossRepo.DeleteObject(ctx, id); err != nil {
return err
}
return err
}
return nil
return i.adsRepo.Create(newSpec)
}
+1 -2
View File
@@ -1,7 +1,6 @@
package adssvc
import (
"context"
adsrepository "legalgo-BE-go/internal/accessor/ads"
"legalgo-BE-go/internal/accessor/oss"
adsdomain "legalgo-BE-go/internal/domain/ads"
@@ -16,7 +15,7 @@ type impl struct {
}
type Ads interface {
Create(context.Context, adsdomain.AdsReq) error
Create(adsdomain.AdsReq) error
GetAll() ([]adsdomain.Ads, error)
}