owncast/utils.go

86 lines
1.9 KiB
Go
Raw Normal View History

2020-05-30 03:08:33 +02:00
package main
import (
2020-06-09 10:52:15 +02:00
"fmt"
"io/ioutil"
"net/http"
2020-05-30 03:08:33 +02:00
"os"
2020-06-09 10:52:15 +02:00
"path"
"path/filepath"
2020-06-09 10:52:15 +02:00
"strconv"
"strings"
log "github.com/sirupsen/logrus"
2020-05-30 03:08:33 +02:00
)
func getTempPipePath() string {
return filepath.Join(os.TempDir(), "streampipe.flv")
}
2020-06-02 01:53:31 +02:00
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
2020-06-04 08:53:39 +02:00
2020-06-09 10:52:15 +02: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-06-04 08:53:39 +02:00
func verifyError(e error) {
if e != nil {
2020-06-09 10:52:15 +02:00
log.Panic(e)
}
}
func copy(src, dst string) {
input, err := ioutil.ReadFile(src)
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(dst, input, 0644)
if err != nil {
fmt.Println("Error creating", dst)
fmt.Println(err)
return
}
}
func resetDirectories(configuration Config) {
// Wipe the public, web-accessible hls data directory
os.RemoveAll(configuration.PublicHLSPath)
os.MkdirAll(configuration.PublicHLSPath, 0777)
// Create private hls data dirs
os.RemoveAll(configuration.PrivateHLSPath)
for index := range configuration.VideoSettings.StreamQualities {
os.MkdirAll(path.Join(configuration.PrivateHLSPath, strconv.Itoa(index)), 0777)
os.MkdirAll(path.Join(configuration.PublicHLSPath, strconv.Itoa(index)), 0777)
2020-06-04 08:53:39 +02:00
}
}
func getClientIDFromRequest(req *http.Request) string {
var ipAddress string
xForwardedFor := req.Header.Get("X-FORWARDED-FOR")
if xForwardedFor != "" {
ipAddress = xForwardedFor
} else {
ipAddressString := req.RemoteAddr
ipAddressComponents := strings.Split(ipAddressString, ":")
ipAddressComponents[len(ipAddressComponents)-1] = ""
ipAddress = strings.Join(ipAddressComponents, ":")
}
// fmt.Println("IP address determined to be", ipAddress)
return ipAddress
}