-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
140 lines (121 loc) · 3.47 KB
/
main.go
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
package main
import (
_ "embed"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
h "html"
flags "github.com/jessevdk/go-flags"
"github.com/spf13/afero"
minify "github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
)
//go:embed template.html
var template string
const galleryFileName = "gal.html"
type Options struct {
LaunchDefaultBrowser bool `short:"b" long:"browser" description:"Launch the default browser after creating the gallery"`
LaunchFirefox bool `short:"f" long:"firefox" description:"Launch Firefox after creating the gallery"`
}
func createHTMLGallery(template, directoryAbsolutePath string, images []fs.FileInfo) string {
// Create a new file in the directory
galleryFileName := filepath.Join(directoryAbsolutePath, galleryFileName)
file, err := os.Create(galleryFileName)
if err != nil {
fmt.Println(err)
return ""
}
defer file.Close()
// Generate the content for the gallery
galleryTitle := filepath.Base(directoryAbsolutePath)
galleryInfo := fmt.Sprintf("Created on %s - %d images", time.Now().Format("January 2, 2006 at 3:04 PM"), len(images))
galleryContents := ""
for _, image := range images {
if fileIsVideo(image) {
galleryContents += fmt.Sprintf(`
<figure class='card'>
<video src='%s' autoplay loop muted/>
</figure>`, h.EscapeString(image.Name()))
continue
}
galleryContents += fmt.Sprintf(`
<figure class='card'>
<img src='%s' />
</figure>`, h.EscapeString(image.Name()))
}
// Replace placeholders in the template
template = strings.Replace(template, "<!-- GALLERY_CONTENTS -->", galleryContents, 1)
template = strings.Replace(template, "<!-- GALLERY_TITLE -->", galleryTitle, 1)
template = strings.Replace(template, "<!-- GALLERY_INFO -->", galleryInfo, 1)
// Minify the HTML
m := minify.New()
m.AddFunc("text/html", html.Minify)
m.AddFunc("text/css", css.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
template, err = m.String("text/html", template)
if err != nil {
panic(err)
}
// Write the HTML to the file
file.WriteString(template)
log.Printf("Gallery created: %s", galleryFileName)
return galleryFileName
}
func main() {
// Parse command line arguments
var opts Options
_, err := flags.Parse(&opts)
if err != nil {
// Handle error
fmt.Println(err)
return
}
// Get the target directory
args := os.Args[1:]
if len(args) == 0 {
log.Fatalf("Usage: %s <directory>", os.Args[0])
return
}
dir := args[0]
log.Printf("Directory to be scanned: %s", dir)
// Ensure the directory exists
dirInfo, err := os.Stat(dir)
if err != nil {
log.Fatalf("Error reading directory: %s", err)
return
}
if !dirInfo.IsDir() {
log.Fatalf("'%s' is not a directory", dir)
return
}
dir, err = filepath.Abs(dir)
if err != nil {
log.Fatalf("Error resolving absolute path: %s", err)
return
}
// Gather browser-supported images in the target directory
images := getImagesInDirectory(afero.NewOsFs(), dir)
// Create a new HTML file
galleryFileName := createHTMLGallery(template, dir, images)
// Optionally, open the gallery in the browser
if opts.LaunchDefaultBrowser || opts.LaunchFirefox {
var browser string
if opts.LaunchFirefox {
browser = "firefox"
}
if opts.LaunchDefaultBrowser {
browser = "default"
}
err = openBrowser(fmt.Sprintf("file://%s", galleryFileName), browser)
if err != nil {
panic(err)
}
}
}