storage.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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" form:"title"`
  18. Created string `json:"created"`
  19. Desc string `json:"description" form:"description"`
  20. Category string `json:"category"`
  21. }
  22. /*
  23. Create a new ImageStoreItem
  24. :param fname: the name of the file to be saved
  25. :param title: the canonical title to give the image
  26. :param desc: the description to associate to the image
  27. */
  28. func NewImageStoreItem(fname string, title string, desc string) *ImageStoreItem {
  29. id := uuid.New()
  30. img := ImageStoreItem{
  31. Identifier: id.String(),
  32. Filename: fname,
  33. Title: title,
  34. Category: DIGITAL_ART,
  35. AbsolutePath: fmt.Sprintf("%s/%s", GetImageStore(), fname),
  36. Created: time.Now().UTC().String(),
  37. Desc: desc,
  38. }
  39. return &img
  40. }
  41. /*
  42. Function to return the location of the image store. Wrapping the env call in
  43. a function so that refactoring is easier
  44. */
  45. func GetImageStore() string {
  46. return os.Getenv(env.IMAGE_STORE)
  47. }
  48. /*
  49. Return all of the filenames of the images that exist in the imagestore location
  50. :param limit: the limit of filenames to return
  51. :param skip: the index to start getting images from
  52. */
  53. func GetImagePaths(limit int, skip int) ([]string, error) {
  54. f, err := os.ReadDir(GetImageStore())
  55. if err != nil {
  56. return nil, err
  57. }
  58. if len(f) < skip {
  59. return nil, &InvalidSkipArg{Skip: skip}
  60. }
  61. if len(f) < limit {
  62. return nil, &InvalidSkipArg{Skip: limit}
  63. }
  64. fnames := []string{}
  65. for i := skip; i < (skip + limit); i++ {
  66. fnames = append(fnames, fmt.Sprintf("/api/v1/images/%s", f[i].Name()))
  67. }
  68. return fnames, err
  69. }