feat: publish complete

This commit is contained in:
Rogee
2024-12-05 18:57:49 +08:00
parent e11924ea8a
commit 9f9d11f621
7 changed files with 40 additions and 16 deletions

View File

@@ -207,7 +207,7 @@ func (svc *Service) GetTenantUUIDs(ctx context.Context, tenantId int64) ([]strin
}
// PublishTenant
func (svc *Service) PublishTenantMedia(ctx context.Context, tenantId int64, uuid uuid.UUID, name string) error {
func (svc *Service) PublishTenantMedia(ctx context.Context, tenantId int64, uuid uuid.UUID, name string, price uint) error {
log := svc.log.WithField("method", "PublishTenant")
resources := pg.MediaResources{}
@@ -225,8 +225,8 @@ func (svc *Service) PublishTenantMedia(ctx context.Context, tenantId int64, uuid
tbl := table.Medias
stmt := tbl.
INSERT(tbl.TenantID, tbl.UUID, tbl.Title, tbl.Resources).
VALUES(Int(tenantId), UUID(uuid), String(name), Json(resources.MustValue()))
INSERT(tbl.TenantID, tbl.UUID, tbl.Title, tbl.Price, tbl.Resources, tbl.Publish).
VALUES(Int(tenantId), UUID(uuid), String(name), Int(int64(price)), Json(resources.MustValue()), Bool(true))
log.Debug(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {

View File

@@ -195,13 +195,13 @@ func TestService_PublishTenant(t *testing.T) {
}
So(svc.Prepare(), ShouldBeNil)
err := svc.PublishTenantMedia(context.TODO(), 1, items[0].UUID, items[0].Title)
err := svc.PublishTenantMedia(context.TODO(), 1, items[0].UUID, items[0].Title, 100)
So(err, ShouldBeNil)
err = svc.PublishTenantMedia(context.TODO(), 1, items[1].UUID, items[1].Title)
err = svc.PublishTenantMedia(context.TODO(), 1, items[1].UUID, items[1].Title, 200)
So(err, ShouldBeNil)
err = svc.PublishTenantMedia(context.TODO(), 1, items[2].UUID, items[2].Title)
err = svc.PublishTenantMedia(context.TODO(), 1, items[2].UUID, items[2].Title, 300)
So(err, ShouldBeNil)
})
})

View File

@@ -1,10 +1,12 @@
package discover
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"backend/common/media_store"
@@ -84,7 +86,12 @@ func (d *DiscoverMedias) RunE(from, to string) error {
}
}
store.Add(uuid, dir)
price, err := d.getPrice(dirPath)
if err != nil {
return errors.Wrapf(err, "get price: %s", dirPath)
}
store.Add(uuid, dir, price)
if err := store.Save(to); err != nil {
return errors.Wrapf(err, "store save: %s", to)
}
@@ -241,3 +248,23 @@ func (d *DiscoverMedias) ffmpegVideoToPoster(video string, output string, second
d.log.Infof("get snapshot: %s", logs)
return nil
}
func (d *DiscoverMedias) getPrice(dir string) (uint, error) {
price, err := os.ReadFile(filepath.Join(dir, "price.txt"))
if err != nil {
return 0, errors.Wrapf(err, "read price: %s", dir)
}
if len(price) == 0 {
return 0, fmt.Errorf("%s, price is empty", dir)
}
price = bytes.TrimSpace(price)
p, err := strconv.Atoi(string(price))
if err != nil {
return 0, fmt.Errorf(dir, ", price is not a number")
}
return uint(p), nil
}

View File

@@ -59,7 +59,7 @@ func (d *StoreMedias) RunE(targetPath string) error {
return errors.Wrap(err, "uuid from bytes")
}
if err := d.mediasSvc.PublishTenantMedia(context.Background(), 1, u, item.Name); err != nil {
if err := d.mediasSvc.PublishTenantMedia(context.Background(), 1, u, item.Name, item.Price); err != nil {
return errors.Wrapf(err, "PublishTenant: %+v", item)
}