images.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package controllers
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. "github.com/owncast/owncast/config"
  6. "github.com/owncast/owncast/utils"
  7. )
  8. const (
  9. contentTypeJPEG = "image/jpeg"
  10. contentTypeGIF = "image/gif"
  11. )
  12. // GetThumbnail will return the thumbnail image as a response.
  13. func GetThumbnail(w http.ResponseWriter, r *http.Request) {
  14. imageFilename := "thumbnail.jpg"
  15. imagePath := filepath.Join(config.TempDir, imageFilename)
  16. var imageBytes []byte
  17. var err error
  18. if utils.DoesFileExists(imagePath) {
  19. imageBytes, err = getImage(imagePath)
  20. } else {
  21. GetLogo(w, r)
  22. return
  23. }
  24. if err != nil {
  25. GetLogo(w, r)
  26. return
  27. }
  28. cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
  29. writeBytesAsImage(imageBytes, contentTypeJPEG, w, cacheTime)
  30. }
  31. // GetPreview will return the preview gif as a response.
  32. func GetPreview(w http.ResponseWriter, r *http.Request) {
  33. imageFilename := "preview.gif"
  34. imagePath := filepath.Join(config.TempDir, imageFilename)
  35. var imageBytes []byte
  36. var err error
  37. if utils.DoesFileExists(imagePath) {
  38. imageBytes, err = getImage(imagePath)
  39. } else {
  40. GetLogo(w, r)
  41. return
  42. }
  43. if err != nil {
  44. GetLogo(w, r)
  45. return
  46. }
  47. cacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
  48. writeBytesAsImage(imageBytes, contentTypeGIF, w, cacheTime)
  49. }