Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

  1. package controllers
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. "github.com/geplauder/lithium/pipelines"
  8. "github.com/geplauder/lithium/settings"
  9. "github.com/geplauder/lithium/storage"
  10. )
  11. type File struct {
  12. Bucket string `json:"bucket"`
  13. FileName string `json:"file_name"`
  14. Size int `json:"size"`
  15. ModifiedAt time.Time `json:"modified_at"`
  16. }
  17. func FileHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPipeline, storageProvider storage.IStorageProvider, appSettings settings.Settings) {
  18. buckets, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath)
  19. if err != nil {
  20. json.NewEncoder(w).Encode(struct {
  21. Files []File `json:"files"`
  22. }{})
  23. return
  24. }
  25. var files []File
  26. for _, b := range buckets {
  27. bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name())
  28. if err != nil {
  29. writeError(w, 500, "Base path not found")
  30. return
  31. }
  32. for _, f := range bucketFiles {
  33. files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()})
  34. }
  35. }
  36. err = json.NewEncoder(w).Encode(struct {
  37. Files []File `json:"files"`
  38. }{files})
  39. if err != nil {
  40. w.WriteHeader(http.StatusInternalServerError)
  41. }
  42. }