-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanmusic.go
170 lines (159 loc) · 6 KB
/
scanmusic.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
package main
import (
"database/sql"
"io/ioutil"
"os"
"path"
"strings"
"time"
_ "github.com/mxk/go-sqlite/sqlite3"
)
var lastMusicScan int64
func getNextKey( transaction *sql.Tx ) int {
// Determine the next available shared key value
rowFolderKey := transaction.QueryRow( "SELECT max(folderKey) FROM folders;" )
var folderMax int
rowFolderKey.Scan( &folderMax )
rowSongKey := transaction.QueryRow( "SELECT max(songKey) FROM songs;" )
var songMax int
rowSongKey.Scan( &songMax )
nextKey := songMax + 1
if folderMax > songMax {
nextKey = folderMax + 1
}
return nextKey
}
func processSong( folderRoot string, songPath string, songParent int, quickScan bool, transaction *sql.Tx ) {
songFile := path.Join( folderRoot, songPath )
songFolder := path.Dir( songFile )
coverArtExists := true
coverArtPath := path.Join( songFolder, "cover.jpg" )
if _,error := os.Stat( coverArtPath ); error != nil {
coverArtPath = path.Join( songFolder, "cover.png" )
if _,error := os.Stat( coverArtPath ); error != nil {
coverArtExists = false
}
}
fileExtension := path.Ext( songFile )
if fileExtension == ".m4a" {
rowSong := transaction.QueryRow( "SELECT songKey FROM songs WHERE songPath=? AND songParent=?;", songPath, songParent )
var songKey int
rowSong.Scan( &songKey )
if songKey != 0 {
if quickScan {
_,error := transaction.Exec( "UPDATE songs SET songLastUpdate=? WHERE songKey=?;", lastMusicScan, songKey )
if error != nil {
println( "Error updating artist in database.", error )
}
} else {
fileTag := processFileM4A( songFile )
_,error := transaction.Exec( "UPDATE songs SET songLastUpdate=?,songTitle=?,songArtist=?,songAlbum=?,songDuration=?,songYear=?,songTrack=?,songDisc=? WHERE songKey=?;", lastMusicScan, fileTag.Title, fileTag.Artist, fileTag.Album, fileTag.Duration, fileTag.Year, fileTag.Track, fileTag.Disc, songKey )
if error != nil {
println( "Error updating artist in database.", error )
}
}
} else {
fileTag := processFileM4A( songFile )
println( " Adding song:",fileTag.Title )
songKey = getNextKey( transaction )
_,error := transaction.Exec( "INSERT INTO songs (songKey,songPath,songParent,songCreated,songLastUpdate,songTitle,songArtist,songAlbum,songDuration,songYear,songTrack,songDisc) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);", songKey, songPath, songParent, lastMusicScan, lastMusicScan, fileTag.Title, fileTag.Artist, fileTag.Album, fileTag.Duration, fileTag.Year, fileTag.Track, fileTag.Disc )
if error != nil {
println( "Error inserting song into database.", error )
}
}
if !coverArtExists {
coverArtData,error := extractCoverArtM4A( songFile )
if error == nil {
var fileName string = "cover.jpg"
if coverArtData.isPng {
fileName = "cover.png"
}
outputFile,error := os.Create( path.Join( songFolder, fileName ) )
if error != nil {
println( "Error creating cover art image.", error.Error() )
} else {
println( "Writing cover art image in",songFolder )
outputFile.Write( coverArtData.data )
outputFile.Close()
}
}
}
}
}
func processFolder( folderRoot string, folderPath string, folderParent int, quickScan bool, transaction *sql.Tx ) {
dirList,error := ioutil.ReadDir( path.Join( folderRoot, folderPath ) )
if error != nil {
println( "Error reading folder.", folderPath, error )
return
}
for _,item := range dirList {
if strings.HasPrefix( item.Name(), "." ) {
continue
}
itemPath := path.Join( folderPath, item.Name() )
if item.IsDir() {
rowFolder := transaction.QueryRow( "SELECT folderKey FROM folders WHERE folderName=? AND folderParent=?;", item.Name(), folderParent )
var folderKey int
rowFolder.Scan( &folderKey )
if folderKey != 0 {
_,error := transaction.Exec( "UPDATE folders SET folderLastUpdate=? WHERE folderKey=?;", lastMusicScan, folderKey )
if error != nil {
println( "Error updating artist in database.", error )
}
} else {
if folderParent == 0 {
println( "Adding artist: " + item.Name() )
} else {
println( " Adding folder: " + item.Name() )
}
folderKey = getNextKey( transaction )
result,error := transaction.Exec( "INSERT INTO folders (folderKey,folderName,folderPath,folderParent,folderCreated,folderLastUpdate,folderRoot) VALUES (?,?,?,?,?,?,?);", folderKey, item.Name(), itemPath, folderParent, lastMusicScan, lastMusicScan, folderRoot )
if error != nil {
println( "Error inserting artist into database.", error )
}
id,error := result.LastInsertId()
folderKey = int(id)
}
processFolder( folderRoot, itemPath, folderKey, quickScan, transaction )
} else {
processSong( folderRoot, itemPath, folderParent, quickScan, transaction )
}
}
}
func scanMusicFolders( quickScan bool ) {
db,error := sql.Open( "sqlite3", settings.Database )
if error != nil {
println( "Error opening database", error )
return
}
defer db.Close()
artistTransaction,error := db.Begin()
if error != nil {
println( "Error starting database transaction.", error )
return
}
lastMusicScan = time.Now().Unix()
for _,folder := range settings.Folders {
processFolder( folder, "", 0, quickScan, artistTransaction )
}
// Delete any artist records that were not updated (and therefore no longer exist on disk)
artistResult,error := artistTransaction.Exec( "DELETE FROM folders WHERE folderLastUpdate<?;", lastMusicScan )
if error != nil {
println( "Error cleaning old artists from database.", error.Error() )
}
artistCount,error := artistResult.RowsAffected()
if error != nil {
println( "Error counting old artists from database.", error.Error() )
}
songResult,error := artistTransaction.Exec( "DELETE FROM songs WHERE songLastUpdate<?;", lastMusicScan )
if error != nil {
println( "Error cleaning old songs from database.", error.Error() )
}
songCount,error := songResult.RowsAffected()
if error != nil {
println( "Error counting old artists from database.", error.Error() )
}
println( "Deleted", artistCount, "missing artists,", songCount, "missing songs." )
artistTransaction.Commit()
db.Close()
}