Browse Source

Add get files endpoint

Roman Zipp 3 years ago
committed by Fabian Vowie
parent
commit
61fc72d051
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 47
      controllers/file.go
  2. 6
      main.go
  3. 36
      main_test.go

47
controllers/file.go

@ -0,0 +1,47 @@
package controllers
import (
"encoding/json"
"github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/settings"
"github.com/geplauder/lithium/storage"
"io/ioutil"
"net/http"
"time"
)
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 {
writeError(w, 500, "Base path not found")
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)
}
}

6
main.go

@ -29,6 +29,11 @@ func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pi
controllers.UploadHandler(w, r, pipelines, storageProvider) 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 := r.Methods(http.MethodGet).Subrouter()
pipeline.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) { pipeline.HandleFunc("/pipelines/{pipeline}", func(w http.ResponseWriter, r *http.Request) {
for _, pipeline := range pipelines { for _, pipeline := range pipelines {
@ -45,6 +50,7 @@ func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pi
authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token) authMiddleware := middlewares.CreateAuthenticationMiddleware(appSettings.Authentication.Token)
upload.Use(authMiddleware.Middleware) upload.Use(authMiddleware.Middleware)
files.Use(authMiddleware.Middleware)
pipeline.Use(authMiddleware.Middleware) pipeline.Use(authMiddleware.Middleware)
} }
} }

36
main_test.go

@ -3,11 +3,11 @@ package main
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt"
"github.com/geplauder/lithium/controllers" "github.com/geplauder/lithium/controllers"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
"github.com/bxcodec/faker/v3" "github.com/bxcodec/faker/v3"
"github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/pipelines"
@ -141,3 +141,37 @@ func TestUploadRoute(t *testing.T) {
assert.JSONEq(t, string(str), responseRecorder.Body.String()) 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)
})
}
Loading…
Cancel
Save