-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
303 lines (247 loc) · 6.5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
"github.com/davidbyttow/govips/v2/vips"
_ "github.com/mattn/go-sqlite3"
"io"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
)
func main() {
//vips.Startup(nil)
//defer vips.Shutdown()
heic := flag.String("heic-in", "", "A HEIC file to test conversion to JPEG")
libraryPath := flag.String("library", "", "Path to the Photos Library")
outputPath := flag.String("output", "", "Path where the synced libraries should be written")
flag.Parse()
if *heic != "" {
exportConvertedHEIC(*heic, "./output")
} else {
if *libraryPath == "" || *outputPath == "" {
fmt.Printf("Forgot to specify a library or output path\n")
os.Exit(1)
}
err := sync(*libraryPath, *outputPath)
if err != nil {
fmt.Printf("Exiting with error. [err:%s]\n", err.Error())
os.Exit(1)
}
}
}
func sync(libraryPath, outputPath string) error {
db, err := sql.Open("sqlite3", path.Join(libraryPath, "database", "Photos.sqlite?mode=ro"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
entityValue, err := findAlbumEntityValue(db)
if err != nil {
return err
}
fmt.Printf("Found album entity value. [value:%d]\n", entityValue)
albums, err := findAlbums(db, entityValue)
if err != nil {
return err
}
for _, album := range albums {
err = syncAlbum(db, album, libraryPath, outputPath)
if err != nil {
return err
}
}
return nil
}
func syncAlbum(db *sql.DB, album *AlbumMetadata, libraryBase, outputBase string) error {
fmt.Printf("Syncing %s with %d assets\n", album.Name, len(album.AssetIDs))
// Make sure the directory exists
dir := path.Join(outputBase, album.Name)
_, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
err = os.Mkdir(dir, 0755)
if err != nil {
return err
}
}
// Loop over all assets
for _, id := range album.AssetIDs {
// if it already exists, don't overwrite
// convert to jpeg from heic if necessary
srcDir, srcFilename, err := findAssetLocation(db, id)
if err != nil {
return err
}
// If srcFilename has a HEIC extension, replace it, the image will be converted later
dstFilename := srcFilename
if strings.HasSuffix(srcFilename, ".heic") {
dstFilename = strings.ReplaceAll(dstFilename, ".heic", ".jpeg")
}
outputPath := path.Join(outputBase, album.Name, dstFilename)
info, err := os.Stat(outputPath)
if info != nil {
fmt.Printf("Skipping file since it already exists. [filename:%s]\n", srcFilename)
continue
}
outF, err := os.Create(outputPath)
if err != nil {
return err
}
defer outF.Close()
var src io.Reader
inputPath := path.Join(libraryBase, "originals", srcDir, srcFilename)
inF, err := os.Open(inputPath)
if err != nil {
return err
}
defer inF.Close()
if strings.HasSuffix(srcFilename, "heic") {
jpegData, _, err := convertHEIC(inF)
if err != nil {
return err
}
src = bytes.NewBuffer(jpegData)
} else {
// Not HEIC so just directly copy
src = inF
}
_, err = io.Copy(outF, src)
if err != nil {
return err
}
err = outF.Sync()
if err != nil {
return err
}
}
return nil
}
func findAssetLocation(db *sql.DB, id int) (string, string, error) {
row := db.QueryRow(fmt.Sprintf(`SELECT ZDIRECTORY, ZFILENAME FROM ZASSET WHERE Z_PK = %d`, id))
var dir, filename string
err := row.Scan(&dir, &filename)
if err != nil {
return "", "", err
}
return dir, filename, nil
}
type AlbumMetadata struct {
PK int
Name string
AssetIDs []int
}
func findAlbums(db *sql.DB, entityID int) ([]*AlbumMetadata, error) {
rows, err := db.Query(fmt.Sprintf(`SELECT Z_PK, ZTITLE FROM ZGENERICALBUM WHERE Z_ENT = %d`, entityID))
if err != nil {
return nil, err
}
defer rows.Close()
albums := make([]*AlbumMetadata, 0, 10)
for rows.Next() {
var pk int
var name sql.NullString
err = rows.Scan(&pk, &name)
if err != nil {
return nil, err
} else if !name.Valid || name.String == "" || strings.HasPrefix(name.String, "progress-") {
// Sorry to all those out there that have album names like this; but it seems ilke
// Photos.app holds some special albums that starts with 'progress-'
continue
}
assetIDs, err := findAssetIDsForAlbum(db, entityID, pk)
if err != nil {
return nil, err
}
albums = append(albums, &AlbumMetadata{PK: pk, Name: name.String, AssetIDs: assetIDs})
}
return albums, nil
}
func findAssetIDsForAlbum(db *sql.DB, entityID, pk int) ([]int, error) {
// Asset ID column name is something like 'Z_NNASSETS'
columnNamePattern := regexp.MustCompile(`Z_\d+ASSETS`)
columnNameRows, err := db.Query(fmt.Sprintf(`SELECT name FROM pragma_table_info('Z_%dASSETS')`, entityID))
if err != nil {
return nil, err
}
var columnName string
for columnNameRows.Next() {
var name string
err = columnNameRows.Scan(&name)
if err != nil {
return nil, err
}
if columnNamePattern.MatchString(name) {
columnName = name
break
}
}
if columnName == "" {
return nil, fmt.Errorf("could not find a column name containing the asset ID values")
}
rows, err := db.Query(fmt.Sprintf(`SELECT %s FROM Z_%dASSETS WHERE Z_%dALBUMS = %d`, columnName, entityID, entityID, pk))
if err != nil {
return nil, err
}
defer rows.Close()
results := make([]int, 0, 100)
for rows.Next() {
var id int
err = rows.Scan(&id)
if err != nil {
return nil, err
}
results = append(results, id)
}
return results, nil
}
func findAlbumEntityValue(db *sql.DB) (int, error) {
rows, err := db.Query("SELECT tbl_name FROM sqlite_master WHERE type = 'table'")
if err != nil {
return -1, err
}
defer rows.Close()
pat := regexp.MustCompile(`Z_(\d+)ASSETS`)
for rows.Next() {
var tableName string
err = rows.Scan(&tableName)
if err != nil {
return -1, err
}
matches := pat.FindStringSubmatch(tableName)
if len(matches) < 2 {
continue
}
return strconv.Atoi(matches[1])
}
return -1, nil
}
func exportConvertedHEIC(inPath string, outDir string) error {
inF, err := os.Open(inPath)
if err != nil {
return err
}
data, _, err := convertHEIC(inF)
filename := filepath.Base(inPath)
ext := filepath.Ext(filename)
newName := strings.Replace(filename, ext, ".jpg", 1)
err = os.WriteFile(path.Join(outDir, newName), data, 0644)
return err
}
func convertHEIC(input io.Reader) ([]byte, *vips.ImageMetadata, error) {
img, err := vips.NewImageFromReader(input)
if err != nil {
return nil, nil, err
}
fmt.Printf("Identified file type: %+v.\n", vips.ImageTypes[img.Format()])
params := vips.NewJpegExportParams()
return img.ExportJpeg(params)
}