storage.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package helpers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "time"
  7. "git.aetherial.dev/aeth/keiji/pkg/env"
  8. "github.com/google/uuid"
  9. "github.com/redis/go-redis/v9"
  10. )
  11. type InvalidSkipArg struct {Skip int}
  12. func (i *InvalidSkipArg) Error() string {
  13. return fmt.Sprintf("Invalid skip amount was passed: %v", i.Skip)
  14. }
  15. type ImageStoreItem struct {
  16. Identifier string `json:"identifier"`
  17. Filename string `json:"filename"`
  18. AbsolutePath string `json:"absolute_path"`
  19. Title string `json:"title" form:"title"`
  20. Created string `json:"created"`
  21. Desc string `json:"description" form:"description"`
  22. Category string `json:"category"`
  23. ApiPath string
  24. }
  25. /*
  26. Create a new ImageStoreItem
  27. :param fname: the name of the file to be saved
  28. :param title: the canonical title to give the image
  29. :param desc: the description to associate to the image
  30. */
  31. func NewImageStoreItem(fname string, title string, desc string) *ImageStoreItem {
  32. id := uuid.New()
  33. img := ImageStoreItem{
  34. Identifier: id.String(),
  35. Filename: fname,
  36. Title: title,
  37. Category: DIGITAL_ART,
  38. AbsolutePath: fmt.Sprintf("%s/%s", GetImageStore(), fname),
  39. Created: time.Now().UTC().String(),
  40. Desc: desc,
  41. }
  42. return &img
  43. }
  44. /*
  45. Function to return the location of the image store. Wrapping the env call in
  46. a function so that refactoring is easier
  47. */
  48. func GetImageStore() string {
  49. return os.Getenv(env.IMAGE_STORE)
  50. }
  51. /*
  52. Return database entries of the images that exist in the imagestore
  53. :param rds: pointer to a RedisCaller to perform the lookups with
  54. */
  55. func GetImageData(rds *RedisCaller) ([]*ImageStoreItem, error) {
  56. ids, err := rds.GetByCategory(DIGITAL_ART)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var imageEntries []*ImageStoreItem
  61. for i := range ids {
  62. val, err := rds.Client.Get(rds.ctx, ids[i]).Result()
  63. if err == redis.Nil {
  64. return nil, err
  65. } else if err != nil {
  66. return nil, err
  67. }
  68. data := []byte(val)
  69. var imageEntry ImageStoreItem
  70. err = json.Unmarshal(data, &imageEntry)
  71. if err != nil {
  72. return nil, err
  73. }
  74. imageEntry.ApiPath = fmt.Sprintf("/api/v1/images/%s", imageEntry.Filename)
  75. imageEntries = append(imageEntries, &imageEntry)
  76. }
  77. return imageEntries, err
  78. }