summaryrefslogtreecommitdiff
path: root/main.go
blob: b3a30198704f0ef990247016d359d50a6618f8e6 (plain)
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
package main

import (
	"database/sql"
	"log"
	"net"
	"net/http"
	"os"
	"path/filepath"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

// TODO: Perhaps maintain the DB, and the gallery directory, as globals.

func openDB(gd string) (*sql.DB, error) {
	return sql.Open("sqlite3", filepath.Join(gd, "gallery.db"))
}

// init GD - initialize a "gallery directory" that contains gallery.sqlite,
// images, thumbs.
func cmdInit(args []string) {
	if len(args) != 1 {
		log.Fatalln("usage: GD")
	}

	gd := args[0]
	db, err := openDB(gd)
	if err != nil {
		log.Fatalln(err)
	}

	if _, err = db.Query(initializeSQL); err != nil {
		log.Fatalln(err)
	}
	if err := os.MkdirAll(filepath.Join(gd, "images"), 0777); err != nil {
		log.Fatalln(err)
	}
	if err := os.MkdirAll(filepath.Join(gd, "thumbs"), 0777); err != nil {
		log.Fatalln(err)
	}
}

// run GD ADDRESS - run a web UI against GD on ADDRESS
func cmdRun(args []string) {
	if len(args) != 2 {
		log.Fatalln("usage: GD ADDRESS")
	}

	gd := args[0]
	db, err := openDB(gd)
	if err != nil {
		log.Fatalln(err)
	}

	_ = db
	address := args[1]

	http.Handle("/", http.FileServer(http.Dir("public")))
	// TODO: These subdirectories should be indirect
	// (skip the hash subpath, don't require the .webp suffix).
	http.Handle("/images",
		http.FileServer(http.Dir(filepath.Join(gd, "images"))))
	http.Handle("/thumbs",
		http.FileServer(http.Dir(filepath.Join(gd, "thumbs"))))

	host, port, err := net.SplitHostPort(address)
	if err != nil {
		log.Println(err)
	} else if host == "" {
		log.Println("http://" + net.JoinHostPort("localhost", port))
	} else {
		log.Println("http://" + address)
	}

	s := &http.Server{
		Addr:           address,
		ReadTimeout:    60 * time.Second,
		WriteTimeout:   60 * time.Second,
		MaxHeaderBytes: 32 << 10,
	}
	log.Fatalln(s.ListenAndServe())
}

// import GD ROOT... - add files to the "entry" table
func cmdImport(args []string) {
	// TODO
}

// sync GD ROOT... - like import, but clear table beforehands
func cmdSync(args []string) {
	// TODO
}

// check GD - see if all files are accessible
func cmdCheck(args []string) {
	// TODO
}

// thumbnail GD [SHA1...] - generate missing thumbnails, in parallel
func cmdThumbnail(args []string) {
	// TODO: Show progress.
}

// dhash GD HASHER [SHA1...] - generate perceptual hash from thumbnails
func cmdDhash(args []string) {
	// TODO
}

var commands = map[string]struct {
	handler func(args []string)
}{
	"init":      {cmdInit},
	"run":       {cmdRun},
	"import":    {cmdImport},
	"sync":      {cmdSync},
	"check":     {cmdCheck},
	"thumbnail": {cmdThumbnail},
	"dhash":     {cmdDhash},
}

func main() {
	if len(os.Args) <= 2 {
		log.Fatalln("Missing arguments")
	}

	cmd, ok := commands[os.Args[1]]
	if !ok {
		log.Fatalln("Unknown command: " + os.Args[1])
	}

	cmd.handler(os.Args[2:])
}