metadata.go 12 KB

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