refactor(thumb): reset thumb status after renaming a file with no thumb available
This commit is contained in:
parent
f36e39991d
commit
ae118c337e
3 changed files with 15 additions and 10 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
|
@ -294,8 +296,13 @@ func GetFilesByUploadSession(sessionID string, uid uint) (*File, error) {
|
|||
|
||||
// Rename 重命名文件
|
||||
func (file *File) Rename(new string) error {
|
||||
if err := file.resetThumb(); err != nil {
|
||||
return err
|
||||
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{}{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
_ = 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…
Add table
Reference in a new issue