renderer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package renderer
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. )
  11. /*
  12. This package is used to extract meta data from files like mp3 and mp4
  13. Also support image caching
  14. */
  15. type RenderHandler struct {
  16. renderingFiles sync.Map
  17. renderingFolder sync.Map
  18. }
  19. // Create a new RenderHandler
  20. func NewRenderHandler() *RenderHandler {
  21. return &RenderHandler{
  22. renderingFiles: sync.Map{},
  23. renderingFolder: sync.Map{},
  24. }
  25. }
  26. // RenderThumbnail generates a thumbnail for the given input file and saves it to the output folder
  27. func (rh *RenderHandler) RenderThumbnail(inputFile string, outputFolder string) error {
  28. if rh.fileIsBusy(inputFile) {
  29. return errors.New("file is rendering")
  30. }
  31. // Check if the cache file exists and is newer than the input file
  32. cacheFile := filepath.Join(outputFolder, filepath.Base(inputFile)+".jpg")
  33. cacheInfo, err := os.Stat(cacheFile)
  34. if err == nil {
  35. inputInfo, err := os.Stat(inputFile)
  36. if err != nil {
  37. // File not found, return error
  38. return err
  39. }
  40. if cacheInfo.ModTime().After(inputInfo.ModTime()) {
  41. // Cache file is newer, return the base64 encoded image
  42. return nil
  43. }
  44. }
  45. //Cache image not exists. Set this file to busy
  46. rh.renderingFiles.Store(inputFile, "busy")
  47. inputFileExt := strings.ToLower(filepath.Ext(inputFile))
  48. //That object not exists. Generate cache image
  49. //Audio formats that might contains id4 thumbnail
  50. id4Formats := []string{".mp3", ".ogg", ".flac"}
  51. if stringInSlice(inputFileExt, id4Formats) {
  52. err := generateThumbnailForAudio(inputFile, outputFolder)
  53. rh.renderingFiles.Delete(inputFileExt)
  54. return err
  55. }
  56. //Generate resized image for images
  57. imageFormats := []string{".png", ".jpeg", ".jpg"}
  58. if stringInSlice(inputFileExt, imageFormats) {
  59. err := generateThumbnailForImage(inputFile, outputFolder)
  60. rh.renderingFiles.Delete(inputFileExt)
  61. return err
  62. }
  63. //Video formats, extract from the 5 sec mark
  64. vidFormats := []string{".mkv", ".mp4", ".webm", ".ogv", ".avi", ".rmvb"}
  65. if stringInSlice(inputFileExt, vidFormats) {
  66. err := generateThumbnailForVideo(inputFile, outputFolder)
  67. rh.renderingFiles.Delete(inputFileExt)
  68. return err
  69. }
  70. //3D Model Formats
  71. modelFormats := []string{".stl", ".obj"}
  72. if stringInSlice(inputFileExt, modelFormats) {
  73. err := generateThumbnailForModel(inputFile, outputFolder)
  74. rh.renderingFiles.Delete(inputFileExt)
  75. return err
  76. }
  77. //Photoshop file
  78. if inputFileExt == ".psd" {
  79. err := generateThumbnailForPSD(inputFile, outputFolder)
  80. rh.renderingFiles.Delete(inputFileExt)
  81. return err
  82. }
  83. //Other filters
  84. rh.renderingFiles.Delete(inputFileExt)
  85. return errors.New("No supported format")
  86. }
  87. func stringInSlice(path string, slice []string) bool {
  88. for _, item := range slice {
  89. if item == path {
  90. return true
  91. }
  92. }
  93. return false
  94. }
  95. func (rh *RenderHandler) fileIsBusy(path string) bool {
  96. if rh == nil {
  97. log.Println("RenderHandler is null!")
  98. return true
  99. }
  100. _, ok := rh.renderingFiles.Load(path)
  101. if !ok {
  102. //File path is not being process by another process
  103. return false
  104. } else {
  105. return true
  106. }
  107. }
  108. // Get the image as base64 string
  109. func getImageAsBase64(inputFile string) (string, error) {
  110. content, err := os.ReadFile(inputFile)
  111. if err != nil {
  112. return "", err
  113. }
  114. encoded := base64.StdEncoding.EncodeToString(content)
  115. return string(encoded), nil
  116. }