summaryrefslogtreecommitdiffstats
path: root/preview.go
blob: 6b0dd84b495340181a31ea2a714dde4d9ee6fe03 (plain) (blame)
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
package main

import (
	"fmt"
	"strconv"
	"strings"
	"html/template"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"log"
	"runtime"
	"sync"
)

type Image struct {
	Name string
	Filename string
	Liked bool
}

var images []Image
var currIndex int = 0
var imagesPerPage int = 4

func openbrowser(url string) {
	var err error

	switch runtime.GOOS {
	case "linux":
		err = exec.Command("xdg-open", url).Start()
	case "windows":
		err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
	case "darwin":
		err = exec.Command("open", url).Start()
	default:
		err = fmt.Errorf("unsupported platform")
	}
	if err != nil {
		log.Fatal(err)
	}
}


var funcMap = template.FuncMap{
    "minus": minus,
}

func minus(a, b int) string {
    return strconv.FormatInt(int64(a-b), 10)
}

func main() {
	jpgs_path := "jpgs"

	http.HandleFunc("/", indexHandler)
	http.HandleFunc("/select", likeHandler)
	http.HandleFunc("/remove", removeHandler)
	http.HandleFunc("/removefav", removeFavsHandler)
	http.HandleFunc("/next", nextHandler)
	http.HandleFunc("/prev", prevHandler)
	http.HandleFunc("/favs", favsHandler)
	http.HandleFunc("/jpgs/", serveImage)
	http.Handle("/static/", http.FileServer(http.Dir(".")))


	filepath.Walk(jpgs_path, func(path string, info os.FileInfo, err error) error {
		if filepath.Ext(path) == ".jpg" || filepath.Ext(path) == ".JPG" {
			base := filepath.Base(path)
			name := base[:len(base)-len(filepath.Ext(base))]
			images = append(images, Image{name, filepath.ToSlash(path), false})
		}
		return nil
	})

	var wg sync.WaitGroup
	port := "42069"
	fmt.Println("Serving on http://localhost:" + port)
	wg.Add(1)
	go http.ListenAndServe(":" + port, nil)
	openbrowser("http://localhost:" + port)
	wg.Wait()
}

func serveImage(w http.ResponseWriter, r *http.Request) {
	filename := r.URL.Path[1:]
	http.ServeFile(w, r, filename)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	tmpl, _ := template.ParseFiles("grid.html")
	idx := r.URL.Query().Get("idx")
	if len(idx) != 0 {
		tmp, _ := strconv.Atoi(idx)
		if tmp - 1 > len(images) {
			currIndex = len(images) - 1
		} else if tmp >= 1 {
			currIndex = tmp - 1
		} else {
			currIndex = 0
		}
	}
	name := r.URL.Query().Get("name")
	if len(name) != 0 {
		for idx, i := range images {
			if (strings.Contains(i.Name, name)) {
				currIndex = idx;
				reload(w, r)
				return
			}
		}
	}
	imagesToShow := images[currIndex:min(currIndex+imagesPerPage, len(images))]
	err := tmpl.Execute(w, imagesToShow)
	if err != nil { log.Fatal(err) }
}

func reload(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "/?idx="+strconv.Itoa(currIndex + 1), http.StatusFound)
}
 
func likeHandler(w http.ResponseWriter, r *http.Request) {
	visibleImages := images[currIndex:min(currIndex+imagesPerPage, len(images))]
	index := r.URL.Query().Get("img_index")
	i, _ := strconv.Atoi(index)
	visibleImages[i].Liked = true
	reload(w, r)
}

func removeHandler(w http.ResponseWriter, r *http.Request) {
	visibleImages := images[currIndex:min(currIndex+imagesPerPage, len(images))]
	index := r.URL.Query().Get("img_index")
	i, _ := strconv.Atoi(index)
	visibleImages[i].Liked = false
	reload(w, r)
}

func nextHandler(w http.ResponseWriter, r *http.Request) {
	currIndex += imagesPerPage
	if currIndex >= len(images) {
		currIndex = len(images) - imagesPerPage
	}
	reload(w, r)
}

func prevHandler(w http.ResponseWriter, r *http.Request) {
	currIndex -= imagesPerPage
	if currIndex < 0 {
		currIndex = 0
	}
	reload(w, r)
}

func removeFavsHandler(w http.ResponseWriter, r *http.Request) {
	filename := r.URL.Query().Get("filename")
	for i := range images {
		if images[i].Filename == filename {
			images[i].Liked = false
		}
	}
	http.Redirect(w, r, "/favs", http.StatusFound)
}

func favsHandler(w http.ResponseWriter, r *http.Request) {
	tmpl, _ := template.New("favs.html").Funcs(funcMap).ParseFiles("favs.html")

	var liked []Image

	for _, i := range images {
		if i.Liked {
			liked = append(liked, i)
		}
	}
	
	err := 	tmpl.Execute(w, liked)
	if err != nil { log.Fatal(err) }
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}