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

package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
)
// Pipelines
const (
Image PipelineType = iota
Video
)
type PipelineType int
type IPipeline interface {
GetName() string
GetSlug() string
GetType() PipelineType
GetSteps() []Step
}
type Pipeline struct {
Name string `json:"name" faker:"name"`
Slug string `json:"slug" faker:"word"`
Type PipelineType `json:"type" faker:"-"`
RemoveMetadata bool `json:"remove_metadata" faker:"-"`
Steps []Step `json:"steps"`
}
func (p Pipeline) GetName() string {
return p.Name
}
func (p Pipeline) GetSlug() string {
return p.Slug
}
func (p Pipeline) GetType() PipelineType {
return p.Type
}
func (p Pipeline) GetSteps() []Step {
return p.Steps
}
type ImagePipeline struct {
Pipeline
}
type VideoPipeline struct {
Pipeline
}
// Steps
type StepType int
const (
TypeResizeImageStep StepType = iota
TypeCompressImageStep
)
type Step struct {
Name string `faker:"name"`
Type StepType `faker:"-"`
}
func (s Step) Translate() (IExecutableStep, error) {
var step IExecutableStep
switch s.GetType() {
case TypeResizeImageStep:
step = ResizeImageStep{s}
case TypeCompressImageStep:
step = CompressImageStep{s}
}
if step == nil {
return nil, errors.New("invalid type")
}
return step, nil
}
func (s Step) GetType() StepType {
return s.Type
}
func (s Step) GetName() string {
return s.Name
}
type IExecutableStep interface {
Execute()
}
// Resize image
type ResizeImageStep struct {
Step
}
func (s ResizeImageStep) Execute() {
// TODO
}
// Compress image
type CompressImageStep struct {
Step
}
func (s CompressImageStep) Execute() {
// TODO
}
// Deserialization
func DeserializePipelines(pipelines [][]byte) []IPipeline {
var values []IPipeline
for _, pipeline := range pipelines {
var deserializedObject Pipeline
err := json.Unmarshal(pipeline, &deserializedObject)
if err != nil {
log.Fatalf("Could not deserialize pipelines config: %s", err)
}
values = append(values, deserializedObject)
}
return values
}
func LoadPipelines() []IPipeline {
var files [][]byte
path, _ := os.Getwd()
err := filepath.Walk(path+"/config", func(path string, info fs.FileInfo, err error) error {
if err == nil && info.IsDir() == false {
fmt.Println(path)
data, _ := os.ReadFile(path)
files = append(files, data)
}
return nil
})
if err != nil {
panic(err)
}
return DeserializePipelines(files)
}