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.

55 lines
1.3 KiB

  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/geplauder/lithium/pipelines"
  5. "github.com/geplauder/lithium/settings"
  6. "github.com/geplauder/lithium/storage"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  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. writeError(w, 500, "Base path not found")
  21. return
  22. }
  23. var files []File
  24. for _, b := range buckets {
  25. if b.IsDir() == false {
  26. continue
  27. }
  28. bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name())
  29. if err != nil {
  30. writeError(w, 500, "Base path not found")
  31. return
  32. }
  33. for _, f := range bucketFiles {
  34. if b.IsDir() == false {
  35. continue
  36. }
  37. files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()})
  38. }
  39. }
  40. err = json.NewEncoder(w).Encode(struct {
  41. Files []File `json:"files"`
  42. }{files})
  43. if err != nil {
  44. w.WriteHeader(http.StatusInternalServerError)
  45. }
  46. }