-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
thumbs.go
154 lines (119 loc) Β· 3.56 KB
/
thumbs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package photoprism
import (
"errors"
"fmt"
"path/filepath"
"runtime/debug"
"sync"
"github.com/karrick/godirwalk"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// Thumbs represents a thumbnail image generator.
type Thumbs struct {
conf *config.Config
}
// NewThumbs returns a new thumbnails generator and expects the config as argument.
func NewThumbs(conf *config.Config) *Thumbs {
return &Thumbs{conf: conf}
}
// Start creates thumbnails for files in the originals and sidecar folders.
func (w *Thumbs) Start(dir string, force, originalsOnly bool) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("thumbs: %s (panic)\nstack: %s", r, debug.Stack())
log.Error(err)
}
}()
originalsPath := w.conf.OriginalsPath()
originalsDir := filepath.Join(originalsPath, dir)
sidecarPath := w.conf.SidecarPath()
sidecarDir := filepath.Join(sidecarPath, dir)
// Valid path provided?
if !fs.PathExists(originalsDir) {
return fmt.Errorf("thumbs: directory %s not found", clean.Log(originalsDir))
}
// Scan sidecar folder?
originalsOnly = originalsOnly || sidecarPath == "" || sidecarPath == originalsPath || !fs.PathExists(sidecarDir)
// Start creating thumbnails.
if _, err = w.Dir(originalsDir, force); err != nil || originalsOnly {
return err
} else if _, err = w.Dir(sidecarDir, force); err != nil {
return err
}
return nil
}
// Dir creates thumbnail images for files found in a given path.
func (w *Thumbs) Dir(dir string, force bool) (fs.Done, error) {
done := make(fs.Done)
if err := mutex.IndexWorker.Start(); err != nil {
return done, err
}
defer mutex.IndexWorker.Stop()
jobs := make(chan ThumbsJob)
thumbnailsPath := w.conf.ThumbCachePath()
// Start a fixed number of goroutines to read and digest files.
var wg sync.WaitGroup
var numWorkers = w.conf.IndexWorkers()
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go func() {
ThumbsWorker(jobs)
wg.Done()
}()
}
ignore := fs.NewIgnoreList(fs.PPIgnoreFilename, true, false)
ignore.Log = func(fileName string) {
log.Infof(`thumbs: ignored "%s"`, fs.RelName(fileName, dir))
}
handler := func(fileName string, info *godirwalk.Dirent) error {
defer func() {
if r := recover(); r != nil {
log.Errorf("thumbs: %s (panic)\nstack: %s", r, debug.Stack())
}
}()
if mutex.IndexWorker.Canceled() {
return errors.New("canceled")
}
isDir, _ := info.IsDirOrSymlinkToDir()
isSymlink := info.IsSymlink()
if skip, result := fs.SkipWalk(fileName, isDir, isSymlink, done, ignore); skip {
return result
}
mf, err := NewMediaFile(fileName)
if err != nil || mf.Empty() || !mf.IsPreviewImage() {
return nil
}
done[fileName] = fs.Processed
relativeName := mf.RelName(dir)
event.Publish("index.thumbnails", event.Data{
"fileName": relativeName,
"baseName": filepath.Base(relativeName),
"force": force,
})
jobs <- ThumbsJob{
mediaFile: mf,
path: thumbnailsPath,
force: force,
}
return nil
}
log.Infof("thumbs: processing %s", clean.Log(dir))
if err := ignore.Dir(dir); err != nil {
log.Infof("thumbs: %s", err)
}
err := godirwalk.Walk(dir, &godirwalk.Options{
ErrorCallback: func(fileName string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Callback: handler,
Unsorted: false,
FollowSymbolicLinks: true,
})
close(jobs)
wg.Wait()
return done, err
}