-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
105 lines (97 loc) · 2.38 KB
/
util.go
File metadata and controls
105 lines (97 loc) · 2.38 KB
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
package main
import (
"io/fs"
"log"
"os/exec"
"runtime"
"strings"
"github.com/spf13/afero"
)
func openBrowser(url string, browser string) error {
validBrowsers := []string{"firefox", "default"}
isValidBrowserOption := false
for _, b := range validBrowsers {
if b == browser {
isValidBrowserOption = true
break
}
}
if !isValidBrowserOption {
log.Fatalf("Invalid browser option: %s", browser)
return nil
}
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
if browser == "firefox" {
args = append(args, "firefox", "-private-window")
}
default:
if browser == "firefox" {
cmd = "firefox"
args = []string{"-private-window"}
} else {
cmd = "xdg-open"
args = []string{}
}
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func fileIsVideo(file fs.FileInfo) bool {
videoFileTypes := []string{
".mp4",
".webm",
".ogg",
".mov",
".mpg",
}
for _, fileType := range videoFileTypes {
if strings.HasSuffix(file.Name(), fileType) {
return true
}
}
return false
}
// fileIsImage checks if a file is a browser-compatible image based on its extension
func fileIsImage(file fs.FileInfo) bool {
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
imageFileTypes := []string{
".apng", // APNG
".avif", // AVIF
".gif", // GIF
".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", // JPEG
".png", // PNG
".svg", // SVG
".webp", // WebP
".bmp", // BMP
".ico", ".cur", // ICO
".tiff", ".tif", // TIFF
}
for _, fileType := range imageFileTypes {
if strings.HasSuffix(file.Name(), fileType) {
return true
}
}
return false
}
// getImagesInDirectory reads the files in a directory and returns a list of images
func getImagesInDirectory(afs afero.Fs, dir string) []fs.FileInfo {
files, err := afero.ReadDir(afs, dir)
if err != nil {
log.Fatalf("Error reading directory: %s", err)
return nil
}
images := []fs.FileInfo{}
for _, file := range files {
if fileIsImage(file) || fileIsVideo(file) {
images = append(images, file)
}
}
log.Printf("Found %d images in the directory", len(images))
log.Printf("%d non-images were excluded from the gallery", len(files)-len(images))
return images
}