fix: don't wait for thumbnail generation if already cached

This commit is contained in:
Louis Dalibard 2024-06-10 22:21:27 +02:00
parent e7e819ff5c
commit 28d526ab74
2 changed files with 4 additions and 5 deletions

View File

@ -109,7 +109,7 @@ func HandleThumb(c *fiber.Ctx) error {
} }
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
pathSlice := strings.Split(req, "/") pathSlice := strings.Split(req, "/")
pathSlice = pathSlice[1:len(pathSlice)] pathSlice = pathSlice[1:]
pathBase, ok := config.Config.ServeDirs[pathSlice[0]] pathBase, ok := config.Config.ServeDirs[pathSlice[0]]
if ok { if ok {
pathSlice[0] = pathBase pathSlice[0] = pathBase
@ -121,12 +121,12 @@ func HandleThumb(c *fiber.Ctx) error {
thumbnail.GetThumbnail(c, completePath) thumbnail.GetThumbnail(c, completePath)
} else if errors.Is(err, os.ErrNotExist) { } else if errors.Is(err, os.ErrNotExist) {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!") return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
} else { } /* else {
// Schrodinger: file may or may not exist. See err for details. // Schrodinger: file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence // Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
} } */
} else { } else {
return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!") return c.Status(fiber.StatusNotFound).SendString("Sorry can't find that!")
} }

View File

@ -63,16 +63,15 @@ func Free() {
} }
func GetThumbnail(c *fiber.Ctx, completePath string) { func GetThumbnail(c *fiber.Ctx, completePath string) {
WaitForAvailable()
c.Set(fiber.HeaderContentType, "image") c.Set(fiber.HeaderContentType, "image")
thumbnailCacheMutex.RLock() thumbnailCacheMutex.RLock()
bytesThumb, ok := thumbnailCache[completePath] bytesThumb, ok := thumbnailCache[completePath]
thumbnailCacheMutex.RUnlock() thumbnailCacheMutex.RUnlock()
if ok { if ok {
c.Write(bytesThumb) c.Write(bytesThumb)
Free()
return return
} }
WaitForAvailable()
fileExt := filepath.Ext(completePath) fileExt := filepath.Ext(completePath)
if !slices.Contains(SupportedFileTypes, fileExt) { if !slices.Contains(SupportedFileTypes, fileExt) {