2020-06-23 04:11:56 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-10-14 02:45:52 +03:00
|
|
|
"bytes"
|
2021-02-19 10:05:52 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-23 04:11:56 +03:00
|
|
|
"io/ioutil"
|
2021-07-20 05:22:29 +03:00
|
|
|
"math/rand"
|
2021-02-19 10:05:52 +03:00
|
|
|
"net/url"
|
2020-06-23 04:11:56 +03:00
|
|
|
"os"
|
2021-02-19 10:05:52 +03:00
|
|
|
"os/exec"
|
2020-10-15 00:07:38 +03:00
|
|
|
"path"
|
2020-06-23 04:11:56 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-07-14 05:07:30 +03:00
|
|
|
|
|
|
|
"github.com/mssola/user_agent"
|
2021-02-19 10:05:52 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-14 02:45:52 +03:00
|
|
|
"github.com/yuin/goldmark"
|
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
|
|
"mvdan.cc/xurls"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// DoesFileExists checks if the file exists.
|
2020-06-23 04:11:56 +03:00
|
|
|
func DoesFileExists(name string) bool {
|
2021-07-10 04:31:43 +03:00
|
|
|
if _, err := os.Stat(name); err == nil {
|
|
|
|
return true
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
log.Errorln(err)
|
|
|
|
return false
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// GetRelativePathFromAbsolutePath gets the relative path from the provided absolute path.
|
2020-06-23 04:11:56 +03:00
|
|
|
func GetRelativePathFromAbsolutePath(path string) string {
|
|
|
|
pathComponents := strings.Split(path, "/")
|
|
|
|
variant := pathComponents[len(pathComponents)-2]
|
|
|
|
file := pathComponents[len(pathComponents)-1]
|
|
|
|
|
|
|
|
return filepath.Join(variant, file)
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// GetIndexFromFilePath is a utility that will return the index/key/variant name in a full path.
|
2020-10-15 00:07:38 +03:00
|
|
|
func GetIndexFromFilePath(path string) string {
|
|
|
|
pathComponents := strings.Split(path, "/")
|
|
|
|
variant := pathComponents[len(pathComponents)-2]
|
|
|
|
|
|
|
|
return variant
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Copy copies the file to destination.
|
2020-06-23 04:11:56 +03:00
|
|
|
func Copy(source, destination string) error {
|
2021-09-12 10:18:15 +03:00
|
|
|
input, err := ioutil.ReadFile(source) // nolint
|
2020-06-23 04:11:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
return ioutil.WriteFile(destination, input, 0600)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
2020-07-14 05:07:30 +03:00
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Move moves the file to destination.
|
2020-10-15 00:07:38 +03:00
|
|
|
func Move(source, destination string) error {
|
|
|
|
return os.Rename(source, destination)
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// IsUserAgentABot returns if a web client user-agent is seen as a bot.
|
2020-07-14 05:07:30 +03:00
|
|
|
func IsUserAgentABot(userAgent string) bool {
|
|
|
|
if userAgent == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
botStrings := []string{
|
|
|
|
"mastodon",
|
|
|
|
"pleroma",
|
2020-09-01 06:36:52 +03:00
|
|
|
"applebot",
|
2021-09-14 03:14:30 +03:00
|
|
|
"whatsapp",
|
2020-07-14 05:07:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, botString := range botStrings {
|
|
|
|
if strings.Contains(strings.ToLower(userAgent), botString) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ua := user_agent.New(userAgent)
|
|
|
|
return ua.Bot()
|
|
|
|
}
|
2020-10-14 02:45:52 +03:00
|
|
|
|
2021-09-01 00:49:04 +03:00
|
|
|
// IsUserAgentAPlayer returns if a web client user-agent is seen as a media player.
|
|
|
|
func IsUserAgentAPlayer(userAgent string) bool {
|
|
|
|
if userAgent == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
playerStrings := []string{
|
|
|
|
"mpv",
|
|
|
|
"player",
|
|
|
|
"vlc",
|
2021-09-01 01:04:29 +03:00
|
|
|
"applecoremedia",
|
2021-09-01 00:49:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, playerString := range playerStrings {
|
|
|
|
if strings.Contains(strings.ToLower(userAgent), playerString) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// RenderSimpleMarkdown will return HTML without sanitization or specific formatting rules.
|
2020-10-14 02:45:52 +03:00
|
|
|
func RenderSimpleMarkdown(raw string) string {
|
|
|
|
markdown := goldmark.New(
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.NewLinkify(
|
|
|
|
extension.WithLinkifyAllowedProtocols([][]byte{
|
|
|
|
[]byte("http:"),
|
|
|
|
[]byte("https:"),
|
|
|
|
}),
|
|
|
|
extension.WithLinkifyURLRegexp(
|
|
|
|
xurls.Strict,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
trimmed := strings.TrimSpace(raw)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := markdown.Convert([]byte(trimmed), &buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// RenderPageContentMarkdown will return HTML specifically handled for the user-specified page content.
|
2021-02-19 10:05:52 +03:00
|
|
|
func RenderPageContentMarkdown(raw string) string {
|
|
|
|
markdown := goldmark.New(
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.GFM,
|
|
|
|
extension.NewLinkify(
|
|
|
|
extension.WithLinkifyAllowedProtocols([][]byte{
|
|
|
|
[]byte("http:"),
|
|
|
|
[]byte("https:"),
|
|
|
|
}),
|
|
|
|
extension.WithLinkifyURLRegexp(
|
|
|
|
xurls.Strict,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
trimmed := strings.TrimSpace(raw)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := markdown.Convert([]byte(trimmed), &buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-23 03:27:39 +03:00
|
|
|
return strings.TrimSpace(buf.String())
|
2021-02-19 10:05:52 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// GetCacheDurationSecondsForPath will return the number of seconds to cache an item.
|
2020-10-15 00:07:38 +03:00
|
|
|
func GetCacheDurationSecondsForPath(filePath string) int {
|
2021-09-04 07:05:22 +03:00
|
|
|
filename := path.Base(filePath)
|
|
|
|
fileExtension := path.Ext(filePath)
|
|
|
|
|
2021-09-04 07:37:29 +03:00
|
|
|
if filename == "thumbnail.jpg" || filename == "preview.gif" {
|
|
|
|
// Thumbnails & preview gif re-generate during live
|
2020-10-15 00:07:38 +03:00
|
|
|
return 20
|
2021-09-04 07:05:22 +03:00
|
|
|
} else if fileExtension == ".js" || fileExtension == ".css" {
|
2020-10-15 00:07:38 +03:00
|
|
|
// Cache javascript & CSS
|
2021-09-04 07:05:22 +03:00
|
|
|
return 60 * 10
|
|
|
|
} else if fileExtension == ".ts" {
|
2020-10-15 00:07:38 +03:00
|
|
|
// Cache video segments as long as you want. They can't change.
|
|
|
|
// This matters most for local hosting of segments for recordings
|
|
|
|
// and not for live or 3rd party storage.
|
|
|
|
return 31557600
|
2021-09-04 07:05:22 +03:00
|
|
|
} else if fileExtension == ".m3u8" {
|
2020-10-15 00:07:38 +03:00
|
|
|
return 0
|
2021-09-04 07:38:25 +03:00
|
|
|
} else if fileExtension == ".jpg" || fileExtension == ".png" || fileExtension == ".gif" || fileExtension == ".svg" {
|
2021-09-04 07:05:22 +03:00
|
|
|
return 60 * 60 * 24
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default cache length in seconds
|
2021-09-04 07:05:22 +03:00
|
|
|
return 60 * 10
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// IsValidURL will return if a URL string is a valid URL or not.
|
|
|
|
func IsValidURL(urlToTest string) bool {
|
2021-07-09 21:16:44 +03:00
|
|
|
if _, err := url.ParseRequestURI(urlToTest); err != nil {
|
2021-02-19 10:05:52 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(urlToTest)
|
|
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatedFfmpegPath will take a proposed path to ffmpeg and return a validated path.
|
|
|
|
func ValidatedFfmpegPath(ffmpegPath string) string {
|
|
|
|
if ffmpegPath != "" {
|
|
|
|
if err := VerifyFFMpegPath(ffmpegPath); err == nil {
|
|
|
|
return ffmpegPath
|
|
|
|
}
|
2021-09-12 10:18:15 +03:00
|
|
|
log.Warnln(ffmpegPath, "is an invalid path to ffmpeg will try to use a copy in your path, if possible")
|
2021-02-19 10:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// First look to see if ffmpeg is in the current working directory
|
|
|
|
localCopy := "./ffmpeg"
|
|
|
|
hasLocalCopyError := VerifyFFMpegPath(localCopy)
|
|
|
|
if hasLocalCopyError == nil {
|
|
|
|
// No error, so all is good. Use the local copy.
|
|
|
|
return localCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("which", "ffmpeg")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2021-09-21 21:40:28 +03:00
|
|
|
log.Fatalln("Unable to determine path to ffmpeg. Please make sure it is installed either globally or a copy exists in the owncast directory.")
|
2021-02-19 10:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
path := strings.TrimSpace(string(out))
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyFFMpegPath verifies that the path exists, is a file, and is executable.
|
|
|
|
func VerifyFFMpegPath(path string) error {
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errors.New("ffmpeg path does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while verifying the ffmpeg path: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
|
|
|
return errors.New("ffmpeg path can not be a folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
mode := stat.Mode()
|
|
|
|
//source: https://stackoverflow.com/a/60128480
|
|
|
|
if mode&0111 == 0 {
|
|
|
|
return errors.New("ffmpeg path is not executable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-08 22:35:53 +03:00
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// CleanupDirectory removes the directory and makes it fresh again. Throws fatal error on failure.
|
2021-07-08 22:35:53 +03:00
|
|
|
func CleanupDirectory(path string) {
|
|
|
|
log.Traceln("Cleaning", path)
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
log.Fatalln("Unable to remove directory. Please check the ownership and permissions", err)
|
|
|
|
}
|
2021-09-12 10:18:15 +03:00
|
|
|
if err := os.MkdirAll(path, 0750); err != nil {
|
2021-07-08 22:35:53 +03:00
|
|
|
log.Fatalln("Unable to create directory. Please check the ownership and permissions", err)
|
|
|
|
}
|
2021-07-09 21:16:44 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// FindInSlice will return the index if a string is located in a slice of strings.
|
2021-07-20 05:22:29 +03:00
|
|
|
func FindInSlice(slice []string, val string) (int, bool) {
|
|
|
|
for i, item := range slice {
|
|
|
|
if item == val {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateRandomDisplayColor will return a random _hue_ to be used when displaying a user.
|
|
|
|
// The UI should determine the right saturation and lightness in order to make it look right.
|
|
|
|
func GenerateRandomDisplayColor() int {
|
|
|
|
rangeLower := 0
|
|
|
|
rangeUpper := 360
|
|
|
|
return rangeLower + rand.Intn(rangeUpper-rangeLower+1) //nolint
|
|
|
|
}
|