-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.go
42 lines (38 loc) · 1002 Bytes
/
queries.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
package main
import (
"github.com/jinzhu/gorm"
"os"
"path/filepath"
)
func LoadPhotos(db *gorm.DB, start int, length int) []Photo {
var photos []Photo
db.Offset(start).Limit(length).Find(&photos)
return photos
}
func FindOrCreatePhoto(db *gorm.DB, path string) Photo {
var media Photo
db.Where(Photo{Path: path}).FirstOrInit(&media)
if db.NewRecord(media) {
media.RefreshAttributes()
db.Save(&media)
}
return media
}
func RebuildIndex(db *gorm.DB, sourceFolder string, destinationFolder string, moveFiles bool, generateThumbnails bool) {
walkFunction := func(path string, info os.FileInfo, err error) error {
CheckError(err, "Unable to transverse source")
if !info.IsDir() {
if IsValidPhoto(path) {
path, err := filepath.Abs(path)
CheckError(err, "Invalid file path")
pic := FindOrCreatePhoto(db, path)
if moveFiles {
pic.MoveToDestination(destinationFolder)
db.Save(&pic)
}
}
}
return nil
}
filepath.Walk(sourceFolder, walkFunction)
}