forked from Mirrors/gomuks
media: fix saving thumbnail hashes
This commit is contained in:
parent
947a853bae
commit
9fd50a6ae3
1 changed files with 23 additions and 3 deletions
|
@ -90,10 +90,15 @@ func (gmx *Gomuks) downloadMediaFromCache(ctx context.Context, w http.ResponseWr
|
||||||
}
|
}
|
||||||
if entry.ThumbnailHash == nil {
|
if entry.ThumbnailHash == nil {
|
||||||
err := gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize)
|
err := gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize)
|
||||||
if err != nil {
|
if errors.Is(err, os.ErrNotExist) && !force {
|
||||||
|
return false
|
||||||
|
} else if err != nil {
|
||||||
log.Err(err).Msg("Failed to generate avatar thumbnail")
|
log.Err(err).Msg("Failed to generate avatar thumbnail")
|
||||||
|
gmx.saveMediaCacheEntryWithThumbnail(ctx, entry, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return true
|
return true
|
||||||
|
} else {
|
||||||
|
gmx.saveMediaCacheEntryWithThumbnail(ctx, entry, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hash = entry.ThumbnailHash
|
hash = entry.ThumbnailHash
|
||||||
|
@ -101,13 +106,15 @@ func (gmx *Gomuks) downloadMediaFromCache(ctx context.Context, w http.ResponseWr
|
||||||
cacheFile, err := os.Open(gmx.cacheEntryToPath(hash[:]))
|
cacheFile, err := os.Open(gmx.cacheEntryToPath(hash[:]))
|
||||||
if useThumbnail && errors.Is(err, os.ErrNotExist) {
|
if useThumbnail && errors.Is(err, os.ErrNotExist) {
|
||||||
err = gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize)
|
err = gmx.generateAvatarThumbnail(entry, gmx.Config.Media.ThumbnailSize)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) && !force {
|
||||||
// Fall through to next error handler
|
return false
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Err(err).Msg("Failed to generate avatar thumbnail")
|
log.Err(err).Msg("Failed to generate avatar thumbnail")
|
||||||
|
gmx.saveMediaCacheEntryWithThumbnail(ctx, entry, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
|
gmx.saveMediaCacheEntryWithThumbnail(ctx, entry, nil)
|
||||||
cacheFile, err = os.Open(gmx.cacheEntryToPath(hash[:]))
|
cacheFile, err = os.Open(gmx.cacheEntryToPath(hash[:]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,6 +158,19 @@ func cacheEntryToHeaders(w http.ResponseWriter, entry *database.Media, thumbnail
|
||||||
w.Header().Set("ETag", entry.ETag(thumbnail))
|
w.Header().Set("ETag", entry.ETag(thumbnail))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gmx *Gomuks) saveMediaCacheEntryWithThumbnail(ctx context.Context, entry *database.Media, err error) {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
entry.ThumbnailError = err.Error()
|
||||||
|
}
|
||||||
|
err = gmx.Client.DB.Media.Put(ctx, entry)
|
||||||
|
if err != nil {
|
||||||
|
zerolog.Ctx(ctx).Err(err).Msg("Failed to save cache entry after generating thumbnail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (gmx *Gomuks) generateAvatarThumbnail(entry *database.Media, size int) error {
|
func (gmx *Gomuks) generateAvatarThumbnail(entry *database.Media, size int) error {
|
||||||
cacheFile, err := os.Open(gmx.cacheEntryToPath(entry.Hash[:]))
|
cacheFile, err := os.Open(gmx.cacheEntryToPath(entry.Hash[:]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue