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.

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