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.

58 lines
1.3 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. if b.IsDir() == false {
  28. continue
  29. }
  30. bucketFiles, err := ioutil.ReadDir("./files/" + appSettings.StorageProvider.BasePath + "/" + b.Name())
  31. if err != nil {
  32. writeError(w, 500, "Base path not found")
  33. return
  34. }
  35. for _, f := range bucketFiles {
  36. if b.IsDir() == false {
  37. continue
  38. }
  39. files = append(files, File{b.Name(), f.Name(), int(f.Size()), f.ModTime()})
  40. }
  41. }
  42. err = json.NewEncoder(w).Encode(struct {
  43. Files []File `json:"files"`
  44. }{files})
  45. if err != nil {
  46. w.WriteHeader(http.StatusInternalServerError)
  47. }
  48. }