refactor(thumb): reset thumb status after renaming a file with no thumb available

pull/1690/head
Aaron Liu 2 years ago
parent f36e39991d
commit ae118c337e

@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"path"
"path/filepath"
"strings"
"time"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
@ -294,9 +296,14 @@ func GetFilesByUploadSession(sessionID string, uid uint) (*File, error) {
// Rename 重命名文件
func (file *File) Rename(new string) error {
if file.MetadataSerialized[ThumbStatusMetadataKey] == ThumbStatusNotAvailable {
if !strings.EqualFold(filepath.Ext(new), filepath.Ext(file.Name)) {
// Reset thumb status for new ext name.
if err := file.resetThumb(); err != nil {
return err
}
}
}
return DB.Model(&file).Set("gorm:association_autoupdate", false).Updates(map[string]interface{}{
"name": new,

@ -101,14 +101,14 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
defer w.Close()
errChan := make(chan error, 1)
go func() {
go func(errChan chan error) {
errChan <- fs.Handler.Put(newCtx, &fsctx.FileStream{
Mode: fsctx.Overwrite,
File: io.NopCloser(r),
Seeker: nil,
SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"),
})
}()
}(errChan)
if err = thumb.Generators.Generate(source, w, file.Name, model.GetSettingByNames(
"thumb_width",
@ -120,11 +120,9 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
"thumb_ffmpeg_path",
)); err != nil {
util.Log().Warning("Failed to generate thumb for %s: %s", file.Name, err)
if errors.Is(err, thumb.ErrNotAvailable) {
// Mark this file as no thumb available
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
}
w.Close()
<-errChan
return
}

@ -51,7 +51,7 @@ func NewThumbFromFile(file io.Reader, name string) (*Thumb, error) {
return nil, fmt.Errorf("unknown image format: %w", ErrPassThrough)
}
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse image: %w (%w)", err, ErrPassThrough)
}
return &Thumb{

Loading…
Cancel
Save