memories/go-vod/transcoder/rpdb.go

74 lines
2.0 KiB
Go

package transcoder
import (
"os"
"strings"
"git.rpjosh.de/RPJosh/RPdb/v4/go/api"
"git.rpjosh.de/RPJosh/RPdb/v4/go/models"
"git.rpjosh.de/RPJosh/go-logger"
)
// SERVER_PREFIX is the nextcloud data directory of the server
const SERVER_PREFIX = "/media/nextclouddata"
// CLIENT_PREFIX is the nextcloud data directory of the client
const CLIENT_PREFIX = "/media/ubuntugui/NextcloudCache"
// getClientPath returns the absolute path of the movie
// on the client.
// The server and client paths are different!
func getClientPath(path string) string {
if strings.HasPrefix(path, SERVER_PREFIX) {
return CLIENT_PREFIX + strings.TrimPrefix(path, SERVER_PREFIX)
} else {
logger.Warning("Server directory configuration is probably incorrect. Got directory %q", path)
return path
}
}
// getFileFromServer sends a request to the nextcloud server to
// copy the file to the shared volume both server and client can
// access
func (c *Config) getFileFromServer(path string) {
serverPath := strings.TrimPrefix(path, CLIENT_PREFIX)
// Creat entry
rsp, err := c.RPdbAPI.CreateEntry(models.Entry{
Attribute: &models.Attribute{ID: 315},
Offset: "now",
Parameters: []models.EntryParameter{{Value: SERVER_PREFIX + serverPath}},
})
if err != nil {
logger.Warning("Failed to create RPdb entry: %s", err)
return
}
if rsp.ResponseCode != 0 {
logger.Warning("Failed to copy file: %s", rsp.Response)
}
}
func GetRPdbAPI() *api.Api {
apiOptions := api.ApiOptions{BaseUrl: "https://rp.rpjosh.de/api/v1", MultiInstance: true}
return api.NewApi(readRPdbToken(), apiOptions)
}
// readRPdbToken reads the API-Key to use for RPdb.
// It panics if no environment variable was set or the specified
// file with the token does not exist
func readRPdbToken() string {
path := os.Getenv("RPDB_TOKEN")
if path == "" {
path = "./token.txt"
}
// Try to read the file
cnt, err := os.ReadFile(path)
if err != nil {
logger.Fatal("Failed to read RPdb API key from file %q: %s", path, err)
}
return string(cnt)
}