// 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)
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)
}
--- /dev/null
+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") }
+ }
+ })
+}