feat: complte media store

This commit is contained in:
Rogee
2024-12-06 15:06:15 +08:00
parent 43bde1e62a
commit 7d446b46c2
10 changed files with 199 additions and 226 deletions

View File

@@ -5,13 +5,13 @@ import (
"database/sql"
"path/filepath"
"backend/common/media_store"
"backend/database/models/qvyun/public/table"
"backend/pkg/path"
"backend/pkg/pg"
"backend/providers/storage"
. "github.com/go-jet/jet/v2/postgres"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
@@ -162,75 +162,40 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
return mediaID > 0, nil
}
// UnPublishTenantWithNotInUUIDs
func (svc *Service) UnPublishTenantWithNotInUUIDs(ctx context.Context, tenantId int64, uuids []string) error {
log := svc.log.WithField("method", "UnPublishTenantWithNotInUUIDs")
tbl := table.Medias
stmt := tbl.
UPDATE().
SET(
tbl.Publish.SET(Bool(false)),
).
WHERE(
tbl.TenantID.EQ(Int(tenantId)).AND(
tbl.UUID.NOT_IN(lo.Map(uuids, func(item string, _ int) Expression {
return String(item)
})...),
),
)
log.Debug(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return errors.Wrap(err, "unpublish tenant with not in uuids")
}
return nil
}
// GetTenantUUIDs
func (svc *Service) GetTenantUUIDs(ctx context.Context, tenantId int64) ([]string, error) {
log := svc.log.WithField("method", "GetTenantUUIDs")
tbl := table.Medias
stmt := tbl.
SELECT(tbl.UUID).
WHERE(tbl.TenantID.EQ(Int(tenantId)))
log.Debug(stmt.DebugSql())
var uuids []string
if err := stmt.QueryContext(ctx, svc.db, &uuids); err != nil {
return nil, errors.Wrap(err, "query tenant uuids")
}
return uuids, nil
}
// PublishTenant
func (svc *Service) PublishTenantMedia(ctx context.Context, tenantId int64, uuid uuid.UUID, name string, price uint) error {
log := svc.log.WithField("method", "PublishTenant")
// Upsert
func (svc *Service) Upsert(ctx context.Context, tenantId int64, item media_store.VideoInfo) error {
log := svc.log.WithField("method", "Upsert")
resources := pg.MediaResources{}
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeVideo.String())) {
if path.DirExists(filepath.Join(svc.storageConfig.Path, item.Hash, pg.MediaTypeVideo.String())) {
resources = append(resources, pg.MediaTypeVideo)
}
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeAudio.String())) {
if path.DirExists(filepath.Join(svc.storageConfig.Path, item.Hash, pg.MediaTypeAudio.String())) {
resources = append(resources, pg.MediaTypeAudio)
}
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypePdf.String())) {
if path.DirExists(filepath.Join(svc.storageConfig.Path, item.Hash, pg.MediaTypePdf.String())) {
resources = append(resources, pg.MediaTypePdf)
}
tbl := table.Medias
stmt := tbl.
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))
INSERT(tbl.TenantID, tbl.Hash, tbl.Title, tbl.Price, tbl.Resources, tbl.Publish).
VALUES(Int(tenantId), String(item.Hash), String(item.Name), Int(item.Price()), Json(resources), Bool(true)).
ON_CONFLICT(tbl.Hash).
DO_UPDATE(
SET(
tbl.Title.SET(String(item.Name)),
tbl.Price.SET(Int(item.Price())),
tbl.Resources.SET(Json(resources)),
tbl.Publish.SET(Bool(true)),
),
)
log.Debug(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
return errors.Wrap(err, "publish tenant")
return errors.Wrapf(err, "upsert media: %s %s", item.Hash, item.Name)
}
return nil