metadata.go 8.9 KB

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