Compare commits
6 Commits
e895beadb7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
181ab5a8e7
|
|||
|
fd192310c7
|
|||
|
b73e0b4622
|
|||
|
0530c5d95f
|
|||
|
ce2e58b6bc
|
|||
|
ca462ac005
|
@@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS node(
|
|||||||
) STRICT;
|
) STRICT;
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS node__sha1 ON node(sha1);
|
CREATE INDEX IF NOT EXISTS node__sha1 ON node(sha1);
|
||||||
|
CREATE INDEX IF NOT EXISTS node__parent ON node(parent);
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS node__parent_name
|
CREATE UNIQUE INDEX IF NOT EXISTS node__parent_name
|
||||||
ON node(IFNULL(parent, 0), name);
|
ON node(IFNULL(parent, 0), name);
|
||||||
|
|
||||||
|
|||||||
122
main.go
122
main.go
@@ -94,10 +94,15 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func openDB(directory string) error {
|
func openDB(directory string) error {
|
||||||
|
galleryDirectory = directory
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
db, err = sql.Open("sqlite3_custom", "file:"+filepath.Join(directory,
|
db, err = sql.Open("sqlite3_custom", "file:"+filepath.Join(directory,
|
||||||
nameOfDB+"?_foreign_keys=1&_busy_timeout=1000"))
|
nameOfDB+"?_foreign_keys=1&_busy_timeout=1000"))
|
||||||
galleryDirectory = directory
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = db.Exec(initializeSQL)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,10 +308,6 @@ func cmdInit(fs *flag.FlagSet, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := db.Exec(initializeSQL); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX: There's technically no reason to keep images as symlinks,
|
// XXX: There's technically no reason to keep images as symlinks,
|
||||||
// we might just keep absolute paths in the database as well.
|
// we might just keep absolute paths in the database as well.
|
||||||
if err := os.MkdirAll(
|
if err := os.MkdirAll(
|
||||||
@@ -657,7 +658,9 @@ func getOrphanReplacement(webPath string) (*webOrphanImage, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent, err := idForDirectoryPath(tx, path[:len(path)-1], false)
|
parent, err := idForDirectoryPath(tx, path[:len(path)-1], false)
|
||||||
if err != nil {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil
|
||||||
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -684,7 +687,8 @@ func getOrphans() (result []webOrphan, err error) {
|
|||||||
FROM orphan AS o
|
FROM orphan AS o
|
||||||
JOIN image AS i ON o.sha1 = i.sha1
|
JOIN image AS i ON o.sha1 = i.sha1
|
||||||
LEFT JOIN tag_assignment AS ta ON o.sha1 = ta.sha1
|
LEFT JOIN tag_assignment AS ta ON o.sha1 = ta.sha1
|
||||||
GROUP BY o.sha1`)
|
GROUP BY o.sha1
|
||||||
|
ORDER BY path`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -841,15 +845,17 @@ type webSimilarImage struct {
|
|||||||
|
|
||||||
func getSimilar(sha1 string, dhash int64, pixels int64, distance int) (
|
func getSimilar(sha1 string, dhash int64, pixels int64, distance int) (
|
||||||
result []webSimilarImage, err error) {
|
result []webSimilarImage, err error) {
|
||||||
// For distance ∈ {0, 1}, this query is quite inefficient.
|
// If there's a dhash, there should also be thumbnail dimensions.
|
||||||
// In exchange, it's generic.
|
var rows *sql.Rows
|
||||||
//
|
common := `SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0)
|
||||||
// If there's a dhash, there should also be thumbnail dimensions,
|
FROM image WHERE sha1 <> ? AND `
|
||||||
// so not bothering with IFNULL on them.
|
if distance == 0 {
|
||||||
rows, err := db.Query(`
|
rows, err = db.Query(common+`dhash = ?`, sha1, dhash)
|
||||||
SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0)
|
} else {
|
||||||
FROM image WHERE sha1 <> ? AND dhash IS NOT NULL
|
// This is generic, but quite inefficient for distance ∈ {0, 1}.
|
||||||
|
rows, err = db.Query(common+`dhash IS NOT NULL
|
||||||
AND hamming(dhash, ?) = ?`, sha1, dhash, distance)
|
AND hamming(dhash, ?) = ?`, sha1, dhash, distance)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -2026,6 +2032,88 @@ func cmdRemove(fs *flag.FlagSet, args []string) error {
|
|||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Forgetting --------------------------------------------------------------
|
||||||
|
|
||||||
|
// cmdForget is for purging orphaned images from the database.
|
||||||
|
func cmdForget(fs *flag.FlagSet, args []string) error {
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fs.NArg() < 2 {
|
||||||
|
return errWrongUsage
|
||||||
|
}
|
||||||
|
if err := openDB(fs.Arg(0)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
// Creating a temporary database seems justifiable in this case.
|
||||||
|
_, err = tx.Exec(
|
||||||
|
`CREATE TEMPORARY TABLE forgotten (sha1 TEXT PRIMARY KEY)`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stmt, err := tx.Prepare(`INSERT INTO forgotten (sha1) VALUES (?)`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
for _, sha1 := range fs.Args()[1:] {
|
||||||
|
if _, err := stmt.Exec(sha1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := tx.Query(`DELETE FROM forgotten
|
||||||
|
WHERE sha1 IN (SELECT sha1 FROM node)
|
||||||
|
OR sha1 NOT IN (SELECT sha1 FROM image)
|
||||||
|
RETURNING sha1`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var sha1 string
|
||||||
|
if err := rows.Scan(&sha1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Printf("not an orphan or not known at all: %s", sha1)
|
||||||
|
}
|
||||||
|
if _, err = tx.Exec(`
|
||||||
|
DELETE FROM tag_assignment WHERE sha1 IN (SELECT sha1 FROM forgotten);
|
||||||
|
DELETE FROM orphan WHERE sha1 IN (SELECT sha1 FROM forgotten);
|
||||||
|
DELETE FROM image WHERE sha1 IN (SELECT sha1 FROM forgotten);
|
||||||
|
`); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err = tx.Query(`SELECT sha1 FROM forgotten`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var sha1 string
|
||||||
|
if err := rows.Scan(&sha1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Remove(imagePath(sha1)); err != nil &&
|
||||||
|
!os.IsNotExist(err) {
|
||||||
|
log.Printf("%s", err)
|
||||||
|
}
|
||||||
|
if err := os.Remove(thumbPath(sha1)); err != nil &&
|
||||||
|
!os.IsNotExist(err) {
|
||||||
|
log.Printf("%s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
// --- Tagging -----------------------------------------------------------------
|
// --- Tagging -----------------------------------------------------------------
|
||||||
|
|
||||||
// cmdTag mass imports tags from data passed on stdin as a TSV
|
// cmdTag mass imports tags from data passed on stdin as a TSV
|
||||||
@@ -2637,6 +2725,7 @@ var commands = map[string]struct {
|
|||||||
"tag": {cmdTag, "GD SPACE [DESCRIPTION]", "Import tags."},
|
"tag": {cmdTag, "GD SPACE [DESCRIPTION]", "Import tags."},
|
||||||
"sync": {cmdSync, "GD ROOT...", "Synchronise with the filesystem."},
|
"sync": {cmdSync, "GD ROOT...", "Synchronise with the filesystem."},
|
||||||
"remove": {cmdRemove, "GD PATH...", "Remove database subtrees."},
|
"remove": {cmdRemove, "GD PATH...", "Remove database subtrees."},
|
||||||
|
"forget": {cmdForget, "GD SHA1...", "Dispose of orphans."},
|
||||||
"check": {cmdCheck, "GD", "Run consistency checks."},
|
"check": {cmdCheck, "GD", "Run consistency checks."},
|
||||||
"thumbnail": {cmdThumbnail, "GD [SHA1...]", "Generate thumbnails."},
|
"thumbnail": {cmdThumbnail, "GD [SHA1...]", "Generate thumbnails."},
|
||||||
"dhash": {cmdDhash, "GD [SHA1...]", "Compute perceptual hashes."},
|
"dhash": {cmdDhash, "GD [SHA1...]", "Compute perceptual hashes."},
|
||||||
@@ -2700,6 +2789,9 @@ func main() {
|
|||||||
// Note that the database object has a closing finalizer,
|
// Note that the database object has a closing finalizer,
|
||||||
// we just additionally print any errors coming from there.
|
// we just additionally print any errors coming from there.
|
||||||
if db != nil {
|
if db != nil {
|
||||||
|
if _, err := db.Exec(`PRAGMA optimize`); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
if err := db.Close(); err != nil {
|
if err := db.Close(); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user