2024-06-22 01:16:24 +01:00
|
|
|
package utilities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2024-06-25 04:45:05 +01:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2024-06-22 01:16:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetFQDN(url string) string {
|
|
|
|
r := regexp.MustCompile(`http(s)?:\/\/`)
|
|
|
|
|
|
|
|
return r.ReplaceAllString(url, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnspecifiedProgramError struct{}
|
|
|
|
|
|
|
|
func (e UnspecifiedProgramError) Error() string {
|
|
|
|
return "the program to view these files is unspecified"
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenMedia(viewer string, paths []string) error {
|
|
|
|
if viewer == "" {
|
|
|
|
return UnspecifiedProgramError{}
|
|
|
|
}
|
|
|
|
|
2024-06-25 04:45:05 +01:00
|
|
|
cmd := slices.Concat(strings.Split(viewer, " "), paths)
|
|
|
|
|
|
|
|
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
|
2024-06-22 01:16:24 +01:00
|
|
|
|
|
|
|
if err := command.Start(); err != nil {
|
2024-06-25 04:45:05 +01:00
|
|
|
return fmt.Errorf("received an error after starting the program: %w", err)
|
2024-06-22 01:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-26 12:38:27 +01:00
|
|
|
|
|
|
|
type UnspecifiedBrowserError struct{}
|
|
|
|
|
|
|
|
func (e UnspecifiedBrowserError) Error() string {
|
|
|
|
return "the browser to view this link is not specified"
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenLink(browser, url string) error {
|
|
|
|
if browser == "" {
|
|
|
|
return UnspecifiedBrowserError{}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := strings.Split(browser, " ")
|
|
|
|
cmd = append(cmd, url)
|
|
|
|
|
2024-08-17 11:23:21 +01:00
|
|
|
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
|
2024-06-26 12:38:27 +01:00
|
|
|
|
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return fmt.Errorf("received an error after starting the program to view the link: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|