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. diff --git a/controllers/file.go b/controllers/file.go new file mode 100644 index 0000000..853ead9 --- /dev/null +++ b/controllers/file.go @@ -0,0 +1,58 @@ +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 { + 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") + return + } + + for _, f := range bucketFiles { + if b.IsDir() == false { + continue + } + + 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 0ed7112..dd5af58 100644 --- a/main_test.go +++ b/main_test.go @@ -3,11 +3,12 @@ package main import ( "encoding/base64" "encoding/json" - "fmt" - "github.com/geplauder/lithium/controllers" "net/http" "net/http/httptest" "testing" + "time" + + "github.com/geplauder/lithium/controllers" "github.com/bxcodec/faker/v3" "github.com/geplauder/lithium/pipelines" @@ -139,6 +140,39 @@ 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()) + }) +} + +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) }) }