summaryrefslogtreecommitdiff
path: root/Endpoints/SongList.go
diff options
context:
space:
mode:
authorPhysick <96335032+DegustatorPonos@users.noreply.github.com>2025-12-06 12:05:43 +0500
committerPhysick <96335032+DegustatorPonos@users.noreply.github.com>2025-12-06 12:05:43 +0500
commit5fd81183f54662fdf08549c0d635e80166c62fe9 (patch)
tree0b730dd4f19c7446fabcc2ee7cc7313e94e67fe3 /Endpoints/SongList.go
initial endpoints
Diffstat (limited to 'Endpoints/SongList.go')
-rw-r--r--Endpoints/SongList.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/Endpoints/SongList.go b/Endpoints/SongList.go
new file mode 100644
index 0000000..9830c74
--- /dev/null
+++ b/Endpoints/SongList.go
@@ -0,0 +1,56 @@
+package endpoints
+
+import (
+ "encoding/json"
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+
+ audio "physick.ru/culture_exam/Audio"
+ settings "physick.ru/culture_exam/Settings"
+)
+
+type Songs = struct {
+ Name string
+ Duration int
+}
+
+type SongsList struct {
+ Songs []Songs
+}
+
+func (base SongsList) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
+
+func getSongs(w http.ResponseWriter, r *http.Request) {
+ var files, dirErr = os.ReadDir(settings.Current.SongsLocation)
+ if dirErr != nil {
+ log.Printf("Failed to look up songs list: %s", dirErr.Error())
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ var outp = SongsList {
+ Songs: make([]Songs, 0, len(files)),
+ }
+ for _, v := range files {
+ var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, v.Name())
+ var duration, durErr = audio.GetSongDuration(fullPath)
+ if durErr != nil {
+ log.Printf("Failed to parse file metadata: File: %s, error: %s\n", fullPath, durErr.Error())
+ continue
+ }
+ var song = Songs {
+ Name: v.Name(),
+ Duration: int(duration.Seconds()),
+ }
+ outp.Songs = append(outp.Songs, song)
+ }
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprint(w, outp)
+}