storage.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package helpers
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "git.aetherial.dev/aeth/keiji/pkg/env"
  7. "github.com/google/uuid"
  8. )
  9. type InvalidSkipArg struct {Skip int}
  10. func (i *InvalidSkipArg) Error() string {
  11. return fmt.Sprintf("Invalid skip amount was passed: %v", i.Skip)
  12. }
  13. type ImageStoreItem struct {
  14. Identifier string `json:"identifier"`
  15. Filename string `json:"filename"`
  16. AbsolutePath string `json:"absolute_path"`
  17. Title string `json:"title"`
  18. Created string `json:"created"`
  19. Desc string `json:"description"`
  20. Category string `json:"category"`
  21. }
  22. func NewImageStoreItem(fname string, title string, desc string) *ImageStoreItem {
  23. id := uuid.New()
  24. img := ImageStoreItem{
  25. Identifier: id.String(),
  26. Filename: fname,
  27. Title: title,
  28. Category: DIGITAL_ART,
  29. AbsolutePath: fmt.Sprintf("%s/%s", env.IMAGE_STORE, fname),
  30. Created: time.Now().UTC().String(),
  31. Desc: desc,
  32. }
  33. return &img
  34. }
  35. /*
  36. Function to return the location of the image store. Wrapping the env call in
  37. a function so that refactoring is easier
  38. */
  39. func GetImageStore() string {
  40. return os.Getenv(env.IMAGE_STORE)
  41. }
  42. /*
  43. Return all of the filenames of the images that exist in the imagestore location
  44. :param limit: the limit of filenames to return
  45. :param skip: the index to start getting images from
  46. */
  47. func GetImagePaths(limit int, skip int) ([]string, error) {
  48. f, err := os.ReadDir(GetImageStore())
  49. if err != nil {
  50. return nil, err
  51. }
  52. if len(f) < skip {
  53. return nil, &InvalidSkipArg{Skip: skip}
  54. }
  55. if len(f) < limit {
  56. return nil, &InvalidSkipArg{Skip: limit}
  57. }
  58. fnames := []string{}
  59. for i := skip; i < (skip + limit); i++ {
  60. fnames = append(fnames, fmt.Sprintf("/api/v1/images/%s", f[i].Name()))
  61. }
  62. return fnames, err
  63. }