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)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
input, err := ioutil.ReadFile(source)
|
|
|
|
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",
|
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
|
|
|
|
|
|
|
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-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 {
|
|
|
|
if path.Base(filePath) == "thumbnail.jpg" {
|
|
|
|
// Thumbnails re-generate during live
|
|
|
|
return 20
|
|
|
|
} else if path.Ext(filePath) == ".js" || path.Ext(filePath) == ".css" {
|
|
|
|
// Cache javascript & CSS
|
|
|
|
return 60
|
|
|
|
} else if path.Ext(filePath) == ".ts" {
|
|
|
|
// 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
|
|
|
|
} else if path.Ext(filePath) == ".m3u8" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default cache length in seconds
|
|
|
|
return 30
|
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
log.Warnln(ffmpegPath, "is an invalid path to ffmpeg will try to use a copy in your path, if possible")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
log.Fatalln("Unable to determine path to ffmpeg. Please specify it in the config file.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Removes the directory and makes it again. Throws fatal error on failure.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path, 0777); err != nil {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|