From 5c6a5b3ec2e820ec75c51ff025624520f714bb1e Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sat, 9 Dec 2023 01:32:32 +0100 Subject: Add a test --- Makefile | 2 +- initialize.sql | 12 +++++++----- main.go | 16 +++++++++------- test.sh | 11 +++++++++++ 4 files changed, 28 insertions(+), 13 deletions(-) create mode 100755 test.sh diff --git a/Makefile b/Makefile index caefa2f..97cc5ef 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ outputs = gallery initialize.go public/mithril.js all: $(outputs) gallery: main.go initialize.go - go build -o $@ + go build -gcflags="all=-N -l" -o $@ initialize.go: initialize.sql gen-initialize.sh ./gen-initialize.sh initialize.sql > $@ public/mithril.js: diff --git a/initialize.sql b/initialize.sql index 746b0e2..6d5a9ad 100644 --- a/initialize.sql +++ b/initialize.sql @@ -5,18 +5,19 @@ CREATE TABLE IF NOT EXISTS entry( mtime INTEGER NOT NULL, -- Unix time of last modification in seconds sha1 TEXT NOT NULL, -- SHA-1 hash of file in lowercase hexadecimal PRIMARY KEY (path, basename) -); +) STRICT; CREATE INDEX IF NOT EXISTS entry_sha1 ON entry(sha1, path, basename); -- XXX: Shouldn't perhaps "entry.sha1" reference "image.sha1"? +-- FIXME CREATE TABLE IF NOT EXISTS image( - sha1 TEXT NOT NULL REFERENCES entry(sha1), + sha1 TEXT NOT NULL REFERENCES entry(sha1, path, basename), thumbw INTEGER, -- cached thumbnail width, if known thumbh INTEGER, -- cached thumbnail height, if known dhash INTEGER, -- uint64 perceptual hash as a signed integer PRIMARY KEY (sha1) -); +) STRICT; CREATE INDEX IF NOT EXISTS image_dhash ON image(dhash, sha1); @@ -24,13 +25,14 @@ CREATE TABLE IF NOT EXISTS image_tag( sha1 TEXT NOT NULL REFERENCES image(sha1), tag TEXT NOT NULL, PRIMARY KEY (sha1) -); +) STRICT; +-- XXX: Perhaps this should be more like namespaces. CREATE TABLE IF NOT EXISTS image_autotag( sha1 TEXT NOT NULL REFERENCES image(sha1), tag TEXT NOT NULL, weight REAL NOT NULL, -- 0..1 normalized weight assigned to tag PRIMARY KEY (sha1, tag) -); +) STRICT; CREATE INDEX IF NOT EXISTS image_autotag_tag ON image_autotag(tag, sha1); diff --git a/main.go b/main.go index fc19dd6..09ff161 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ var ( func openDB(directory string) error { var err error - db, err = sql.Open("sqlite3", filepath.Join(directory, "gallery.db")) + db, err = sql.Open("sqlite3", + "file:"+filepath.Join(directory, "gallery.db?_foreign_keys=1")) gd = directory return err } @@ -77,10 +78,10 @@ func cmdInit(args []string) { // XXX: There's technically no reason to keep images as symlinks, // we might just keep absolute paths in the database as well. - if err := os.MkdirAll(filepath.Join(gd, "images"), 0777); err != nil { + if err := os.MkdirAll(filepath.Join(gd, "images"), 0755); err != nil { log.Fatalln(err) } - if err := os.MkdirAll(filepath.Join(gd, "thumbs"), 0777); err != nil { + if err := os.MkdirAll(filepath.Join(gd, "thumbs"), 0755); err != nil { log.Fatalln(err) } } @@ -180,10 +181,10 @@ func importFunc(path string, d fs.DirEntry, err error) error { return err } - hexSHA1 := hex.EncodeToString(hash.Sum(make([]byte, sha1.Size))) + hexSHA1 := hex.EncodeToString(hash.Sum(nil)) pathImage := imagePath(hexSHA1) imageDirname, _ := filepath.Split(pathImage) - if err := os.MkdirAll(imageDirname, 755); err != nil { + if err := os.MkdirAll(imageDirname, 0755); err != nil { return err } if err := os.Symlink(absPath, pathImage); err != nil && @@ -210,6 +211,7 @@ func cmdImport(args []string) { } // TODO: This would better be done in parallel (making hashes). + // TODO: Show progress in some manner. Perhaps port my propeller code. for _, name := range args[1:] { if err := filepath.WalkDir(name, importFunc); err != nil { log.Fatalln(err) @@ -244,7 +246,7 @@ func cmdCheck(args []string) { func makeThumbnail(pathImage, pathThumb string) (int, int, error) { thumbDirname, _ := filepath.Split(pathThumb) - if err := os.MkdirAll(thumbDirname, 755); err != nil { + if err := os.MkdirAll(thumbDirname, 0755); err != nil { return 0, 0, err } @@ -285,7 +287,7 @@ func cmdThumbnail(args []string) { if len(hexSHA1) == 0 { // Get all unique images in the database with no thumbnail. var err error - hexSHA1, err = dbCollect(`SELECT DISTINCT sha1 FROM entry + hexSHA1, err = dbCollect(`SELECT DISTINCT entry.sha1 FROM entry LEFT OUTER JOIN image ON entry.sha1 = image.sha1 WHERE thumbw IS NULL OR thumbh IS NULL`) if err != nil { diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..adacca5 --- /dev/null +++ b/test.sh @@ -0,0 +1,11 @@ +#!/bin/sh -xe +make gallery +target=/tmp/G input=/tmp/Gi +rm -rf $target $input + +mkdir -p $target +cp -r $HOME/Pictures/Anime $input +./gallery init $target +./gallery import $target $input +./gallery thumbnail $target +./gallery dhash $target $HOME/Projects/fiv/build/hash -- cgit v1.2.3-70-g09d2