module.Video.go_disabled 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "encoding/json"
  6. "log"
  7. )
  8. func module_Video_init(){
  9. http.HandleFunc("/Video/buildPlaylist", module_ArdPlayer_buildPlaylist)
  10. //Register module
  11. registerModule(moduleInfo{
  12. Name: "Video",
  13. Desc: "The basic video player for ArOZ Online",
  14. Group: "Media",
  15. IconPath: "Video/img/module_icon.png",
  16. Version: "0.0.4",
  17. StartDir: "Video/index.html",
  18. SupportFW: true,
  19. LaunchFWDir: "Video/index.html",
  20. SupportEmb: true,
  21. LaunchEmb: "Video/embedded.html",
  22. InitFWSize: []int{585, 820},
  23. InitEmbSize: []int{700, 470},
  24. SupportedExt: []string{".webm",".mp4",".ogg"},
  25. })
  26. }
  27. //Scan all the attached storage devices and generate a global playlist
  28. func module_ArdPlayer_buildPlaylist(w http.ResponseWriter, r *http.Request){
  29. username, err := system_auth_getUserName(w,r);
  30. if (err != nil){
  31. sendErrorResponse(w,"User not logged in")
  32. return;
  33. }
  34. type videoFile struct{
  35. Filename string
  36. Filepath string
  37. Ext string
  38. }
  39. type playList struct{
  40. Name string
  41. Files []videoFile
  42. }
  43. type viewPoint struct{
  44. StorageName string
  45. PlayLists []playList
  46. UnsortedVideos []videoFile
  47. }
  48. results := []viewPoint{}
  49. for _, dev := range storages{
  50. //Get the base dir of this storage device
  51. scanBaseDir := ""
  52. devicePath := dev.Path;
  53. if (devicePath[len(devicePath)-1:] != "/"){
  54. devicePath = devicePath + "/"
  55. }
  56. if (dev.Hierarchy == "users"){
  57. scanBaseDir = devicePath + username + "/Video"
  58. }else if (dev.Hierarchy == "public"){
  59. scanBaseDir = devicePath
  60. }
  61. if (scanBaseDir == "" || !fileExists(scanBaseDir)){
  62. //This directory has no supported hierarchy or root folder not exists
  63. continue;
  64. }
  65. //log.Println(scanBaseDir)
  66. //Scan this directory for folders or video files and build a playlist out of it
  67. supportExt := []string{".mp4",".webm",".ogg"}
  68. objs, _ := system_fs_specialGlob(scanBaseDir)
  69. //Declare a new ViewPort for this device
  70. thisViewPort := new(viewPoint)
  71. allPlayLists := []playList{}
  72. unsortedFiles := []videoFile{}
  73. for _, file := range objs{
  74. if (IsDir(file)){
  75. //This is a playlist. List all its contents
  76. filesInside := []string{}
  77. filesInPlaylist, _ := system_fs_specialGlob(file + "/")
  78. for _, videoInList := range filesInPlaylist{
  79. if (system_fs_matchFileExt(videoInList, supportExt)){
  80. filesInside = append(filesInside, videoInList)
  81. }
  82. }
  83. if (len(filesInside) > 0){
  84. //This is a valid playlist
  85. thisPlayList := new(playList)
  86. thisPlayList.Name = filepath.Base(file)
  87. videosInPlaylist := []videoFile{}
  88. for _, videoInPlaylist := range filesInside{
  89. thisVideoFile := new(videoFile)
  90. thisVideoFile.Filename = filepath.Base(videoInPlaylist)
  91. thisVideoFile.Filepath, _ = realpathToVirtualpath(videoInPlaylist, username)
  92. thisVideoFile.Ext = filepath.Ext(videoInPlaylist)
  93. videosInPlaylist = append(videosInPlaylist, *thisVideoFile)
  94. }
  95. thisPlayList.Files = videosInPlaylist;
  96. allPlayLists = append(allPlayLists, *thisPlayList)
  97. }
  98. }else if (system_fs_matchFileExt(file, supportExt)){
  99. //This is an unsorted video file
  100. vpath, _ := realpathToVirtualpath(file, username)
  101. thisVideoFile := &videoFile{
  102. Filename: filepath.Base(file),
  103. Filepath: vpath,
  104. Ext: filepath.Ext(file),
  105. }
  106. unsortedFiles = append(unsortedFiles, *thisVideoFile)
  107. }
  108. }
  109. //Build the required objects from information
  110. thisViewPort.PlayLists = allPlayLists
  111. thisViewPort.UnsortedVideos = unsortedFiles
  112. thisViewPort.StorageName = dev.Name
  113. results = append(results,*thisViewPort)
  114. }
  115. //Format the results as JSON string and output
  116. jsonString, err := json.Marshal(results);
  117. if (err != nil){
  118. log.Println("[ArdPlayer] Unable to parse playlist")
  119. sendErrorResponse(w, "Unable to parse playlist.")
  120. return
  121. }
  122. sendJSONResponse(w, string(jsonString))
  123. return;
  124. }