- streamd: MPD-only start, proxy field, ffmpeg/NRE/mp4decrypt auto-install - GET/PUT /api/modules for module.yaml sync; agent sync_modules - RTE Live-tab AutoPlay fixes; modules zip named bbc/rte/tg4.zip - Telegram notify scripts wired into Forgejo release workflows
153 lines
3.6 KiB
Go
153 lines
3.6 KiB
Go
package deps
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// extractNamed finds a file whose base name matches wantName (case-insensitive
|
|
// on Windows) inside archivePath and writes it to destPath.
|
|
func extractNamed(archivePath, _ /*binDir*/, wantName, destPath string) error {
|
|
lower := strings.ToLower(archivePath)
|
|
switch {
|
|
case strings.HasSuffix(lower, ".zip"):
|
|
return extractZipNamed(archivePath, wantName, destPath)
|
|
case strings.HasSuffix(lower, ".tar.gz"), strings.HasSuffix(lower, ".tgz"):
|
|
return extractTarGZNamed(archivePath, wantName, destPath)
|
|
case strings.HasSuffix(lower, ".tar.xz"):
|
|
return extractTarXZNamed(archivePath, wantName, destPath)
|
|
default:
|
|
return fmt.Errorf("unsupported archive type: %s", archivePath)
|
|
}
|
|
}
|
|
|
|
func nameMatch(base, want string) bool {
|
|
return strings.EqualFold(filepath.Base(base), want)
|
|
}
|
|
|
|
func extractZipNamed(archivePath, wantName, destPath string) error {
|
|
r, err := zip.OpenReader(archivePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
for _, f := range r.File {
|
|
if f.FileInfo().IsDir() {
|
|
continue
|
|
}
|
|
if !nameMatch(f.Name, wantName) {
|
|
continue
|
|
}
|
|
rc, err := f.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = writeFile(destPath, rc)
|
|
_ = rc.Close()
|
|
return err
|
|
}
|
|
return fmt.Errorf("%s not found in %s", wantName, filepath.Base(archivePath))
|
|
}
|
|
|
|
func extractTarGZNamed(archivePath, wantName, destPath string) error {
|
|
f, err := os.Open(archivePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
gz, err := gzip.NewReader(f)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer gz.Close()
|
|
return readTarNamed(gz, wantName, destPath, filepath.Base(archivePath))
|
|
}
|
|
|
|
// extractTarXZNamed shells out to tar when available (linux/mac; Git Bash on Windows).
|
|
func extractTarXZNamed(archivePath, wantName, destPath string) error {
|
|
tmp, err := os.MkdirTemp("", "ffmpeg-xz-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(tmp)
|
|
|
|
tarBin, err := exec.LookPath("tar")
|
|
if err != nil {
|
|
return fmt.Errorf("tar required to extract .tar.xz (install tar or place %s in bin/): %w", wantName, err)
|
|
}
|
|
cmd := exec.Command(tarBin, "-xJf", archivePath, "-C", tmp)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
return fmt.Errorf("tar extract: %w (%s)", err, strings.TrimSpace(string(out)))
|
|
}
|
|
var found string
|
|
_ = filepath.Walk(tmp, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil || info.IsDir() {
|
|
return nil
|
|
}
|
|
if nameMatch(path, wantName) {
|
|
found = path
|
|
return io.EOF
|
|
}
|
|
return nil
|
|
})
|
|
if found == "" {
|
|
return fmt.Errorf("%s not found in %s", wantName, filepath.Base(archivePath))
|
|
}
|
|
in, err := os.Open(found)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer in.Close()
|
|
return writeFile(destPath, in)
|
|
}
|
|
|
|
func readTarNamed(r io.Reader, wantName, destPath, archiveBase string) error {
|
|
tr := tar.NewReader(r)
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hdr.Typeflag != tar.TypeReg {
|
|
continue
|
|
}
|
|
if !nameMatch(hdr.Name, wantName) {
|
|
continue
|
|
}
|
|
return writeFile(destPath, tr)
|
|
}
|
|
return fmt.Errorf("%s not found in %s", wantName, archiveBase)
|
|
}
|
|
|
|
func writeFile(dest string, r io.Reader) error {
|
|
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
|
|
return err
|
|
}
|
|
tmp := dest + ".partial"
|
|
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, copyErr := io.Copy(f, r)
|
|
closeErr := f.Close()
|
|
if copyErr != nil {
|
|
_ = os.Remove(tmp)
|
|
return copyErr
|
|
}
|
|
if closeErr != nil {
|
|
_ = os.Remove(tmp)
|
|
return closeErr
|
|
}
|
|
_ = os.Remove(dest)
|
|
return os.Rename(tmp, dest)
|
|
}
|