From fe38aefcc93d95dc211369ca3dfbd7f6e01f3162 Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Thu, 21 May 2026 16:07:19 +0200 Subject: [PATCH] basic files api going --- main.go | 9 ++- pkg/dbhandling/filesapi.go | 124 +++++++++++++++++++++++++++----- pkg/dbhandling/filesapi_test.go | 31 ++++++++ 3 files changed, 141 insertions(+), 23 deletions(-) create mode 100644 pkg/dbhandling/filesapi_test.go diff --git a/main.go b/main.go index 16473ca..cbdb269 100644 --- 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) diff --git a/pkg/dbhandling/filesapi.go b/pkg/dbhandling/filesapi.go index c79d1b8..3a03ee7 100644 --- a/pkg/dbhandling/filesapi.go +++ b/pkg/dbhandling/filesapi.go @@ -1,39 +1,127 @@ 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 index 0000000..bee9ba9 --- /dev/null +++ b/pkg/dbhandling/filesapi_test.go @@ -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") } + } + }) +} -- 2.54.0