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.

160 lines
2.5 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/fs"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. )
  11. // Pipelines
  12. const (
  13. Image PipelineType = iota
  14. Video
  15. )
  16. type PipelineType int
  17. type IPipeline interface {
  18. GetName() string
  19. GetSlug() string
  20. GetType() PipelineType
  21. GetSteps() []Step
  22. }
  23. type Pipeline struct {
  24. Name string `json:"name" faker:"name"`
  25. Slug string `json:"slug" faker:"word"`
  26. Type PipelineType `json:"type" faker:"-"`
  27. RemoveMetadata bool `json:"remove_metadata" faker:"-"`
  28. Steps []Step `json:"steps"`
  29. }
  30. func (p Pipeline) GetName() string {
  31. return p.Name
  32. }
  33. func (p Pipeline) GetSlug() string {
  34. return p.Slug
  35. }
  36. func (p Pipeline) GetType() PipelineType {
  37. return p.Type
  38. }
  39. func (p Pipeline) GetSteps() []Step {
  40. return p.Steps
  41. }
  42. type ImagePipeline struct {
  43. Pipeline
  44. }
  45. type VideoPipeline struct {
  46. Pipeline
  47. }
  48. // Steps
  49. type StepType int
  50. const (
  51. TypeResizeImageStep StepType = iota
  52. TypeCompressImageStep
  53. )
  54. type Step struct {
  55. Name string `faker:"name"`
  56. Type StepType `faker:"-"`
  57. }
  58. func (s Step) Translate() (IExecutableStep, error) {
  59. var step IExecutableStep
  60. switch s.GetType() {
  61. case TypeResizeImageStep:
  62. step = ResizeImageStep{s}
  63. case TypeCompressImageStep:
  64. step = CompressImageStep{s}
  65. }
  66. if step == nil {
  67. return nil, errors.New("invalid type")
  68. }
  69. return step, nil
  70. }
  71. func (s Step) GetType() StepType {
  72. return s.Type
  73. }
  74. func (s Step) GetName() string {
  75. return s.Name
  76. }
  77. type IExecutableStep interface {
  78. Execute()
  79. }
  80. // Resize image
  81. type ResizeImageStep struct {
  82. Step
  83. }
  84. func (s ResizeImageStep) Execute() {
  85. // TODO
  86. }
  87. // Compress image
  88. type CompressImageStep struct {
  89. Step
  90. }
  91. func (s CompressImageStep) Execute() {
  92. // TODO
  93. }
  94. // Deserialization
  95. func DeserializePipelines(pipelines [][]byte) []IPipeline {
  96. var values []IPipeline
  97. for _, pipeline := range pipelines {
  98. var deserializedObject Pipeline
  99. err := json.Unmarshal(pipeline, &deserializedObject)
  100. if err != nil {
  101. log.Fatalf("Could not deserialize pipelines config: %s", err)
  102. }
  103. values = append(values, deserializedObject)
  104. }
  105. return values
  106. }
  107. func LoadPipelines() []IPipeline {
  108. var files [][]byte
  109. path, _ := os.Getwd()
  110. err := filepath.Walk(path+"/config", func(path string, info fs.FileInfo, err error) error {
  111. if err == nil && info.IsDir() == false {
  112. fmt.Println(path)
  113. data, _ := os.ReadFile(path)
  114. files = append(files, data)
  115. }
  116. return nil
  117. })
  118. if err != nil {
  119. panic(err)
  120. }
  121. return DeserializePipelines(files)
  122. }