From 4191a4cefddf3c5b4a6d67f167ee1b2f0b7322c9 Mon Sep 17 00:00:00 2001 From: mysterion Date: Tue, 3 Sep 2024 17:11:08 +0530 Subject: [PATCH] !feat: changed release from tags to commits feat: simplified updates, and custom sha versions chore: updated `README.md` --- README.md | 36 ++++++-- avrp.go | 8 +- internal/thumbnails/thumbnails.go | 6 ++ internal/utils/video.go | 26 +++++- web/api/handlers.go | 3 +- web/dist/dist.go | 32 ++----- web/dist/download.go | 17 ++-- web/dist/update.go | 142 ++++++++++++++++++------------ 8 files changed, 168 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 9457954..0e5e48e 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,41 @@ single executable program for aframe-vr-player with update feature # usage ``` +Usage of avrp: + -dev + starts in dev mode, serves 'index.html' from current directory -dir string path to video files - + -reset + removes all configs, thumbnails & 'aframe-vr-player' files + -sha string + Optional - download a specific commit of aframe-vr-player (default "latest") -update - checks & downloads the latest version of 'aframe-vr-player' + checks & downloads the latest version(commit) of 'aframe-vr-player' +``` + +### Examples +```sh +# -flag +# --flag +# -flag=x + +# deletes all configs of avrp and aframe-vr-player / factory reset +avrp --reset + +# serve video directory +avrp --dir "C:\\Users\\User\\Video\\SFW\\" +# linux path +avrp --dir "/home/user/Videos/NSFW" + +# downloads the latest version(commit) of aframe-vr-player +avrp --update + +# downloads https://github.com/mysterion/aframe-vr-player/tree/6d1b7cfacfd873180e80fde53cb2a7873e1faffc +# https://github.com/mysterion/aframe-vr-player/commits/main/ +avrp --update --sha 6d1b7cfacfd873180e80fde53cb2a7873e1faffc - -dev - starts in dev mode, serves 'index.html' from current directory - -reset - removes all configs, thumbnails & 'aframe-vr-player' files ``` # installation ## go cli diff --git a/avrp.go b/avrp.go index 5161087..08990dc 100644 --- a/avrp.go +++ b/avrp.go @@ -43,11 +43,13 @@ func main() { log.SetFlags(log.Lshortfile | log.LstdFlags) var servDir string + var sha string var update bool var reset bool flag.BoolVar(&utils.DEV, "dev", false, "starts in dev mode, serves 'index.html' from current directory") - flag.BoolVar(&update, "update", false, "checks & downloads the latest version of 'aframe-vr-player'") + flag.BoolVar(&update, "update", false, "checks & downloads the latest version(commit) of 'aframe-vr-player'") + flag.StringVar(&sha, "sha", "latest", "Optional - download a specific commit of aframe-vr-player") flag.StringVar(&servDir, "dir", "", "path to video files") flag.BoolVar(&reset, "reset", false, "removes all configs, thumbnails & 'aframe-vr-player' files") @@ -69,7 +71,7 @@ func main() { } if update { - dist.Update() + dist.Update(sha) } /// do i need this anymore? @@ -77,7 +79,7 @@ func main() { // running for the first time if !dist.Valid() && !utils.DEV { - dist.Update() + dist.Update("latest") } if len(servDir) == 0 { diff --git a/internal/thumbnails/thumbnails.go b/internal/thumbnails/thumbnails.go index 6f90b46..bd12925 100644 --- a/internal/thumbnails/thumbnails.go +++ b/internal/thumbnails/thumbnails.go @@ -1,6 +1,7 @@ package thumbnails import ( + "errors" "fmt" "log" "math" @@ -18,6 +19,8 @@ import ( var thumbdir string +var ErrNotVideo = errors.New("not a video") + var Available = true var muGen sync.Mutex @@ -38,6 +41,9 @@ func Init() { } func GetDuration(file string) (float64, error) { + if !utils.IsVideo(file) { + return 0, ErrNotVideo + } var secs string secs = cache.Get("DUR_" + file) if secs == "" { diff --git a/internal/utils/video.go b/internal/utils/video.go index b9bd9c3..6039a4e 100644 --- a/internal/utils/video.go +++ b/internal/utils/video.go @@ -1,8 +1,10 @@ package utils -import "strings" +import ( + "strings" +) -var exts = []string{ +var video = []string{ "3g2", "3gp", "aaf", @@ -37,12 +39,30 @@ var exts = []string{ "webm", "wmv", "yuv", +} + +var subs = []string{ "srt", } +func CanServe(file string) bool { + f := strings.ToLower(file) + for _, e := range video { + if strings.HasSuffix(f, e) { + return true + } + } + for _, e := range subs { + if strings.HasSuffix(f, e) { + return true + } + } + return false +} + func IsVideo(file string) bool { f := strings.ToLower(file) - for _, e := range exts { + for _, e := range video { if strings.HasSuffix(f, e) { return true } diff --git a/web/api/handlers.go b/web/api/handlers.go index 6bc80a7..dbfd9a5 100644 --- a/web/api/handlers.go +++ b/web/api/handlers.go @@ -70,9 +70,10 @@ func listFilesAndFolders(dirPath string) ([]File, []string, error) { if entry.IsDir() { folders = append(folders, entry.Name()) } else { - if !utils.IsVideo(entry.Name()) { + if !utils.CanServe(entry.Name()) { continue } + secs, _ := thumbnails.GetDuration(path.Join(dirPath, entry.Name())) files = append(files, File{Name: entry.Name(), Duration: int(secs)}) diff --git a/web/dist/dist.go b/web/dist/dist.go index 56f56ef..99d0718 100644 --- a/web/dist/dist.go +++ b/web/dist/dist.go @@ -1,21 +1,13 @@ package dist import ( - "errors" - "io/fs" "log" - "math" "os" "path/filepath" - "strconv" - "strings" "github.com/mysterion/avrp/internal/utils" ) -const RepoOwner = "mysterion" -const RepoName = "aframe-vr-player" - var VersionFile string func Init() { @@ -32,26 +24,14 @@ func Delete() error { return os.RemoveAll(utils.DistDir) } -// returns 0 , when no dist -func Ver() int { - fd, err := os.Open(VersionFile) - if errors.Is(err, fs.ErrNotExist) { - log.Println("ERR: Version file doesn't exist") - return 0 - } - defer fd.Close() - - d := make([]byte, 1024) - n, err := fd.Read(d) - if err != nil { - return math.MaxInt - } +// returns sha of aframe-vr-player dist +func Ver() string { + d, err := os.ReadFile(VersionFile) - vs := strings.ReplaceAll(string(d[:n]), ".", "") - v, err := strconv.Atoi(vs) if err != nil { - return math.MaxInt + log.Printf("WARN: while reading dist version, %v\n", err.Error()) + return "" } - return v + return string(d) } diff --git a/web/dist/download.go b/web/dist/download.go index 8ef0bf9..e5023b8 100644 --- a/web/dist/download.go +++ b/web/dist/download.go @@ -13,11 +13,12 @@ import ( "github.com/mysterion/avrp/internal/utils" ) -func DownloadTag(t Tag) error { +func DownloadCommit(sha string) error { - log.Printf("Latest Release - %v - %v\n", t.Name, t.ZipballUrl) + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/zipball/%s", RepoOwner, RepoName, sha) + log.Printf("Downloading - %v\n", url) - zipFile, err := os.CreateTemp("", "avrp-latest") + zipFile, err := os.CreateTemp("", "aframe-vr-player") if err != nil { return err } @@ -25,7 +26,7 @@ func DownloadTag(t Tag) error { log.Println("Downloading to - ", zipFile.Name()) - resp, err := http.Get(t.ZipballUrl) + resp, err := http.Get(url) if err != nil { return err } @@ -36,13 +37,13 @@ func DownloadTag(t Tag) error { return err } - err = extractZip(t, zipFile.Name()) + err = extractZip(sha, zipFile.Name()) if err != nil { log.Println("ERR: Failed to extract the zip") return err } - err = os.WriteFile(VersionFile, []byte(t.Name), 0644) + err = os.WriteFile(VersionFile, []byte(sha), 0644) if err != nil { log.Println("ERR: Failed to write version file") @@ -56,7 +57,7 @@ func DownloadTag(t Tag) error { return nil } -func extractZip(t Tag, srcZip string) error { +func extractZip(sha string, srcZip string) error { log.Printf("Extracting %v", srcZip) r, err := zip.OpenReader(srcZip) @@ -70,7 +71,7 @@ func extractZip(t Tag, srcZip string) error { return err } - folderName := fmt.Sprintf("%s-%s-%s", RepoOwner, RepoName, t.Commit.Sha[:7]) + folderName := fmt.Sprintf("%s-%s-%s", RepoOwner, RepoName, sha[:7]) for _, f := range r.File { fi := f.FileInfo() diff --git a/web/dist/update.go b/web/dist/update.go index 5991b55..b70ed05 100644 --- a/web/dist/update.go +++ b/web/dist/update.go @@ -8,110 +8,142 @@ import ( "io/fs" "log" "net/http" - "os" - "strconv" - "strings" + "time" ) +const RepoOwner = "mysterion" +const RepoName = "aframe-vr-player" + +// curl https://api.github.com/repos/mysterion/aframe-vr-player/commits?per_page=1 type Commit struct { - Sha string `json:"sha"` - Url string `json:"url"` + Sha string `json:"sha"` + Details CommitDetails `json:"commit"` } -type Tag struct { - Name string `json:"name"` - ZipballUrl string `json:"zipball_url"` - TarballUrl string `json:"tarball_url"` - Commit Commit `json:"commit"` - NodeId string `json:"node_id"` +type CommitDetails struct { + Sha string `json:"sha"` + Author struct { + Name string `json:"name"` + Date time.Time `json:"date"` + } `json:"author"` + Message string `json:"message"` } -func (t *Tag) Version() int { - vs := strings.ReplaceAll(t.Name, ".", "") - v, err := strconv.Atoi(vs) +func GetLatestCommit() (Commit, error) { + + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits?per_page=1", RepoOwner, RepoName) + + var commit Commit + var commits []Commit + + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) if err != nil { - return 0 + return commit, err } - return v -} -func LatestTag() (Tag, error) { - tags, err := AllTags() + req.Header.Set("Accept", "application/json") + + resp, err := client.Do(req) if err != nil { - return Tag{}, err + return commit, err } - return tags[0], nil -} - -func AllTags() ([]Tag, error) { + defer resp.Body.Close() - url := os.Getenv("URL_ALL_RELEASES") - if url == "" { - url = fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", RepoOwner, RepoName) + body, err := io.ReadAll(resp.Body) + if err != nil { + return commit, err } + err = json.Unmarshal(body, &commits) + if err != nil { + return commit, err + } + if len(commits) < 1 { + return commit, fmt.Errorf("No commits found 🤨") + } + return commits[0], err +} - var tags []Tag +func GetCommit(sha string) (CommitDetails, error) { + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/git/commits/%s", RepoOwner, RepoName, sha) + + var commit CommitDetails client := &http.Client{} req, err := http.NewRequest("GET", url, nil) if err != nil { - return tags, err + return commit, err } req.Header.Set("Accept", "application/json") resp, err := client.Do(req) if err != nil { - return tags, err + return commit, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return tags, err + return commit, err } - - err = json.Unmarshal(body, &tags) + err = json.Unmarshal(body, &commit) if err != nil { - return tags, err + return commit, err } - - return tags, err + return commit, nil } -// Updates if latest version > current version -func Update() { +func Update(sha string) error { - v := Ver() - log.Printf("Current version: %v\n", v) + if sha == "latest" { + v := Ver() + log.Printf("Current version: %v\n", v) + log.Println("Checking for updates") + c, err := GetLatestCommit() + if err != nil { + log.Println("ERR: Failed to fetch latest version", err) + return err + } - log.Println("Checking for updates") + log.Printf("Commit Details :-\n\nDate: %v\nMessage:\n%v\n\n", c.Details.Author.Date, c.Details.Message) - t, err := LatestTag() - if err != nil { - log.Println("ERR: Failed to fetch latest Release", err) - log.Println("Skipping Update check") - return - } + if Valid() { + if v == c.Sha { + o := "this" + if sha == "latest" { + o = "the latest" + } + log.Printf("Already on %s version\n", o) + return nil + } + } - log.Printf("Latest release: %v, v:%v\n", t.Name, t.Version()) + sha = c.Sha - if v >= t.Version() { - log.Println("Already on the latest version") - return + } else { + c, err := GetCommit(sha) + if err != nil { + log.Printf("ERR: Failed to get %s of aframe-vr-player\n", sha) + log.Printf("ERR: %v\n", err.Error()) + return err + } + log.Println(c, err) + log.Printf("Commit Details :-\n\nDate: %v\nMessage:\n%v\n\n", c.Author.Date, c.Message) } - log.Println("Downloading the latest version") + fmt.Println("\n\nPress Enter to update") + fmt.Scanln() - err = DownloadTag(t) + err := DownloadCommit(sha) if err != nil { log.Println("ERR: Failed to fetch latest Release", err) - log.Println("Skipping Update check") if !Valid() { if err := Delete(); errors.Is(err, fs.ErrNotExist) { panic(err) } } - return + return err } + return nil }