-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimgdb.go
More file actions
373 lines (338 loc) · 8.64 KB
/
imgdb.go
File metadata and controls
373 lines (338 loc) · 8.64 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Imgdb.go
/
/ Copyright (c) 2014, Gianluca Fiore
/
/ This program is free software: you can redistribute it and/or modify
/ it under the terms of the GNU General Public License as published by
/ the Free Software Foundation, either version 3 of the License, or
/ (at your option) any later version.
/
/ Requirements: Go, go-sqlite3
/
/ What's for:
/ Nothing, actually. I just wanted to play with Go and SQLite :-)
/ Feel free to improve/hack/fork/pull request this.
/
*/
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"regexp"
"time"
_ "github.com/mattn/go-sqlite3"
)
var createArg bool // Create the db flag
var updateArg bool // Just update the db flag
var usedArg bool // Used state for a db row flag
var insensitiveArg bool // Case insensitive search flag
var randomArg bool // Random image flag
var searchArg string // Search string flag
var usageMessage = `
imgdb.go [options] [imgpath]
Description:
imgdb.go will create anew or update a pre-existing database of file paths in
a specific filesystem location. Every file will have a "used" column in
the database to tell whether it has already been used or not.
Implemented also are searching the database and printing a random image,
among the ones that haven't yet been used.
Arguments:
-help|-h
This help
-create|-c
Create the database
-update|-u
Just update the database with new files and removing old ones
-used|-d
Change the used state of a row in the database
-random|-r
Print a random image's path
-search|-s <string>
Search for a string, usually part of the path or the image name,
in the database
-insensitive|-i
Search in case insensitive mode. It requires also -s (obviously)
`
// Init flags
func flagsInit() {
const (
flagCreate = false
flagUpdate = false
flagUsed = false
flagInsensitive = false
flagRandom = false
flagSearch = ""
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usageMessage)
}
flag.BoolVar(&createArg, "create", flagCreate, "")
flag.BoolVar(&createArg, "c", flagCreate, "")
flag.BoolVar(&updateArg, "update", flagUpdate, "")
flag.BoolVar(&updateArg, "u", flagUpdate, "")
flag.BoolVar(&usedArg, "used", flagUsed, "")
flag.BoolVar(&usedArg, "d", flagUsed, "")
flag.BoolVar(&insensitiveArg, "insensitive", flagInsensitive, "")
flag.BoolVar(&insensitiveArg, "i", flagInsensitive, "")
flag.BoolVar(&randomArg, "random", flagRandom, "")
flag.BoolVar(&randomArg, "r", flagRandom, "")
flag.StringVar(&searchArg, "search", flagSearch, "")
flag.StringVar(&searchArg, "s", flagSearch, "")
flag.Parse()
}
// Make sure that the db actually exists
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Connect to the db and return a pointer to it
func connectDb(dbpath string) *sql.DB {
e, err := pathExists(dbpath)
if e != true {
if err != nil {
log.Fatal(err)
}
}
// Connect
db, err := sql.Open("sqlite3", dbpath)
if err != nil {
log.Fatal(err)
}
return db
}
// Create the db
func createDb(db *sql.DB) *sql.DB {
// Create
sql := `
create table if not exists images (id integer not null primary key,
path text unique,
used integer default 0);
`
_, err := db.Exec(sql)
if err != nil {
log.Printf("%q: %s\n", err, sql)
db.Close()
log.Fatal(err)
}
return db
}
// Save file list from a path in a map
func saveFileList(path string) []string {
var pathmaps []string // map of paths to add to the db
// Check the path exists
if _, err := os.Stat(path); err != nil {
log.Fatal(err)
}
visit := func(path string, f os.FileInfo, err error) error {
if f.IsDir() != true {
pathmaps = append(pathmaps, path)
}
return nil
}
err := filepath.Walk(path, visit)
if err != nil {
fmt.Fprintf(os.Stderr, "filepath.Walk() returned %v\n", err)
}
return pathmaps
}
// Purge deleted files from the database and add new ones to it
func updateDb(db *sql.DB, pathmaps []string) {
currentRows := make(map[string]int) // current paths present in db
rows, err := db.Query("select path, used from images")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Build a map of current paths in the db
for rows.Next() {
var path string
var used int
rows.Scan(&path, &used)
currentRows[path] = used
}
for _, v := range pathmaps {
found := false
for k := range currentRows {
if v == k {
found = true
break
}
}
if !found {
fmt.Println("Found a new image ", v)
addRow(db, v, 0)
}
}
for k := range currentRows {
toDelete := true
for _, v := range pathmaps {
if k == v {
toDelete = false
break
}
}
if toDelete {
fmt.Println("Deleting ", k)
_, err = db.Exec("delete from images where path=?", k)
if err != nil {
log.Fatal(err)
}
}
}
}
// Query the db with the search string
func searchRow(db *sql.DB, query string) map[string]int {
var currentPath string // Scanned rows' path
var currentUsed int // Scanned rows' used status
matchingPaths := make(map[string]int) // Map of paths matching the query
// along with used status integers being the keys
r, err := regexp.Compile(query)
if err != nil {
fmt.Fprintf(os.Stderr, "There is a problem with the search string, sorry\n")
return matchingPaths
}
// We got to select ALL paths as the user most probably isn't going
// to search with a full path string but just with part of the
// filename. Therefore, it won't match any row.
rows, err := db.Query("select path, used from images")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
rows.Scan(¤tPath, ¤tUsed)
if r.MatchString(currentPath) == true {
matchingPaths[currentPath] = currentUsed
}
}
return matchingPaths
}
// Change the used column in the db for the given paths
func changeUsedState(db *sql.DB, path string) {
_, err := db.Exec(`update images set
used = case when used=0 then 1 else 0 end where path = ?`, path)
if err != nil {
log.Fatal(err)
}
fmt.Println("Updated used status for ", path)
}
// Print a random image's path
func printRandomPath(db *sql.DB) {
// Get current number of rows present
var totalRows int
err := db.QueryRow("select count(*) from images").Scan(&totalRows)
switch {
case err == sql.ErrNoRows:
log.Printf("Couldn't get total number of rows in database, perhaps it is empty?")
case err != nil:
log.Fatal(err)
default:
fmt.Println(rand.Intn(totalRows))
}
randomPath, err := db.Query("select path, used from images where id=?", rand.Intn(totalRows))
if err != nil {
log.Fatal(err)
}
defer randomPath.Close()
for randomPath.Next() {
var path string
var used int
randomPath.Scan(&path, &used)
if used == 1 {
// We have already used this image, we want a fresh one
printRandomPath(db)
} else {
fmt.Println(path)
}
}
}
// Add a row (id, path, used) to the db
func addRow(db *sql.DB, path string, used int) {
// Add
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
statement, err := tx.Prepare("insert into images(path, used) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer statement.Close()
_, err = statement.Exec(path, used)
if err != nil {
// The row is already present, skip it
return
}
tx.Commit()
// Select
rows, err := db.Query("select id, path, used from images")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int
var path string
var used int
rows.Scan(&id, &path, &used)
}
}
func main() {
dbpath := "/media/private/img/img.db" // path of the db
rand.Seed(time.Now().UTC().UnixNano()) // Initialize random seed
flagsInit()
imgpath := flag.Arg(0) // path of the images
if imgpath == "" {
// default to below if we didn't give it as an argument
imgpath = "/media/private/img/Models/"
}
pathmaps := saveFileList(imgpath)
if searchArg != "" {
db := connectDb(dbpath)
if insensitiveArg { // Perform a case insensitive search
searchArg = `(?i)` + searchArg
}
rslt := searchRow(db, searchArg)
if len(rslt) > 0 {
fmt.Println("We found these matching images\n")
for p, i := range rslt {
fmt.Println(p, i)
if usedArg { // Change used status then
changeUsedState(db, p)
}
}
} else {
fmt.Println("Sorry, nothing found")
}
db.Close()
} else if randomArg {
db := connectDb(dbpath)
printRandomPath(db)
db.Close()
} else if updateArg {
db := connectDb(dbpath)
fmt.Printf("There are currently a total of %d images in the database\n", len(pathmaps))
updateDb(db, pathmaps)
db.Close()
} else if createArg {
db := createDb(connectDb(dbpath))
for _, f := range pathmaps {
addRow(db, f, 0)
}
fmt.Println("Database created")
db.Close()
} else {
flag.Usage()
}
}