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.

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