metadata.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package metadata
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gorilla/websocket"
  16. hidden "imuslab.com/arozos/mod/filesystem/hidden"
  17. )
  18. /*
  19. This package is used to extract meta data from files like mp3 and mp4
  20. Also support image caching
  21. */
  22. type RenderHandler struct {
  23. renderingFiles sync.Map
  24. renderingFolder sync.Map
  25. }
  26. //Create a new RenderHandler
  27. func NewRenderHandler() *RenderHandler {
  28. return &RenderHandler{
  29. renderingFiles: sync.Map{},
  30. renderingFolder: sync.Map{},
  31. }
  32. }
  33. //Build cache for all files (non recursive) for the given filepath
  34. func (rh *RenderHandler) BuildCacheForFolder(path string) error {
  35. //Get a list of all files inside this path
  36. files, err := filepath.Glob(filepath.ToSlash(filepath.Clean(path)) + "/*")
  37. if err != nil {
  38. return err
  39. }
  40. for _, file := range files {
  41. //Load Cache in generate mode
  42. rh.LoadCache(file, true)
  43. }
  44. //Check if the cache folder has file. If not, remove it
  45. cachedFiles, _ := filepath.Glob(filepath.ToSlash(filepath.Clean(path)) + "/.cache/*")
  46. if len(cachedFiles) == 0 {
  47. os.RemoveAll(filepath.ToSlash(filepath.Clean(path)) + "/.cache/")
  48. }
  49. return nil
  50. }
  51. func (rh *RenderHandler) CacheExists(file string) bool {
  52. cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
  53. return fileExists(cacheFolder + filepath.Base(file) + ".jpg")
  54. }
  55. //Try to load a cache from file. If not exists, generate it now
  56. func (rh *RenderHandler) LoadCache(file string, generateOnly bool) (string, error) {
  57. //Create a cache folder
  58. cacheFolder := filepath.ToSlash(filepath.Clean(filepath.Dir(file))) + "/.cache/"
  59. os.Mkdir(cacheFolder, 0755)
  60. hidden.HideFile(cacheFolder)
  61. //Check if cache already exists. If yes, return the image from the cache folder
  62. if fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
  63. if generateOnly {
  64. //Only generate, do not return image
  65. return "", nil
  66. }
  67. //Check if the file is being writting by another process. If yes, wait for it
  68. counter := 0
  69. for rh.fileIsBusy(file) && counter < 15 {
  70. counter += 1
  71. time.Sleep(1 * time.Second)
  72. }
  73. //Time out and the file is still busy
  74. if rh.fileIsBusy(file) {
  75. log.Println("Process racing for cache file. Skipping", file)
  76. return "", errors.New("Process racing for cache file. Skipping")
  77. }
  78. //Read and return the image
  79. ctx, err := getImageAsBase64(cacheFolder + filepath.Base(file) + ".jpg")
  80. return ctx, err
  81. } else {
  82. //This file not exists yet. Check if it is being hold by another process already
  83. if rh.fileIsBusy(file) {
  84. log.Println("Process racing for cache file. Skipping", file)
  85. return "", errors.New("Process racing for cache file. Skipping")
  86. }
  87. }
  88. //Cache image not exists. Set this file to busy
  89. rh.renderingFiles.Store(file, "busy")
  90. //That object not exists. Generate cache image
  91. id4Formats := []string{".mp3", ".ogg", ".flac"}
  92. if inArray(id4Formats, strings.ToLower(filepath.Ext(file))) {
  93. img, err := generateThumbnailForAudio(cacheFolder, file, generateOnly)
  94. rh.renderingFiles.Delete(file)
  95. return img, err
  96. }
  97. //Generate cache for images
  98. imageFormats := []string{".png", ".jpeg", ".jpg"}
  99. if inArray(imageFormats, strings.ToLower(filepath.Ext(file))) {
  100. img, err := generateThumbnailForImage(cacheFolder, file, generateOnly)
  101. rh.renderingFiles.Delete(file)
  102. return img, err
  103. }
  104. vidFormats := []string{".mkv", ".mp4", ".webm", ".ogv", ".avi", ".rmvb"}
  105. if inArray(vidFormats, strings.ToLower(filepath.Ext(file))) {
  106. img, err := generateThumbnailForVideo(cacheFolder, file, generateOnly)
  107. rh.renderingFiles.Delete(file)
  108. return img, err
  109. }
  110. modelFormats := []string{".stl", ".obj"}
  111. if inArray(modelFormats, strings.ToLower(filepath.Ext(file))) {
  112. img, err := generateThumbnailForModel(cacheFolder, file, generateOnly)
  113. rh.renderingFiles.Delete(file)
  114. return img, err
  115. }
  116. //Other filters
  117. rh.renderingFiles.Delete(file)
  118. return "", errors.New("No supported format")
  119. }
  120. func (rh *RenderHandler) fileIsBusy(path string) bool {
  121. if rh == nil {
  122. log.Println("RenderHandler is null!")
  123. return true
  124. }
  125. _, ok := rh.renderingFiles.Load(path)
  126. if !ok {
  127. //File path is not being process by another process
  128. return false
  129. } else {
  130. return true
  131. }
  132. }
  133. func getImageAsBase64(path string) (string, error) {
  134. f, err := os.Open(path)
  135. if err != nil {
  136. return "", err
  137. }
  138. reader := bufio.NewReader(f)
  139. content, err := ioutil.ReadAll(reader)
  140. if err != nil {
  141. return "", err
  142. }
  143. encoded := base64.StdEncoding.EncodeToString(content)
  144. f.Close()
  145. return string(encoded), nil
  146. }
  147. //Load a list of folder cache from websocket
  148. func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request, rpath string) {
  149. //Get a list of files pending to be cached and sent
  150. targetPath := filepath.ToSlash(filepath.Clean(rpath))
  151. //Check if this path already exists another websocket ongoing connection.
  152. //If yes, disconnect the oldone
  153. oldc, ok := rh.renderingFolder.Load(targetPath)
  154. if ok {
  155. //Close and remove the old connection
  156. oldc.(*websocket.Conn).Close()
  157. }
  158. files, err := specialGlob(targetPath + "/*")
  159. if err != nil {
  160. w.WriteHeader(http.StatusInternalServerError)
  161. w.Write([]byte("500 - Internal Server Error"))
  162. return
  163. }
  164. //Upgrade the connection to websocket
  165. var upgrader = websocket.Upgrader{}
  166. c, err := upgrader.Upgrade(w, r, nil)
  167. if err != nil {
  168. log.Print("upgrade:", err)
  169. w.WriteHeader(http.StatusInternalServerError)
  170. w.Write([]byte("500 - Internal Server Error"))
  171. return
  172. }
  173. //Set this realpath as websocket connected
  174. rh.renderingFolder.Store(targetPath, c)
  175. //For each file, serve a cached image preview
  176. errorExists := false
  177. filesWithoutCache := []string{}
  178. //Updated implementation 24/12/2020: Load image with cache first before rendering those without
  179. for _, file := range files {
  180. if rh.CacheExists(file) == false {
  181. //Cache not exists. Render this later
  182. filesWithoutCache = append(filesWithoutCache, file)
  183. } else {
  184. //Cache exists. Send it out first
  185. cachedImage, err := rh.LoadCache(file, false)
  186. if err != nil {
  187. } else {
  188. jsonString, _ := json.Marshal([]string{filepath.Base(file), cachedImage})
  189. err := c.WriteMessage(1, jsonString)
  190. if err != nil {
  191. //Connection closed
  192. errorExists = true
  193. break
  194. }
  195. }
  196. }
  197. }
  198. //Render the remaining cache files
  199. for _, file := range filesWithoutCache {
  200. //Load the image cache
  201. cachedImage, err := rh.LoadCache(file, false)
  202. if err != nil {
  203. } else {
  204. jsonString, _ := json.Marshal([]string{filepath.Base(file), cachedImage})
  205. err := c.WriteMessage(1, jsonString)
  206. if err != nil {
  207. //Connection closed
  208. errorExists = true
  209. break
  210. }
  211. }
  212. }
  213. //Clear record from syncmap
  214. if !errorExists {
  215. //This ended normally. Delete the targetPath
  216. rh.renderingFolder.Delete(targetPath)
  217. }
  218. c.Close()
  219. }
  220. func specialGlob(path string) ([]string, error) {
  221. files, err := filepath.Glob(path)
  222. if err != nil {
  223. return []string{}, err
  224. }
  225. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  226. if len(files) == 0 {
  227. //Handle reverse check. Replace all [ and ] with *
  228. newSearchPath := strings.ReplaceAll(path, "[", "?")
  229. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  230. //Scan with all the similar structure except [ and ]
  231. tmpFilelist, _ := filepath.Glob(newSearchPath)
  232. for _, file := range tmpFilelist {
  233. file = filepath.ToSlash(file)
  234. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  235. files = append(files, file)
  236. }
  237. }
  238. }
  239. }
  240. //Convert all filepaths to slash
  241. for i := 0; i < len(files); i++ {
  242. files[i] = filepath.ToSlash(files[i])
  243. }
  244. return files, nil
  245. }