fix: issues
This commit is contained in:
@@ -3,12 +3,15 @@ package medias
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"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"
|
||||
@@ -16,8 +19,9 @@ import (
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *logrus.Entry `inject:"false"`
|
||||
db *sql.DB
|
||||
storageConfig *storage.Config
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
@@ -42,26 +46,7 @@ func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*L
|
||||
return nil, errors.Wrap(err, "query media by id")
|
||||
}
|
||||
|
||||
tblResource := table.MediaResources
|
||||
stmtResource := tblResource.
|
||||
SELECT(
|
||||
tblResource.MediaID,
|
||||
tblResource.Type,
|
||||
tblResource.Size,
|
||||
).
|
||||
WHERE(tblResource.MediaID.EQ(Int(id)))
|
||||
log.Debug(stmtResource.DebugSql())
|
||||
|
||||
var resources []model.MediaResources
|
||||
if err := stmt.QueryContext(ctx, svc.db, &resources); err != nil {
|
||||
return nil, errors.Wrap(err, "query media resources")
|
||||
}
|
||||
|
||||
if len(resources) == 0 {
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
media.MediaResources = resources
|
||||
// todo: resources
|
||||
|
||||
var err error
|
||||
media.Bought, err = svc.HasUserBought(ctx, tenantId, userId, media.ID)
|
||||
@@ -72,55 +57,6 @@ func (svc *Service) GetByID(ctx context.Context, tenantId, userId, id int64) (*L
|
||||
return &media, nil
|
||||
}
|
||||
|
||||
// Decorate List Resources
|
||||
func (svc *Service) DecorateListResources(ctx context.Context, tenantId, userId int64, items []ListItem) ([]ListItem, error) {
|
||||
log := svc.log.WithField("method", "DecorateListResources")
|
||||
|
||||
mediaIDs := make([]int64, len(items))
|
||||
for i, item := range items {
|
||||
mediaIDs[i] = item.ID
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.
|
||||
SELECT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
).
|
||||
WHERE(tbl.MediaID.IN(lo.Map(mediaIDs, func(item int64, _ int) Expression {
|
||||
return Int(item)
|
||||
})...))
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
var resources []model.MediaResources
|
||||
if err := stmt.QueryContext(ctx, svc.db, &resources); err != nil {
|
||||
return nil, errors.Wrap(err, "query media resources")
|
||||
}
|
||||
|
||||
if len(resources) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// group resources by media id
|
||||
resourcesMap := make(map[int64][]model.MediaResources)
|
||||
for _, resource := range resources {
|
||||
if _, ok := resourcesMap[resource.MediaID]; !ok {
|
||||
resourcesMap[resource.MediaID] = make([]model.MediaResources, 0)
|
||||
}
|
||||
resourcesMap[resource.MediaID] = append(resourcesMap[resource.MediaID], resource)
|
||||
}
|
||||
|
||||
// set resources to items
|
||||
for i, item := range items {
|
||||
if resources, ok := resourcesMap[item.ID]; ok {
|
||||
items[i].MediaResources = resources
|
||||
}
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// List
|
||||
func (svc *Service) List(ctx context.Context, tenantId, userId int64, filter *ListFilter) ([]ListItem, error) {
|
||||
log := svc.log.WithField("method", "List")
|
||||
@@ -226,57 +162,76 @@ func (svc *Service) HasUserBought(ctx context.Context, tenantId, userId, mediaId
|
||||
return mediaID > 0, nil
|
||||
}
|
||||
|
||||
// GetVideo
|
||||
func (svc *Service) GetVideo(ctx context.Context, tenantId, userId, mediaId int64) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetVideo")
|
||||
// UnPublishTenantWithNotInUUIDs
|
||||
func (svc *Service) UnPublishTenantWithNotInUUIDs(ctx context.Context, tenantId int64, uuids []string) error {
|
||||
log := svc.log.WithField("method", "UnPublishTenantWithNotInUUIDs")
|
||||
|
||||
ok, err := svc.HasUserBought(ctx, tenantId, userId, mediaId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "check user bought")
|
||||
}
|
||||
log.Debugf("user bought media: %d of tennatID: %d", mediaId, tenantId)
|
||||
|
||||
source, err := svc.GetResourceOfType(ctx, mediaId, pg.MediaTypeVideo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get video resource")
|
||||
}
|
||||
|
||||
if ok {
|
||||
return source, nil
|
||||
}
|
||||
// cut 1 min video
|
||||
maxDuration := int64(60)
|
||||
duration := int64(0)
|
||||
|
||||
for i, item := range source.Items {
|
||||
duration += item.Duration
|
||||
if duration >= maxDuration {
|
||||
source.Items = source.Items[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return source, nil
|
||||
}
|
||||
|
||||
// GetResourceOfType
|
||||
func (svc *Service) GetResourceOfType(ctx context.Context, mediaId int64, resourceType pg.MediaType) (*pg.MediaSource, error) {
|
||||
log := svc.log.WithField("method", "GetResourceOfType")
|
||||
|
||||
tbl := table.MediaResources
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.Source).
|
||||
UPDATE().
|
||||
SET(
|
||||
tbl.Publish.SET(Bool(false)),
|
||||
).
|
||||
WHERE(
|
||||
tbl.MediaID.EQ(Int(mediaId)).AND(
|
||||
tbl.Type.EQ(String(resourceType.String())),
|
||||
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())
|
||||
|
||||
var source pg.MediaSource
|
||||
if err := stmt.QueryContext(ctx, svc.db, &source); err != nil {
|
||||
return nil, errors.Wrap(err, "query media source")
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrap(err, "unpublish tenant with not in uuids")
|
||||
}
|
||||
|
||||
return &source, nil
|
||||
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) error {
|
||||
log := svc.log.WithField("method", "PublishTenant")
|
||||
|
||||
resources := pg.MediaResources{}
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeVideo.String())) {
|
||||
resources = append(resources, pg.MediaTypeVideo)
|
||||
}
|
||||
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypeAudio.String())) {
|
||||
resources = append(resources, pg.MediaTypeAudio)
|
||||
}
|
||||
|
||||
if path.DirExists(filepath.Join(svc.storageConfig.Path, uuid.String(), pg.MediaTypePdf.String())) {
|
||||
resources = append(resources, pg.MediaTypePdf)
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
INSERT(tbl.TenantID, tbl.UUID, tbl.Title, tbl.Resources).
|
||||
VALUES(Int(tenantId), UUID(uuid), String(name), Json(resources.MustValue()))
|
||||
log.Debug(stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return errors.Wrap(err, "publish tenant")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user