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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package controllers
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "time"
  8. "github.com/casbin/casibase/object"
  9. "github.com/casbin/casibase/util"
  10. "github.com/casbin/casibase/video"
  11. )
  12. func (c *ApiController) GetGlobalVideos() {
  13. c.Data["json"] = object.GetGlobalVideos()
  14. c.ServeJSON()
  15. }
  16. func (c *ApiController) GetVideos() {
  17. owner := c.Input().Get("owner")
  18. c.Data["json"] = object.GetVideos(owner)
  19. c.ServeJSON()
  20. }
  21. func (c *ApiController) GetVideo() {
  22. id := c.Input().Get("id")
  23. video := object.GetVideo(id)
  24. video.Populate()
  25. c.Data["json"] = video
  26. c.ServeJSON()
  27. }
  28. func (c *ApiController) UpdateVideo() {
  29. id := c.Input().Get("id")
  30. var video object.Video
  31. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  32. if err != nil {
  33. panic(err)
  34. }
  35. c.Data["json"] = object.UpdateVideo(id, &video)
  36. c.ServeJSON()
  37. }
  38. func (c *ApiController) AddVideo() {
  39. var video object.Video
  40. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  41. if err != nil {
  42. panic(err)
  43. }
  44. c.Data["json"] = object.AddVideo(&video)
  45. c.ServeJSON()
  46. }
  47. func (c *ApiController) DeleteVideo() {
  48. var video object.Video
  49. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  50. if err != nil {
  51. panic(err)
  52. }
  53. c.Data["json"] = object.DeleteVideo(&video)
  54. c.ServeJSON()
  55. }
  56. func startCoverUrlJob(owner string, name string, videoId string) {
  57. go func(owner string, name string, videoId string) {
  58. for i := 0; i < 20; i++ {
  59. coverUrl := video.GetVideoCoverUrl(videoId)
  60. if coverUrl != "" {
  61. video := object.GetVideo(util.GetIdFromOwnerAndName(owner, name))
  62. if video.CoverUrl != "" {
  63. break
  64. }
  65. video.CoverUrl = coverUrl
  66. object.UpdateVideo(util.GetIdFromOwnerAndName(owner, name), video)
  67. break
  68. }
  69. time.Sleep(time.Second * 5)
  70. }
  71. }(owner, name, videoId)
  72. }
  73. func (c *ApiController) UploadVideo() {
  74. owner := c.GetSessionUsername()
  75. file, header, err := c.GetFile("file")
  76. if err != nil {
  77. panic(err)
  78. }
  79. defer file.Close()
  80. filename := header.Filename
  81. fileId := util.RemoveExt(filename)
  82. fileBuffer := bytes.NewBuffer(nil)
  83. if _, err = io.Copy(fileBuffer, file); err != nil {
  84. c.ResponseError(err.Error())
  85. return
  86. }
  87. fileType := "unknown"
  88. contentType := header.Header.Get("Content-Type")
  89. fileType, _ = util.GetOwnerAndNameFromId(contentType)
  90. if fileType != "video" {
  91. c.ResponseError(fmt.Sprintf("contentType: %s is not video", contentType))
  92. return
  93. }
  94. videoId := video.UploadVideo(fileId, filename, fileBuffer)
  95. if videoId != "" {
  96. startCoverUrlJob(owner, fileId, videoId)
  97. video := &object.Video{
  98. Owner: owner,
  99. Name: fileId,
  100. CreatedTime: util.GetCurrentTime(),
  101. DisplayName: fileId,
  102. VideoId: videoId,
  103. Labels: []*object.Label{},
  104. DataUrls: []string{},
  105. }
  106. object.AddVideo(video)
  107. c.ResponseOk(fileId)
  108. } else {
  109. c.ResponseError("videoId is empty")
  110. }
  111. }

基于Casbin的开源AI领域知识库平台

Contributors (1)