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

View File

@ -1,91 +1,53 @@
package adshttp
import (
"fmt"
authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth"
adsdomain "legalgo-BE-go/internal/domain/ads"
"legalgo-BE-go/internal/domain/oss"
adssvc "legalgo-BE-go/internal/services/ads"
"legalgo-BE-go/internal/utilities/response"
"legalgo-BE-go/internal/utilities/utils"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-playground/validator/v10"
)
const _oneMB = 1 << 20
const _maxUploadSize = 2 * _oneMB
const _folderName = "/file"
func Create(
router chi.Router,
adsSvc adssvc.Ads,
validate *validator.Validate,
) {
router.With(authmiddleware.Authorize()).Post("/ads/create", func(w http.ResponseWriter, r *http.Request) {
router.
With(authmiddleware.Authorize()).
Post("/ads/create", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var spec adsdomain.AdsReq
err := r.ParseMultipartForm(10 << 20) // 10 MB
if err != nil {
if err := utils.UnmarshalBody(r, &spec); err != nil {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"unable to parse form",
"failed to unmarshal body",
)
return
}
url := r.FormValue("url")
if url == "" {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"URL is missing",
)
return
}
file, header, err := r.FormFile("image")
if err != nil {
response.RespondJsonErrorWithCode(
ctx,
w,
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
"unable to retrieve image",
)
return
}
defer file.Close()
if header.Size > int64(_maxUploadSize) {
if err := validate.Struct(spec); err != nil {
response.ResponseWithErrorCode(
ctx,
w,
fmt.Errorf("file too big"),
err,
response.ErrBadRequest.Code,
response.ErrBadRequest.HttpCode,
fmt.Sprintf("The file is too big. The maximum size is %d MB", _maxUploadSize/_oneMB),
err.(validator.ValidationErrors).Error(),
)
return
}
imageRequest := &oss.UploadFileRequest{
FileHeader: header,
FolderName: _folderName,
}
adsReq := adsdomain.AdsReq{
Image: imageRequest,
Url: url,
}
if err := adsSvc.Create(ctx, adsReq); err != nil {
if err := adsSvc.Create(spec); err != nil {
response.RespondJsonErrorWithCode(
ctx,
w,

View File

@ -1,7 +1,6 @@
package adsdomain
import (
"legalgo-BE-go/internal/domain/oss"
"time"
)
@ -14,6 +13,6 @@ type Ads struct {
}
type AdsReq struct {
Image *oss.UploadFileRequest `json:"image"`
Url string `json:"url"`
Image string `json:"image" validate:"required"`
URL string `json:"url" validate:"required"`
}

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)
}

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)
}