67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package imageprocessor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"screenmark/screenmark/models"
|
|
"screenmark/screenmark/notifier"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DbImageProcessor struct {
|
|
logger *log.Logger
|
|
images models.ImageModel
|
|
|
|
incomingImages chan string
|
|
|
|
notifier *notifier.Notifier[Notification]
|
|
}
|
|
|
|
func (p *DbImageProcessor) processImage(incomingMsg string) error {
|
|
imageStringUUID := incomingMsg[0:36]
|
|
status := incomingMsg[36:]
|
|
|
|
imageUUID, err := uuid.Parse(imageStringUUID)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing: %w", err)
|
|
}
|
|
|
|
processingImage, err := p.images.GetToProcess(context.Background(), imageUUID)
|
|
if err != nil {
|
|
return fmt.Errorf("get to processes: %w", err)
|
|
}
|
|
|
|
p.logger.Info("Update", "id", imageStringUUID, "status", status)
|
|
|
|
notification := getImageNotification(ImageNotification{
|
|
Type: IMAGE_TYPE,
|
|
ImageID: processingImage.ImageID,
|
|
ImageName: processingImage.Image.ImageName,
|
|
Status: status,
|
|
})
|
|
|
|
return p.notifier.SendAndCreate(processingImage.UserID.String(), notification)
|
|
}
|
|
|
|
func (p *DbImageProcessor) Work() {
|
|
for incomingMsg := range p.incomingImages {
|
|
go func() {
|
|
err := p.processImage(incomingMsg)
|
|
if err != nil {
|
|
p.logger.Error("processing image", "err", err)
|
|
}
|
|
}()
|
|
}
|
|
}
|
|
|
|
func NewImageProcessor(logger *log.Logger, imageModel models.ImageModel, incoming chan string, notifier *notifier.Notifier[Notification]) *DbImageProcessor {
|
|
return &DbImageProcessor{
|
|
logger: logger,
|
|
images: imageModel,
|
|
incomingImages: incoming,
|
|
notifier: notifier,
|
|
}
|
|
}
|