Files
ocis/services/postprocessing/pkg/postprocessing/postprocessing.go
T

109 lines
2.9 KiB
Go
Raw Normal View History

2022-12-08 11:00:42 +01:00
package postprocessing
import (
"time"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
)
// Postprocessing handles postprocessing of a file
type Postprocessing struct {
2023-05-11 13:05:49 +02:00
ID string
URL string
User *user.User
Filename string
Filesize uint64
ResourceID *provider.ResourceId
Steps []events.Postprocessingstep
2023-06-12 12:01:00 +02:00
Status Status
2023-05-11 13:05:49 +02:00
PPDelay time.Duration
2022-12-08 11:00:42 +01:00
}
2023-06-12 12:01:00 +02:00
// Status is helper struct to show current postprocessing status
type Status struct {
CurrentStep events.Postprocessingstep
Outcome events.PostprocessingOutcome
}
2022-12-08 11:00:42 +01:00
// New returns a new postprocessing instance
2023-01-25 12:31:47 +01:00
func New(uploadID string, uploadURL string, user *user.User, filename string, filesize uint64, resourceID *provider.ResourceId, steps []events.Postprocessingstep, delay time.Duration) *Postprocessing {
2022-12-08 11:00:42 +01:00
return &Postprocessing{
2023-05-11 13:05:49 +02:00
ID: uploadID,
URL: uploadURL,
User: user,
Filename: filename,
Filesize: filesize,
ResourceID: resourceID,
Steps: steps,
PPDelay: delay,
2022-12-08 11:00:42 +01:00
}
}
// Init is the first step of the postprocessing
func (pp *Postprocessing) Init(ev events.BytesReceived) interface{} {
2023-05-11 13:05:49 +02:00
if len(pp.Steps) == 0 {
2022-12-08 11:00:42 +01:00
return pp.finished(events.PPOutcomeContinue)
}
2023-06-12 12:01:00 +02:00
return pp.step(pp.Steps[0])
2022-12-08 11:00:42 +01:00
}
2023-01-26 10:25:19 +01:00
// NextStep returns the next postprocessing step
func (pp *Postprocessing) NextStep(ev events.PostprocessingStepFinished) interface{} {
2022-12-08 11:00:42 +01:00
switch ev.Outcome {
case events.PPOutcomeContinue:
2023-01-26 10:25:19 +01:00
return pp.next(ev.FinishedStep)
2022-12-08 11:00:42 +01:00
default:
return pp.finished(ev.Outcome)
}
}
2023-06-12 12:01:00 +02:00
// CurrentStep returns the current postprocessing step
func (pp *Postprocessing) CurrentStep() interface{} {
if pp.Status.Outcome != "" {
return pp.finished(pp.Status.Outcome)
}
return pp.step(pp.Status.CurrentStep)
}
2022-12-08 11:00:42 +01:00
// Delay will sleep the configured time then continue
func (pp *Postprocessing) Delay(ev events.StartPostprocessingStep) interface{} {
2023-05-11 13:05:49 +02:00
time.Sleep(pp.PPDelay)
2022-12-08 11:00:42 +01:00
return pp.next(events.PPStepDelay)
}
func (pp *Postprocessing) next(current events.Postprocessingstep) interface{} {
2023-05-11 13:05:49 +02:00
l := len(pp.Steps)
for i, s := range pp.Steps {
2022-12-08 11:00:42 +01:00
if s == current && i+1 < l {
2023-06-12 12:01:00 +02:00
return pp.step(pp.Steps[i+1])
2022-12-08 11:00:42 +01:00
}
}
return pp.finished(events.PPOutcomeContinue)
}
2023-06-12 12:01:00 +02:00
func (pp *Postprocessing) step(next events.Postprocessingstep) events.StartPostprocessingStep {
pp.Status.CurrentStep = next
2022-12-08 11:00:42 +01:00
return events.StartPostprocessingStep{
2023-05-11 13:05:49 +02:00
UploadID: pp.ID,
URL: pp.URL,
ExecutingUser: pp.User,
Filename: pp.Filename,
Filesize: pp.Filesize,
ResourceID: pp.ResourceID,
2022-12-08 11:00:42 +01:00
StepToStart: next,
}
}
func (pp *Postprocessing) finished(outcome events.PostprocessingOutcome) events.PostprocessingFinished {
2023-06-12 12:01:00 +02:00
pp.Status.Outcome = outcome
2022-12-08 11:00:42 +01:00
return events.PostprocessingFinished{
2023-05-11 13:05:49 +02:00
UploadID: pp.ID,
ExecutingUser: pp.User,
Filename: pp.Filename,
2022-12-08 11:00:42 +01:00
Outcome: outcome,
}
}