From 0ab6cf2b6eb70e3b0a12409b28a22e544e561dd2 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 14 Feb 2022 15:02:13 +0100 Subject: [PATCH 1/4] Remove debug statement from upload endpoint test --- main_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 0ed7112..fe7a5c9 100644 --- a/main_test.go +++ b/main_test.go @@ -3,12 +3,12 @@ package main import ( "encoding/base64" "encoding/json" - "fmt" - "github.com/geplauder/lithium/controllers" "net/http" "net/http/httptest" "testing" + "github.com/geplauder/lithium/controllers" + "github.com/bxcodec/faker/v3" "github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/settings" @@ -139,6 +139,5 @@ func TestUploadRoute(t *testing.T) { assert.Equal(t, 0x1A6, responseRecorder.Code) str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==") assert.JSONEq(t, string(str), responseRecorder.Body.String()) - fmt.Println(responseRecorder.Body.String()) }) } From 728f53c38f73850ed8086cc10bf534880f5f6289 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 14 Feb 2022 15:08:55 +0100 Subject: [PATCH 2/4] Add get files endpoint --- controllers/file.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++++++ main_test.go | 35 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 controllers/file.go diff --git a/controllers/file.go b/controllers/file.go new file mode 100644 index 0000000..5384780 --- /dev/null +++ b/controllers/file.go @@ -0,0 +1,50 @@ +package controllers + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "time" + + "github.com/geplauder/lithium/pipelines" + "github.com/geplauder/lithium/settings" + "github.com/geplauder/lithium/storage" +) + +type File struct { + Bucket string `json:"bucket"` + FileName string `json:"file_name"` + Size int `json:"size"` + ModifiedAt time.Time `json:"modified_at"` +} + +func FileHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPipeline, storageProvider storage.IStorageProvider, appSettings settings.Settings) { + buckets, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath) + if err != nil { + json.NewEncoder(w).Encode(struct { + Files []File `json:"files"` + }{}) + return + } + + var files []File + + for _, b := range buckets { + bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name()) + if err != nil { + writeError(w, 500, "Base path not found") + return + } + + for _, f := range bucketFiles { + files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()}) + } + } + + err = json.NewEncoder(w).Encode(struct { + Files []File `json:"files"` + }{files}) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } +} diff --git a/main.go b/main.go index ad1a0f9..6bc9aa2 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,11 @@ func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pi controllers.UploadHandler(w, r, pipelines, storageProvider) }) + files := r.Methods(http.MethodGet).Subrouter() + files.HandleFunc("/files", func(w http.ResponseWriter, r *http.Request) { + controllers.FileHandler(w, r, pipelines, storageProvider, appSettings) + }) + pipeline := r.Methods(http.MethodGet).Subrouter() pipeline.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { for _, pipeline := range pipelines { @@ -45,6 +50,7 @@ func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pi authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token) upload.Use(authMiddleware.Middleware) + files.Use(authMiddleware.Middleware) pipeline.Use(authMiddleware.Middleware) } } diff --git a/main_test.go b/main_test.go index fe7a5c9..dd5af58 100644 --- a/main_test.go +++ b/main_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/geplauder/lithium/controllers" @@ -141,3 +142,37 @@ func TestUploadRoute(t *testing.T) { assert.JSONEq(t, string(str), responseRecorder.Body.String()) }) } + +func TestFilesRoute(t *testing.T) { + data := pipelines.Pipeline{} + err := faker.FakeData(&data) + assert.Nil(t, err) + + t.Run("Files endpoint returns files", func(t *testing.T) { + router := mux.NewRouter() + fs := storage.GetMemoryStorageProvider() + appSettings, _ := settings.LoadSettings(afero.NewMemMapFs()) + + RegisterRoutes(router, appSettings, []pipelines.IPipeline{data}, fs) + + request, _ := http.NewRequest("GET", "/files", nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + type responseBody struct { + Files []struct { + Bucket string `json:"bucket"` + FileName string `json:"file_name"` + Size int `json:"size"` + ModifiedAt time.Time `json:"modified_at"` + } `json:"files"` + } + + var res responseBody + + assert.Equal(t, 200, responseRecorder.Code) + err := json.Unmarshal(responseRecorder.Body.Bytes(), &res) + assert.Nil(t, err) + }) +} From 315c9adfca623ac62fcd3cb0474682d5b8961c2c Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 14 Feb 2022 15:09:38 +0100 Subject: [PATCH 3/4] Add check for directories in files endpoint --- controllers/file.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/controllers/file.go b/controllers/file.go index 5384780..853ead9 100644 --- a/controllers/file.go +++ b/controllers/file.go @@ -30,6 +30,10 @@ func FileHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPipe var files []File for _, b := range buckets { + if b.IsDir() == false { + continue + } + bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name()) if err != nil { writeError(w, 500, "Base path not found") @@ -37,6 +41,10 @@ func FileHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPipe } for _, f := range bucketFiles { + if b.IsDir() == false { + continue + } + files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()}) } } From b5c0c9700883928856820a678e432e78558862a6 Mon Sep 17 00:00:00 2001 From: Roman Zipp Date: Mon, 14 Feb 2022 15:11:05 +0100 Subject: [PATCH 4/4] Add get files endpoint readme docs --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index b01eb41..cfedeca 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,31 @@ Authorization: Bearer } ``` +### `GET` `/files` + +Show files. + +**Required headers**: + +```shell +Authorization: Bearer +``` + +**Example response**: + +```json +{ + "files": [ + { + "bucket": "foo", + "file_name": "output.jpg", + "size": 14523, + "modified_at": "2022-02-14T14:19:07.491308411+01:00" + } + ] +} +``` + ### `GET` `/pipelines/{pipeline}` Show pipeline information.