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.

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