]> git.example.dev Git - binbsis50-sm.git/commitdiff
basic files api going
author2weiEmu <saalbach.robert@outlook.de>
Thu, 21 May 2026 14:07:19 +0000 (16:07 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Thu, 21 May 2026 14:07:19 +0000 (16:07 +0200)
main.go
pkg/dbhandling/filesapi.go
pkg/dbhandling/filesapi_test.go [new file with mode: 0644]

diff --git a/main.go b/main.go
index 16473cae2f853a3ff88c33dbae14d8f0f9cd596b..cbdb269949024030c417e92627ef8d2e63d68cbd 100644 (file)
--- a/main.go
+++ b/main.go
@@ -66,11 +66,10 @@ func main() {
 
        // api/files - api related to song files
        mux.HandleFunc("GET /api/files/get", DBwrapper.FilesGet)
-       mux.HandleFunc("POST /api/files/createWithURL", DBwrapper.FilesCreateWithURL)
-       mux.HandleFunc("POST /api/files/createWithUpload", DBwrapper.FilesCreateWithUpload)
-       mux.HandleFunc("DELETE /api/files/delete/{id}", DBwrapper.FilesDeleteById)
-       mux.HandleFunc("PUT /api/files/rename/{id}", DBwrapper.FilesRenameById)
-       mux.HandleFunc("PUT /api/files/replace/{id}", DBwrapper.FilesReplaceById)
+       mux.HandleFunc("POST /api/files/createWithURL/{filename}", DBwrapper.FilesCreateWithURL)
+       mux.HandleFunc("POST /api/files/createWithUpload/{filename}", DBwrapper.FilesCreateWithUpload)
+       mux.HandleFunc("DELETE /api/files/delete/{name}", DBwrapper.FilesDeleteByName)
+       mux.HandleFunc("PUT /api/files/rename/{name}/{newname}", DBwrapper.FilesRenameByName)
 
        listenPort := ":" + strconv.Itoa(*paramPort)
 
index c79d1b8ae1effa2b84304fb53d39afab6f83ad4f..3a03ee760ca0c691216b92a8ea7e113f3a6e6b1b 100644 (file)
 package dbhandling
 
-import "net/http"
+import (
+       "encoding/json"
+       "io"
+       "net/http"
+       "os"
+)
 
-/** TODO
- *
+/**
+ * Get all the names of all the files
  **/
 func (dbw *DBWrapper) FilesGet(w http.ResponseWriter, r *http.Request) {
+       entry, err := os.ReadDir(dbw.PathToSongs)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       var nameList []string
+       for _, v := range entry {
+               nameList = append(nameList, v.Name())
+       }
+
+       bytes, err := json.Marshal(nameList)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       w.Write(bytes)
+       w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- *
+/** TEST
+ * Get the file from a URL (of an m4a or mp3 etc, it has to be audio file for now)
+ * and add it to the song files directory
+ * TODO: make it so that it could perhaps accept things like youtube videos
  **/
 func (dbw *DBWrapper) FilesCreateWithURL(w http.ResponseWriter, r *http.Request) {
+       // the URL should be the thing in the body
+       bytes, err := io.ReadAll(r.Body)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+       // TODO: make sure that the URL is clean
+       url := string(bytes)
+       filename := r.PathValue("filename")
+
+       file, err := os.Create(dbw.PathToSongs + "/" + filename)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       defer file.Close()
+
+       resp, err := http.Get(url)
+       if resp.Status != "200 OK" {
+               w.WriteHeader(http.StatusNotFound)
+               return
+       }
+
+
+       _, err = io.Copy(file, resp.Body)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- *
+/** TEST
+ * Create a file in the directory using an attached upload (i.e. the file is in the body of the request I think is what I meant
+ * that and the filename, i.e. the name given to the location should be in the path
  **/
 func (dbw *DBWrapper) FilesCreateWithUpload(w http.ResponseWriter, r *http.Request) {
-}
+       filename := r.PathValue("filename")
 
-/** TODO
- *
- **/
-func (dbw *DBWrapper) FilesDeleteById(w http.ResponseWriter, r *http.Request) {
+       file, err := os.Create(dbw.PathToSongs + "/" + filename)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+       defer file.Close()
+
+       _, err = io.Copy(file, r.Body)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- *
+/** TEST
+ * Using the given name of the files, in the Path, delete that file
  **/
-func (dbw *DBWrapper) FilesRenameById(w http.ResponseWriter, r *http.Request) {
+func (dbw *DBWrapper) FilesDeleteByName(w http.ResponseWriter, r *http.Request) {
+       name := r.PathValue("name")
+
+       err := os.Remove(dbw.PathToSongs + "/" + name)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- *
+/** TEST
+ * Given the name in the path, rename that file to the new name, also given in the path
  **/
-func (dbw *DBWrapper) FilesReplaceById(w http.ResponseWriter, r *http.Request) {
+func (dbw *DBWrapper) FilesRenameByName(w http.ResponseWriter, r *http.Request) {
+       name := r.PathValue("name")
+       newName := r.PathValue("newname")
+
+       err := os.Rename(dbw.PathToSongs + "/" + name, dbw.PathToSongs + "/" + newName)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+       
+       w.WriteHeader(http.StatusOK)
 }
diff --git a/pkg/dbhandling/filesapi_test.go b/pkg/dbhandling/filesapi_test.go
new file mode 100644 (file)
index 0000000..bee9ba9
--- /dev/null
@@ -0,0 +1,31 @@
+package dbhandling_test
+
+import (
+       "encoding/json"
+       "net/http"
+       "net/http/httptest"
+       "songmanager/pkg/dbhandling"
+       "testing"
+)
+
+func TestFilesGet(t *testing.T) {
+       wrapper := dbhandling.NewDbWrapper("localhost:6379", "../../testing/songs")
+
+       t.Run("FilesGet1", func(t *testing.T) {
+               req := httptest.NewRequest(http.MethodGet, "/api/files/get", &EmptyReader{})
+               rec := httptest.NewRecorder()
+
+               wrapper.FilesGet(rec, req)
+
+               var fileList []string
+               err := json.Unmarshal(rec.Body.Bytes(), &fileList)
+               if err != nil { t.Error("Did not return valid JSON:", err) }
+
+               // check that the list is of a certain length and that those are not empty entries
+               if len(fileList) < 10 { t.Error("There was not at least 10 songs in the file list") }
+
+               for _, v := range fileList {
+                       if v == "" { t.Error("Empty entry in the file list") }
+               }
+       })
+}