storage.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package helpers
  2. import (
  3. "fmt"
  4. "os"
  5. "git.aetherial.dev/aeth/keiji/pkg/env"
  6. )
  7. type InvalidSkipArg struct {Skip int}
  8. func (i *InvalidSkipArg) Error() string {
  9. return fmt.Sprintf("Invalid skip amount was passed: %s", i.Skip)
  10. }
  11. /*
  12. Function to return the location of the image store. Wrapping the env call in
  13. a function so that refactoring is easier
  14. */
  15. func GetImageStore() string {
  16. return os.Getenv(env.IMAGE_STORE)
  17. }
  18. /*
  19. Return all of the filenames of the images that exist in the imagestore location
  20. :param limit: the limit of filenames to return
  21. :param skip: the index to start getting images from
  22. */
  23. func GetImagePaths(limit int, skip int) ([]string, error) {
  24. f, err := os.ReadDir(GetImageStore())
  25. if err != nil {
  26. return nil, err
  27. }
  28. if len(f) < skip {
  29. return nil, &InvalidSkipArg{Skip: skip}
  30. }
  31. if len(f) < limit {
  32. return nil, &InvalidSkipArg{Skip: limit}
  33. }
  34. fnames := []string{}
  35. for i := skip; i < (skip + limit); i++ {
  36. fnames = append(fnames, fmt.Sprintf("/api/v1/images/%s", f[i].Name()))
  37. }
  38. return fnames, err
  39. }