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 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 controllers
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "time"
  21. "github.com/casbin/casibase/object"
  22. "github.com/casbin/casibase/util"
  23. "github.com/casbin/casibase/video"
  24. )
  25. func (c *ApiController) GetGlobalVideos() {
  26. videos, err := object.GetGlobalVideos()
  27. if err != nil {
  28. c.ResponseError(err.Error())
  29. return
  30. }
  31. c.ResponseOk(videos)
  32. }
  33. func (c *ApiController) GetVideos() {
  34. owner := c.Input().Get("owner")
  35. videos, err := object.GetVideos(owner)
  36. if err != nil {
  37. c.ResponseError(err.Error())
  38. return
  39. }
  40. c.ResponseOk(videos)
  41. }
  42. func (c *ApiController) GetVideo() {
  43. id := c.Input().Get("id")
  44. video, err := object.GetVideo(id)
  45. if err != nil {
  46. c.ResponseError(err.Error())
  47. return
  48. }
  49. err = video.Populate()
  50. if err != nil {
  51. c.ResponseError(err.Error())
  52. return
  53. }
  54. c.ResponseOk(video)
  55. }
  56. func (c *ApiController) UpdateVideo() {
  57. id := c.Input().Get("id")
  58. var video object.Video
  59. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  60. if err != nil {
  61. c.ResponseError(err.Error())
  62. return
  63. }
  64. success, err := object.UpdateVideo(id, &video)
  65. if err != nil {
  66. c.ResponseError(err.Error())
  67. return
  68. }
  69. c.ResponseOk(success)
  70. }
  71. func (c *ApiController) AddVideo() {
  72. var video object.Video
  73. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  74. if err != nil {
  75. c.ResponseError(err.Error())
  76. return
  77. }
  78. success, err := object.AddVideo(&video)
  79. if err != nil {
  80. c.ResponseError(err.Error())
  81. return
  82. }
  83. c.ResponseOk(success)
  84. }
  85. func (c *ApiController) DeleteVideo() {
  86. var video object.Video
  87. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  88. if err != nil {
  89. c.ResponseError(err.Error())
  90. return
  91. }
  92. success, err := object.DeleteVideo(&video)
  93. if err != nil {
  94. c.ResponseError(err.Error())
  95. return
  96. }
  97. c.ResponseOk(success)
  98. }
  99. func startCoverUrlJob(owner string, name string, videoId string) {
  100. go func(owner string, name string, videoId string) {
  101. for i := 0; i < 20; i++ {
  102. coverUrl := video.GetVideoCoverUrl(videoId)
  103. if coverUrl != "" {
  104. video, _ := object.GetVideo(util.GetIdFromOwnerAndName(owner, name))
  105. if video.CoverUrl != "" {
  106. break
  107. }
  108. video.CoverUrl = coverUrl
  109. object.UpdateVideo(util.GetIdFromOwnerAndName(owner, name), video)
  110. break
  111. }
  112. time.Sleep(time.Second * 5)
  113. }
  114. }(owner, name, videoId)
  115. }
  116. func (c *ApiController) UploadVideo() {
  117. owner := c.GetSessionUsername()
  118. file, header, err := c.GetFile("file")
  119. if err != nil {
  120. c.ResponseError(err.Error())
  121. return
  122. }
  123. defer file.Close()
  124. filename := header.Filename
  125. fileId := util.RemoveExt(filename)
  126. fileBuffer := bytes.NewBuffer(nil)
  127. if _, err = io.Copy(fileBuffer, file); err != nil {
  128. c.ResponseError(err.Error())
  129. return
  130. }
  131. fileType := "unknown"
  132. contentType := header.Header.Get("Content-Type")
  133. fileType, _ = util.GetOwnerAndNameFromId(contentType)
  134. if fileType != "video" {
  135. c.ResponseError(fmt.Sprintf("contentType: %s is not video", contentType))
  136. return
  137. }
  138. videoId := video.UploadVideo(fileId, filename, fileBuffer)
  139. if videoId != "" {
  140. startCoverUrlJob(owner, fileId, videoId)
  141. video := &object.Video{
  142. Owner: owner,
  143. Name: fileId,
  144. CreatedTime: util.GetCurrentTime(),
  145. DisplayName: fileId,
  146. VideoId: videoId,
  147. Labels: []*object.Label{},
  148. DataUrls: []string{},
  149. }
  150. _, err = object.AddVideo(video)
  151. if err != nil {
  152. c.ResponseError(err.Error())
  153. return
  154. }
  155. c.ResponseOk(fileId)
  156. } else {
  157. c.ResponseError("videoId is empty")
  158. }
  159. }