metadata.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.Join(filepath.Clean(path), "/.metadata/.cache/*")))
  47. if len(cachedFiles) == 0 {
  48. os.RemoveAll(filepath.ToSlash(filepath.Join(filepath.Clean(path), "/.metadata/.cache/")) + "/")
  49. }
  50. return nil
  51. }
  52. func (rh *RenderHandler) LoadCacheAsBytes(file string, generateOnly bool) ([]byte, error) {
  53. b64, err := rh.LoadCache(file, generateOnly)
  54. if err != nil {
  55. return []byte{}, err
  56. }
  57. resultingBytes, _ := base64.StdEncoding.DecodeString(b64)
  58. return resultingBytes, nil
  59. }
  60. //Try to load a cache from file. If not exists, generate it now
  61. func (rh *RenderHandler) LoadCache(file string, generateOnly bool) (string, error) {
  62. //Create a cache folder
  63. cacheFolder := filepath.ToSlash(filepath.Join(filepath.Clean(filepath.Dir(file)), "/.metadata/.cache/") + "/")
  64. os.MkdirAll(cacheFolder, 0755)
  65. hidden.HideFile(filepath.Dir(filepath.Clean(cacheFolder)))
  66. hidden.HideFile(cacheFolder)
  67. //Check if cache already exists. If yes, return the image from the cache folder
  68. if CacheExists(file) {
  69. if generateOnly {
  70. //Only generate, do not return image
  71. return "", nil
  72. }
  73. //Allow thumbnail to be either jpg or png file
  74. ext := ".jpg"
  75. if !fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
  76. ext = ".png"
  77. }
  78. //Updates 02/10/2021: Check if the source file is newer than the cache. Update the cache if true
  79. if mtime(file) > mtime(cacheFolder+filepath.Base(file)+ext) {
  80. //File is newer than cache. Delete the cache
  81. os.Remove(cacheFolder + filepath.Base(file) + ext)
  82. } else {
  83. //Check if the file is being writting by another process. If yes, wait for it
  84. counter := 0
  85. for rh.fileIsBusy(file) && counter < 15 {
  86. counter += 1
  87. time.Sleep(1 * time.Second)
  88. }
  89. //Time out and the file is still busy
  90. if rh.fileIsBusy(file) {
  91. log.Println("Process racing for cache file. Skipping", file)
  92. return "", errors.New("Process racing for cache file. Skipping")
  93. }
  94. //Read and return the image
  95. ctx, err := getImageAsBase64(cacheFolder + filepath.Base(file) + ext)
  96. return ctx, err
  97. }
  98. } else {
  99. //This file not exists yet. Check if it is being hold by another process already
  100. if rh.fileIsBusy(file) {
  101. log.Println("Process racing for cache file. Skipping", file)
  102. return "", errors.New("Process racing for cache file. Skipping")
  103. }
  104. }
  105. //Cache image not exists. Set this file to busy
  106. rh.renderingFiles.Store(file, "busy")
  107. //That object not exists. Generate cache image
  108. //Audio formats that might contains id4 thumbnail
  109. id4Formats := []string{".mp3", ".ogg", ".flac"}
  110. if inArray(id4Formats, strings.ToLower(filepath.Ext(file))) {
  111. img, err := generateThumbnailForAudio(cacheFolder, file, generateOnly)
  112. rh.renderingFiles.Delete(file)
  113. return img, err
  114. }
  115. //Generate resized image for images
  116. imageFormats := []string{".png", ".jpeg", ".jpg"}
  117. if inArray(imageFormats, strings.ToLower(filepath.Ext(file))) {
  118. img, err := generateThumbnailForImage(cacheFolder, file, generateOnly)
  119. rh.renderingFiles.Delete(file)
  120. return img, err
  121. }
  122. //Video formats, extract from the 5 sec mark
  123. vidFormats := []string{".mkv", ".mp4", ".webm", ".ogv", ".avi", ".rmvb"}
  124. if inArray(vidFormats, strings.ToLower(filepath.Ext(file))) {
  125. img, err := generateThumbnailForVideo(cacheFolder, file, generateOnly)
  126. rh.renderingFiles.Delete(file)
  127. return img, err
  128. }
  129. //3D Model Formats
  130. modelFormats := []string{".stl", ".obj"}
  131. if inArray(modelFormats, strings.ToLower(filepath.Ext(file))) {
  132. img, err := generateThumbnailForModel(cacheFolder, file, generateOnly)
  133. rh.renderingFiles.Delete(file)
  134. return img, err
  135. }
  136. //Photoshop file
  137. if strings.ToLower(filepath.Ext(file)) == ".psd" {
  138. img, err := generateThumbnailForPSD(cacheFolder, file, generateOnly)
  139. rh.renderingFiles.Delete(file)
  140. return img, err
  141. }
  142. //Folder preview renderer
  143. if isDir(file) && len(filepath.Base(file)) > 0 && filepath.Base(file)[:1] != "." {
  144. img, err := generateThumbnailForFolder(cacheFolder, file, generateOnly)
  145. rh.renderingFiles.Delete(file)
  146. return img, err
  147. }
  148. //Other filters
  149. rh.renderingFiles.Delete(file)
  150. return "", errors.New("No supported format")
  151. }
  152. func (rh *RenderHandler) fileIsBusy(path string) bool {
  153. if rh == nil {
  154. log.Println("RenderHandler is null!")
  155. return true
  156. }
  157. _, ok := rh.renderingFiles.Load(path)
  158. if !ok {
  159. //File path is not being process by another process
  160. return false
  161. } else {
  162. return true
  163. }
  164. }
  165. func getImageAsBase64(path string) (string, error) {
  166. f, err := os.Open(path)
  167. if err != nil {
  168. return "", err
  169. }
  170. //Added Seek to 0,0 function to prevent failed to decode: Unexpected EOF error
  171. f.Seek(0, 0)
  172. reader := bufio.NewReader(f)
  173. content, err := ioutil.ReadAll(reader)
  174. if err != nil {
  175. return "", err
  176. }
  177. encoded := base64.StdEncoding.EncodeToString(content)
  178. f.Close()
  179. return string(encoded), nil
  180. }
  181. //Load a list of folder cache from websocket, pass in "" (empty string) for default sorting method
  182. func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request, rpath string, sortmode string) {
  183. //Get a list of files pending to be cached and sent
  184. targetPath := filepath.ToSlash(filepath.Clean(rpath))
  185. //Check if this path already exists another websocket ongoing connection.
  186. //If yes, disconnect the oldone
  187. oldc, ok := rh.renderingFolder.Load(targetPath)
  188. if ok {
  189. //Close and remove the old connection
  190. oldc.(*websocket.Conn).Close()
  191. }
  192. files, err := specialGlob(targetPath + "/*")
  193. if err != nil {
  194. w.WriteHeader(http.StatusInternalServerError)
  195. w.Write([]byte("500 - Internal Server Error"))
  196. return
  197. }
  198. //Upgrade the connection to websocket
  199. var upgrader = websocket.Upgrader{}
  200. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  201. c, err := upgrader.Upgrade(w, r, nil)
  202. if err != nil {
  203. log.Print("upgrade:", err)
  204. w.WriteHeader(http.StatusInternalServerError)
  205. w.Write([]byte("500 - Internal Server Error"))
  206. return
  207. }
  208. //Set this realpath as websocket connected
  209. rh.renderingFolder.Store(targetPath, c)
  210. //For each file, serve a cached image preview
  211. errorExists := false
  212. filesWithoutCache := []string{}
  213. //Updates implementation 02/10/2021: Load thumbnail of files first before folder and apply user preference sort mode
  214. if sortmode == "" {
  215. sortmode = "default"
  216. }
  217. pendingFiles := []string{}
  218. pendingFolders := []string{}
  219. for _, file := range files {
  220. if isDir(file) {
  221. pendingFiles = append(pendingFiles, file)
  222. } else {
  223. pendingFolders = append(pendingFolders, file)
  224. }
  225. }
  226. pendingFiles = append(pendingFiles, pendingFolders...)
  227. files = fssort.SortFileList(pendingFiles, sortmode)
  228. //Updated implementation 24/12/2020: Load image with cache first before rendering those without
  229. for _, file := range files {
  230. if !CacheExists(file) {
  231. //Cache not exists. Render this later
  232. filesWithoutCache = append(filesWithoutCache, file)
  233. } else {
  234. //Cache exists. Send it out first
  235. cachedImage, err := rh.LoadCache(file, false)
  236. if err != nil {
  237. } else {
  238. jsonString, _ := json.Marshal([]string{filepath.Base(file), cachedImage})
  239. err := c.WriteMessage(1, jsonString)
  240. if err != nil {
  241. //Connection closed
  242. errorExists = true
  243. break
  244. }
  245. }
  246. }
  247. }
  248. retryList := []string{}
  249. //Render the remaining cache files
  250. for _, file := range filesWithoutCache {
  251. //Load the image cache
  252. cachedImage, err := rh.LoadCache(file, false)
  253. if err != nil {
  254. //Unable to load this file's cache. Push it to retry list
  255. retryList = append(retryList, file)
  256. } else {
  257. jsonString, _ := json.Marshal([]string{filepath.Base(file), cachedImage})
  258. err := c.WriteMessage(1, jsonString)
  259. if err != nil {
  260. //Connection closed
  261. errorExists = true
  262. break
  263. }
  264. }
  265. }
  266. //Process the retry list after some wait time
  267. if len(retryList) > 0 {
  268. time.Sleep(1000 * time.Millisecond)
  269. for _, file := range retryList {
  270. //Load the image cache
  271. cachedImage, err := rh.LoadCache(file, false)
  272. if err != nil {
  273. } else {
  274. jsonString, _ := json.Marshal([]string{filepath.Base(file), cachedImage})
  275. err := c.WriteMessage(1, jsonString)
  276. if err != nil {
  277. //Connection closed
  278. errorExists = true
  279. break
  280. }
  281. }
  282. }
  283. }
  284. //Clear record from syncmap
  285. if !errorExists {
  286. //This ended normally. Delete the targetPath
  287. rh.renderingFolder.Delete(targetPath)
  288. }
  289. c.Close()
  290. }
  291. //Check if the cache for a file exists
  292. func CacheExists(file string) bool {
  293. cacheFolder := filepath.ToSlash(filepath.Join(filepath.Clean(filepath.Dir(file)), "/.metadata/.cache/") + "/")
  294. return fileExists(cacheFolder+filepath.Base(file)+".jpg") || fileExists(cacheFolder+filepath.Base(file)+".png")
  295. }
  296. //Get cache path for this file, given realpath
  297. func GetCacheFilePath(file string) (string, error) {
  298. if CacheExists(file) {
  299. cacheFolder := filepath.ToSlash(filepath.Join(filepath.Clean(filepath.Dir(file)), "/.metadata/.cache/") + "/")
  300. if fileExists(cacheFolder + filepath.Base(file) + ".jpg") {
  301. return cacheFolder + filepath.Base(file) + ".jpg", nil
  302. } else if fileExists(cacheFolder + filepath.Base(file) + ".png") {
  303. return cacheFolder + filepath.Base(file) + ".png", nil
  304. } else {
  305. return "", errors.New("Unable to resolve thumbnail cache location")
  306. }
  307. } else {
  308. return "", errors.New("No thumbnail cached for this file")
  309. }
  310. }
  311. //Remove cache if exists, given realpath
  312. func RemoveCache(file string) error {
  313. if CacheExists(file) {
  314. cachePath, err := GetCacheFilePath(file)
  315. //log.Println("Removing ", cachePath, err)
  316. if err != nil {
  317. return err
  318. }
  319. //Remove the thumbnail cache
  320. os.Remove(cachePath)
  321. return nil
  322. } else {
  323. //log.Println("Cache not exists: ", file)
  324. return errors.New("Thumbnail cache not exists for this file")
  325. }
  326. }
  327. func specialGlob(path string) ([]string, error) {
  328. files, err := filepath.Glob(path)
  329. if err != nil {
  330. return []string{}, err
  331. }
  332. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  333. if len(files) == 0 {
  334. //Handle reverse check. Replace all [ and ] with *
  335. newSearchPath := strings.ReplaceAll(path, "[", "?")
  336. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  337. //Scan with all the similar structure except [ and ]
  338. tmpFilelist, _ := filepath.Glob(newSearchPath)
  339. for _, file := range tmpFilelist {
  340. file = filepath.ToSlash(file)
  341. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  342. files = append(files, file)
  343. }
  344. }
  345. }
  346. }
  347. //Convert all filepaths to slash
  348. for i := 0; i < len(files); i++ {
  349. files[i] = filepath.ToSlash(files[i])
  350. }
  351. return files, nil
  352. }