You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

video.go 3.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package object
  15. import (
  16. "fmt"
  17. "github.com/casibase/casibase/util"
  18. "github.com/casibase/casibase/video"
  19. "xorm.io/core"
  20. )
  21. type Label struct {
  22. Id string `xorm:"varchar(100)" json:"id"`
  23. StartTime float64 `json:"startTime"`
  24. EndTime float64 `json:"endTime"`
  25. Text string `xorm:"varchar(100)" json:"text"`
  26. }
  27. type Video struct {
  28. Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
  29. Name string `xorm:"varchar(100) notnull pk" json:"name"`
  30. CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
  31. DisplayName string `xorm:"varchar(500)" json:"displayName"`
  32. VideoId string `xorm:"varchar(100)" json:"videoId"`
  33. CoverUrl string `xorm:"varchar(200)" json:"coverUrl"`
  34. Labels []*Label `xorm:"mediumtext" json:"labels"`
  35. DataUrls []string `xorm:"mediumtext" json:"dataUrls"`
  36. DataUrl string `xorm:"varchar(200)" json:"dataUrl"`
  37. TagOnPause bool `json:"tagOnPause"`
  38. PlayAuth string `xorm:"-" json:"playAuth"`
  39. }
  40. func GetGlobalVideos() ([]*Video, error) {
  41. videos := []*Video{}
  42. err := adapter.engine.Asc("owner").Desc("created_time").Find(&videos)
  43. if err != nil {
  44. return videos, err
  45. }
  46. return videos, nil
  47. }
  48. func GetVideos(owner string) ([]*Video, error) {
  49. videos := []*Video{}
  50. err := adapter.engine.Desc("created_time").Find(&videos, &Video{Owner: owner})
  51. if err != nil {
  52. return videos, err
  53. }
  54. return videos, nil
  55. }
  56. func getVideo(owner string, name string) (*Video, error) {
  57. v := Video{Owner: owner, Name: name}
  58. existed, err := adapter.engine.Get(&v)
  59. if err != nil {
  60. return &v, err
  61. }
  62. if existed {
  63. if v.VideoId != "" {
  64. v.PlayAuth = video.GetVideoPlayAuth(v.VideoId)
  65. }
  66. return &v, nil
  67. } else {
  68. return nil, nil
  69. }
  70. }
  71. func GetVideo(id string) (*Video, error) {
  72. owner, name := util.GetOwnerAndNameFromId(id)
  73. return getVideo(owner, name)
  74. }
  75. func UpdateVideo(id string, video *Video) (bool, error) {
  76. owner, name := util.GetOwnerAndNameFromId(id)
  77. _, err := getVideo(owner, name)
  78. if err != nil {
  79. return false, err
  80. }
  81. if video == nil {
  82. return false, nil
  83. }
  84. _, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(video)
  85. if err != nil {
  86. return false, err
  87. }
  88. // return affected != 0
  89. return true, nil
  90. }
  91. func AddVideo(video *Video) (bool, error) {
  92. affected, err := adapter.engine.Insert(video)
  93. if err != nil {
  94. return false, err
  95. }
  96. return affected != 0, nil
  97. }
  98. func DeleteVideo(video *Video) (bool, error) {
  99. affected, err := adapter.engine.ID(core.PK{video.Owner, video.Name}).Delete(&Video{})
  100. if err != nil {
  101. return false, err
  102. }
  103. return affected != 0, nil
  104. }
  105. func (video *Video) GetId() string {
  106. return fmt.Sprintf("%s/%s", video.Owner, video.Name)
  107. }
  108. func (video *Video) Populate() error {
  109. store, err := GetDefaultStore("admin")
  110. if err != nil {
  111. return err
  112. }
  113. if store == nil {
  114. return nil
  115. }
  116. dataUrls, err := store.GetVideoData()
  117. if err != nil {
  118. return err
  119. }
  120. video.DataUrls = dataUrls
  121. return nil
  122. }