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.

117 lines
1.7 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/fs"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. )
  10. // Pipelines
  11. const (
  12. Image PipelineType = iota
  13. Video
  14. )
  15. type PipelineType int
  16. type IPipeline interface {
  17. GetName() string
  18. GetType() PipelineType
  19. GetSteps() []Step
  20. }
  21. type Pipeline struct {
  22. Name string `json:"name"`
  23. Type PipelineType `json:"type"`
  24. RemoveMetadata bool `json:"remove_metadata"`
  25. Steps []Step `json:"steps"`
  26. }
  27. func (p Pipeline) GetName() string {
  28. return p.Name
  29. }
  30. func (p Pipeline) GetType() PipelineType {
  31. return p.Type
  32. }
  33. func (p Pipeline) GetSteps() []Step {
  34. return p.Steps
  35. }
  36. type ImagePipeline struct {
  37. Pipeline
  38. }
  39. type VideoPipeline struct {
  40. Pipeline
  41. }
  42. // Steps
  43. type IStep interface {
  44. Run()
  45. GetName() string
  46. }
  47. type Step struct {
  48. Name string
  49. Type int
  50. }
  51. func (s Step) Run() {}
  52. func (s Step) GetName() string {
  53. return s.Name
  54. }
  55. type ResizeImageStep struct {
  56. Step
  57. }
  58. type CompressImageStep struct {
  59. Step
  60. }
  61. // Deserialization
  62. func DeserializePipelines(pipelines [][]byte) []IPipeline {
  63. var values []IPipeline
  64. for _, pipeline := range pipelines {
  65. var deserializedObject Pipeline
  66. err := json.Unmarshal(pipeline, &deserializedObject)
  67. if err != nil {
  68. log.Fatalf("Could not deserialize pipelines config: %s", err)
  69. }
  70. values = append(values, deserializedObject)
  71. }
  72. return values
  73. }
  74. func LoadPipelines() []IPipeline {
  75. var files [][]byte
  76. path, _ := os.Getwd()
  77. err := filepath.Walk(path+"/pipelines", func(path string, info fs.FileInfo, err error) error {
  78. if err == nil && info.IsDir() == false {
  79. fmt.Println(path)
  80. data, _ := os.ReadFile(path)
  81. files = append(files, data)
  82. }
  83. return nil
  84. })
  85. if err != nil {
  86. panic(err)
  87. }
  88. return DeserializePipelines(files)
  89. }