@@ -2,19 +2,18 @@ package dms
22
33import (
44 "bytes"
5+ "context"
56 "crypto/md5"
67 "encoding/xml"
78 "errors"
89 "fmt"
910 "io"
1011 "io/ioutil"
11- "math/rand"
1212 "net"
1313 "net/http"
1414 "net/http/pprof"
1515 "net/url"
1616 "os"
17- "os/exec"
1817 "os/user"
1918 "path"
2019 "path/filepath"
@@ -652,32 +651,22 @@ func (s *Server) filePath(_path string) string {
652651
653652func (me * Server ) serveIcon (w http.ResponseWriter , r * http.Request ) {
654653 filePath := me .filePath (r .URL .Query ().Get ("path" ))
655- c := r .URL .Query ().Get ("c" )
656- if c == "" {
657- c = "png"
658- }
659- args := []string {}
660- _ , fqThumbnail := os .LookupEnv ("DMS_THUMBNAIL_FULLQUALITY" )
661- if fqThumbnail {
662- args = append (args , "-s" , "0" , "-q" , "10" )
663- }
664-
665- _ , randThumbnail := os .LookupEnv ("DMS_THUMBNAIL_RANDOM" )
666- if randThumbnail {
667- args = append (args , "-t" , strconv .Itoa (rand .Intn (100 )))
668- }
654+
655+ // Create a context with timeout for thumbnail generation
656+ ctx , cancel := context .WithTimeout (r .Context (), 30 * time .Second )
657+ defer cancel ()
669658
670- args = append (args , "-i" , filePath , "-o" , "/dev/stdout" , "-c" + c )
671- cmd := exec .Command ("ffmpegthumbnailer" , args ... )
672- // cmd.Stderr = os.Stderr
673- body , err := cmd .Output ()
659+ // Generate thumbnail using ffmpeg
660+ body , err := generateThumbnailFFmpeg (ctx , filePath )
674661 if err != nil {
675- // serve 1st Icon if no ffmpegthumbnailer
662+ // serve 1st Icon if thumbnail generation fails
676663 w .Header ().Set ("Content-Type" , me .Icons [0 ].Mimetype )
677664 http .ServeContent (w , r , "" , time.Time {}, bytes .NewReader (me .Icons [0 ].Bytes ))
678- // http.Error(w, err.Error(), http.StatusInternalServerError)
679665 return
680666 }
667+
668+ // Serve the generated thumbnail as PNG
669+ w .Header ().Set ("Content-Type" , "image/png" )
681670 http .ServeContent (w , r , "" , time .Now (), bytes .NewReader (body ))
682671}
683672
0 commit comments