mirror of https://github.com/cloudreve/Cloudreve
refactor(thumb): reset thumb status after renaming a file with no thumb available
parent
f36e39991d
commit
ae118c337e
|
@ -6,6 +6,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||||
|
@ -294,9 +296,14 @@ func GetFilesByUploadSession(sessionID string, uid uint) (*File, error) {
|
||||||
|
|
||||||
// Rename 重命名文件
|
// Rename 重命名文件
|
||||||
func (file *File) Rename(new string) error {
|
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 {
|
if err := file.resetThumb(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return DB.Model(&file).Set("gorm:association_autoupdate", false).Updates(map[string]interface{}{
|
return DB.Model(&file).Set("gorm:association_autoupdate", false).Updates(map[string]interface{}{
|
||||||
"name": new,
|
"name": new,
|
||||||
|
|
|
@ -101,14 +101,14 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
|
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
go func() {
|
go func(errChan chan error) {
|
||||||
errChan <- fs.Handler.Put(newCtx, &fsctx.FileStream{
|
errChan <- fs.Handler.Put(newCtx, &fsctx.FileStream{
|
||||||
Mode: fsctx.Overwrite,
|
Mode: fsctx.Overwrite,
|
||||||
File: io.NopCloser(r),
|
File: io.NopCloser(r),
|
||||||
Seeker: nil,
|
Seeker: nil,
|
||||||
SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"),
|
SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"),
|
||||||
})
|
})
|
||||||
}()
|
}(errChan)
|
||||||
|
|
||||||
if err = thumb.Generators.Generate(source, w, file.Name, model.GetSettingByNames(
|
if err = thumb.Generators.Generate(source, w, file.Name, model.GetSettingByNames(
|
||||||
"thumb_width",
|
"thumb_width",
|
||||||
|
@ -120,11 +120,9 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
|
||||||
"thumb_ffmpeg_path",
|
"thumb_ffmpeg_path",
|
||||||
)); err != nil {
|
)); err != nil {
|
||||||
util.Log().Warning("Failed to generate thumb for %s: %s", file.Name, err)
|
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)
|
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
|
||||||
}
|
w.Close()
|
||||||
|
<-errChan
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ func NewThumbFromFile(file io.Reader, name string) (*Thumb, error) {
|
||||||
return nil, fmt.Errorf("unknown image format: %w", ErrPassThrough)
|
return nil, fmt.Errorf("unknown image format: %w", ErrPassThrough)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to parse image: %w (%w)", err, ErrPassThrough)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Thumb{
|
return &Thumb{
|
||||||
|
|
Loading…
Reference in New Issue