Skip to content

Commit

Permalink
Added error indication file instead of thumbnail to avoid re-generation
Browse files Browse the repository at this point in the history
of thumbnails that failed before.
  • Loading branch information
midstar committed Jan 5, 2020
1 parent f8dfec2 commit 9651e77
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ This can be fixed by automatically resizing the images on the server side by ena

enablepreview = on

It will take some time for the server to resize the image, but once the image has been resized, it will be cached in the same location as the thumbnails. Next time the image is viewed it will be balzing fast.
It will take some time for the server to resize the image, but once the image has been resized, it will be cached in the same location as the thumbnails. Next time the image is viewed it will be blazing fast.

The size of the preview images is set with the previewmaxside configuration parameter. To limit the height and width to 1000 pixels, set:

Expand Down
37 changes: 32 additions & 5 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ func (m *Media) thumbnailPath(relativeMediaPath string) (string, error) {
return m.getFullThumbPath(relativeThumbnailPath)
}

// errorIndicationPath returns the file path with the extension
// replaced with err.
func (m *Media) errorIndicationPath(anyPath string) string {
path, file := filepath.Split(anyPath)
ext := filepath.Ext(file)
file = strings.Replace(file, ext, ".err.txt", -1)
return filepath.Join(path, file)
}

// generateImageThumbnail generates a thumbnail from any of the supported
// images. Will create necessary subdirectories in the thumbpath.
func (m *Media) generateImageThumbnail(fullMediaPath, fullThumbPath string) error {
Expand Down Expand Up @@ -404,14 +413,24 @@ func (m *Media) generateThumbnail(relativeFilePath string) (string, error) {
if err == nil {
return thumbFileName, nil // Thumb already generated
}
errorIndicationFile := m.errorIndicationPath(thumbFileName)
_, err = os.Stat(errorIndicationFile) // Check if file exist
if err == nil {
// File has failed to be generated before, don't bother
// trying to re-generate it.
msg := fmt.Sprintf("Skipping generate thumbnail for %s since it has failed before.",
relativeFilePath)
llog.Trace(msg)
return "", fmt.Errorf(msg)
}

// No thumb exist. Create it
llog.Info("Creating new thumbnail for %s", relativeFilePath)
startTime := time.Now().UnixNano()
fullMediaPath, err := m.getFullMediaPath(relativeFilePath)
if err != nil {
llog.Error("%s", err)
return thumbFileName, err
return "", err
}
if m.isVideo(fullMediaPath) {
err = m.generateVideoThumbnail(fullMediaPath, thumbFileName)
Expand All @@ -420,7 +439,14 @@ func (m *Media) generateThumbnail(relativeFilePath string) (string, error) {
}
if err != nil {
llog.Error("%s", err)
return thumbFileName, err
// To avoid generate the file again, create an error indication file
errorFile, err2 := os.Create(errorIndicationFile)
if err2 == nil {
defer errorFile.Close()
errorFile.WriteString(err.Error())
llog.Info("Created: %s", errorIndicationFile)
}
return "", err
}
deltaTime := (time.Now().UnixNano() - startTime) / int64(time.Millisecond)
llog.Info("Thumbnail done for %s (conversion time: %d ms)",
Expand Down Expand Up @@ -565,9 +591,10 @@ func (m *Media) extractVideoScreenshot(inFilePath, outFilePath string) error {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("%s %s\nError: %s\nStdout: %s\nStderr: %s",
ffmpegCmd, strings.Join(ffmpegArgs, " "), err, stdout.String(), stderr.String())
_, outFileErr := os.Stat(outFilePath)
if err != nil || outFileErr != nil {
return fmt.Errorf("%s %s\nStdout: %s\nStderr: %s",
ffmpegCmd, strings.Join(ffmpegArgs, " "), stdout.String(), stderr.String())
}
return nil
}
Expand Down
11 changes: 10 additions & 1 deletion media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,22 @@ func TestWriteThumbnail(t *testing.T) {
// Video - only if video is supported
if media.videoThumbnailSupport() {
tWriteThumbnail(t, media, "video.mp4", "tmpout/TestWriteThumbnail/video.jpg", false)

// Test invalid
tWriteThumbnail(t, media, "invalidvideo.mp4", "tmpout/TestWriteThumbnail/invalidvideo.jpg", true)
// Check that error indication file is created
assertFileExist(t, "", "tmpcache/TestWriteThumbnail/_invalidvideo.err.txt")
}

// Non existing file
tWriteThumbnail(t, media, "dont_exist.jpg", "tmpout/TestWriteThumbnail/dont_exist.jpg", true)

// Invalid file
tWriteThumbnail(t, media, "invalid.jpg", "tmpout/TestWriteThumbnail/invalid.jpg", true)
// Check that error indication file is created
assertFileExist(t, "", "tmpcache/TestWriteThumbnail/_invalid.err.txt")
// Generate again - just for coverage
tWriteThumbnail(t, media, "invalid.jpg", "tmpout/TestWriteThumbnail/invalid.jpg", true)

// Disable thumb cache
media = createMedia(box, "testmedia", "tmpcache/TestWriteThumbnail", false, false, false, true, false, 0)
Expand Down Expand Up @@ -415,7 +424,7 @@ func TestGenerateVideoThumbnail(t *testing.T) {
// Test some invalid
err := media.generateVideoThumbnail("nonexisting.mp4", tmp+"dont_matter.jpg")
assertExpectErr(t, "", err)
err = media.generateVideoThumbnail("invalidvideo.mp4", tmp+"dont_matter_png.jpg")
err = media.generateVideoThumbnail("invalidvideo.mp4", tmp+"/invalidvideo.jpg")
assertExpectErr(t, "", err)
}

Expand Down

0 comments on commit 9651e77

Please sign in to comment.