mirror of
https://github.com/owncast/owncast.git
synced 2024-11-28 19:19:06 +03:00
80 lines
2 KiB
Go
80 lines
2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/jellydator/ttlcache/v3"
|
|
"github.com/owncast/owncast/config"
|
|
"github.com/owncast/owncast/utils"
|
|
)
|
|
|
|
const (
|
|
contentTypeJPEG = "image/jpeg"
|
|
contentTypeGIF = "image/gif"
|
|
)
|
|
|
|
var previewThumbCache = ttlcache.New(
|
|
ttlcache.WithTTL[string, []byte](15),
|
|
ttlcache.WithCapacity[string, []byte](1),
|
|
ttlcache.WithDisableTouchOnHit[string, []byte](),
|
|
)
|
|
|
|
// GetThumbnail will return the thumbnail image as a response.
|
|
func GetThumbnail(w http.ResponseWriter, r *http.Request) {
|
|
imageFilename := "thumbnail.jpg"
|
|
imagePath := filepath.Join(config.TempDir, imageFilename)
|
|
httpCacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
|
inMemoryCacheTime := time.Duration(15) * time.Second
|
|
|
|
var imageBytes []byte
|
|
var err error
|
|
|
|
if previewThumbCache.Get(imagePath) != nil {
|
|
ci := previewThumbCache.Get(imagePath)
|
|
imageBytes = ci.Value()
|
|
} else if utils.DoesFileExists(imagePath) {
|
|
imageBytes, err = getImage(imagePath)
|
|
previewThumbCache.Set(imagePath, imageBytes, inMemoryCacheTime)
|
|
} else {
|
|
GetLogo(w, r)
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
GetLogo(w, r)
|
|
return
|
|
}
|
|
|
|
writeBytesAsImage(imageBytes, contentTypeJPEG, w, httpCacheTime)
|
|
}
|
|
|
|
// GetPreview will return the preview gif as a response.
|
|
func GetPreview(w http.ResponseWriter, r *http.Request) {
|
|
imageFilename := "preview.gif"
|
|
imagePath := filepath.Join(config.TempDir, imageFilename)
|
|
httpCacheTime := utils.GetCacheDurationSecondsForPath(imagePath)
|
|
inMemoryCacheTime := time.Duration(15) * time.Second
|
|
|
|
var imageBytes []byte
|
|
var err error
|
|
|
|
if previewThumbCache.Get(imagePath) != nil {
|
|
ci := previewThumbCache.Get(imagePath)
|
|
imageBytes = ci.Value()
|
|
} else if utils.DoesFileExists(imagePath) {
|
|
imageBytes, err = getImage(imagePath)
|
|
previewThumbCache.Set(imagePath, imageBytes, inMemoryCacheTime)
|
|
} else {
|
|
GetLogo(w, r)
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
GetLogo(w, r)
|
|
return
|
|
}
|
|
|
|
writeBytesAsImage(imageBytes, contentTypeGIF, w, httpCacheTime)
|
|
}
|