-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (129 loc) · 3.54 KB
/
main.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
155
156
157
158
159
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"image"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"github.com/disintegration/imaging"
_ "github.com/mattn/go-sqlite3"
"github.com/nickalie/go-webpbin"
"github.com/panjf2000/ants"
"go.uber.org/multierr"
)
const timelinerRepo = "./timeliner_repo"
func main() {
log.Println("Opening database...")
db, err := sql.Open("sqlite3", timelinerRepo+"/index.db")
if err != nil {
log.Fatalf("Open database: %v", err)
}
defer db.Close()
log.Println("Quering database...")
rows, err := db.Query(`
select c.name, i.original_id, i.data_file from items i
inner join collection_items ci on ci.item_id = i.id
inner join collections c on ci.collection_id = c.id
`)
if err != nil {
log.Fatalf("Query database: %v", err)
}
defer rows.Close()
// will force download of any webp binary
webpbin.NewCWebP().BinWrapper.Run()
p, err := ants.NewPool(25)
if err != nil {
log.Fatalf("New pool: %v", err)
}
var (
errs error
photos = make(map[string][]string)
mu sync.Mutex
wg sync.WaitGroup
)
log.Println("Looping over rows...")
for rows.Next() {
var (
albumName string
photoID string
photoPath string
)
if err = rows.Scan(&albumName, &photoID, &photoPath); err != nil {
log.Printf("scan row: %v", err)
continue
}
wg.Add(1)
p.Submit(func() {
defer wg.Done()
fullPhotoPath := filepath.Join(timelinerRepo, photoPath)
if _, err = os.Stat(fullPhotoPath); err != nil {
// image not found so we skip processing it
return
}
fullDstPhotoDir := filepath.Dir(filepath.Join(timelinerRepo, "processed", photoPath))
fullProcessedPhotoPath := filepath.Join(fullDstPhotoDir, photoID+".webp")
if _, err = os.Stat(fullProcessedPhotoPath); err == nil {
// processed image exists so we skip processing it
mu.Lock()
photos[albumName] = append(photos[albumName], fullProcessedPhotoPath)
mu.Unlock()
return
}
img, err := imaging.Open(fullPhotoPath)
if err != nil {
if !errors.Is(err, image.ErrFormat) {
errs = multierr.Append(errs, fmt.Errorf("open image '%s': %w", photoPath, err))
}
return
}
if err = os.MkdirAll(fullDstPhotoDir, os.ModePerm); err != nil {
errs = multierr.Append(errs, fmt.Errorf("create directory structure '%s': %w", photoPath, err))
return
}
if img.Bounds().Dx() >= img.Bounds().Dy() {
img = imaging.Resize(img, 1920, 0, imaging.Lanczos)
} else {
img = imaging.Resize(img, 0, 1080, imaging.Lanczos)
}
f, err := os.Create(fullProcessedPhotoPath)
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("create webp image '%s': %w", photoPath, err))
return
}
if err = webpbin.Encode(f, img); err != nil {
f.Close()
errs = multierr.Append(errs, fmt.Errorf("save webp image '%s': %w", photoPath, err))
return
}
if err = f.Close(); err != nil {
errs = multierr.Append(errs, fmt.Errorf("close webp image '%s': %w", photoPath, err))
return
}
mu.Lock()
photos[albumName] = append(photos[albumName], fullProcessedPhotoPath)
mu.Unlock()
return
})
}
if err = rows.Err(); err != nil {
log.Fatalf("Loop over rows: %v", err)
}
wg.Wait()
if errs != nil {
log.Printf("Multiple errors: %v\n", errs)
}
log.Println("Marshaling photos...")
jsonStr, err := json.Marshal(photos)
if err != nil {
log.Fatalf("Marshal photos: %v", err)
}
log.Println("Writing to file...")
if err = ioutil.WriteFile("./index.json", jsonStr, 0644); err != nil {
log.Fatalf("Write json file: %v", err)
}
}