folder.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package metadata
  2. import (
  3. "errors"
  4. "image"
  5. "image/draw"
  6. "image/jpeg"
  7. "image/png"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "github.com/nfnt/resize"
  13. "imuslab.com/arozos/mod/filesystem"
  14. )
  15. /*
  16. Generate folder thumbnail from the containing files
  17. The preview is generated by overlapping 2 - 3 layers of images
  18. */
  19. func generateThumbnailForFolder(fsh *filesystem.FileSystemHandler, cacheFolder string, file string, generateOnly bool) (string, error) {
  20. if fsh.RequireBuffer {
  21. return "", nil
  22. }
  23. //Check if this folder has cache image folder
  24. cacheFolderInsideThisFolder := filepath.Join(file, "/.metadata/.cache")
  25. if !fileExists(cacheFolderInsideThisFolder) {
  26. //This folder do not have a cache folder
  27. return "", errors.New("No previewable files")
  28. }
  29. //Load the base template
  30. if !fileExists("web/img/system/folder-preview.png") {
  31. //Missing system files. Skip rendering
  32. return "", errors.New("Missing system template image file")
  33. }
  34. image1, err := os.Open("web/img/system/folder-preview.png")
  35. if err != nil {
  36. return "", err
  37. }
  38. baseTemplate, err := png.Decode(image1)
  39. if err != nil {
  40. return "", err
  41. }
  42. image1.Close()
  43. //Generate the base image
  44. b := baseTemplate.Bounds()
  45. resultThumbnail := image.NewRGBA(b)
  46. draw.Draw(resultThumbnail, b, baseTemplate, image.ZP, draw.Over)
  47. //Get cached file inside this folder, only include jpg (non folder)
  48. contentCache, _ := wGlob(filepath.Join(cacheFolderInsideThisFolder, "/*.jpg"))
  49. //Check if there are more than 1 file inside this folder that is cached
  50. if len(contentCache) > 1 {
  51. //More than 1 files. Render the image at the back
  52. image2, err := os.Open(contentCache[1])
  53. if err != nil {
  54. return "", err
  55. }
  56. backImage, err := jpeg.Decode(image2)
  57. if err != nil {
  58. return "", err
  59. }
  60. backImgOffset := image.Pt(155, 110)
  61. defer image2.Close()
  62. resizedBackImg := resize.Resize(250, 250, backImage, resize.Lanczos3)
  63. draw.Draw(resultThumbnail, resizedBackImg.Bounds().Add(backImgOffset), resizedBackImg, image.ZP, draw.Over)
  64. } else {
  65. //Nothing to preview inside this folder
  66. return "", errors.New("No previewable files")
  67. }
  68. //Render the top image
  69. image3, err := os.Open(contentCache[0])
  70. if err != nil {
  71. return "", errors.New("failed to open: " + err.Error())
  72. }
  73. topImage, err := jpeg.Decode(image3)
  74. if err != nil {
  75. //Fail to decode the image. Try to remove the damaged iamge file
  76. image3.Close()
  77. os.Remove(contentCache[0])
  78. log.Println("Failed to decode cahce image for: " + contentCache[0] + ". Removing thumbnail cache")
  79. return "", errors.New("failed to decode: " + err.Error())
  80. }
  81. defer image3.Close()
  82. topImageOffset := image.Pt(210, 210)
  83. resizedTopImage := resize.Resize(260, 260, topImage, resize.Lanczos3)
  84. draw.Draw(resultThumbnail, resizedTopImage.Bounds().Add(topImageOffset), resizedTopImage, image.ZP, draw.Over)
  85. outfile, err := os.Create(filepath.Join(cacheFolder, filepath.Base(file)+".png"))
  86. if err != nil {
  87. log.Fatalf("failed to create: %s", err)
  88. }
  89. png.Encode(outfile, resultThumbnail)
  90. outfile.Close()
  91. ctx, err := getImageAsBase64(fsh, cacheFolder+filepath.Base(file)+".png")
  92. return ctx, err
  93. }
  94. //Special glob function to handle file name containing wildcard characters
  95. func wGlob(path string) ([]string, error) {
  96. files, err := filepath.Glob(path)
  97. if err != nil {
  98. return []string{}, err
  99. }
  100. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  101. if len(files) == 0 {
  102. //Handle reverse check. Replace all [ and ] with ?
  103. newSearchPath := strings.ReplaceAll(path, "[", "?")
  104. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  105. //Scan with all the similar structure except [ and ]
  106. tmpFilelist, _ := filepath.Glob(newSearchPath)
  107. for _, file := range tmpFilelist {
  108. file = filepath.ToSlash(file)
  109. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  110. files = append(files, file)
  111. }
  112. }
  113. }
  114. }
  115. //Convert all filepaths to slash
  116. for i := 0; i < len(files); i++ {
  117. files[i] = filepath.ToSlash(files[i])
  118. }
  119. return files, nil
  120. }