github.com/cs3org/reva/v2@v2.27.7/pkg/events/postprocessing.go (about)

     1  // Copyright 2018-2022 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package events
    20  
    21  import (
    22  	"encoding/json"
    23  	"time"
    24  
    25  	user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    26  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    27  	types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
    28  )
    29  
    30  type (
    31  	// Postprocessingstep are the available postprocessingsteps
    32  	Postprocessingstep string
    33  
    34  	// PostprocessingOutcome defines the result of the postprocessing
    35  	PostprocessingOutcome string
    36  )
    37  
    38  var (
    39  	// PPStepAntivirus is the step that scans for viruses
    40  	PPStepAntivirus Postprocessingstep = "virusscan"
    41  	// PPStepPolicies is the step the step that enforces policies
    42  	PPStepPolicies Postprocessingstep = "policies"
    43  	// PPStepDelay is the step that processing. Useful for testing or user annoyment
    44  	PPStepDelay Postprocessingstep = "delay"
    45  	// PPStepFinished is the step that signals that postprocessing is finished, but storage provider hasn't acknowledged it yet
    46  	PPStepFinished Postprocessingstep = "finished"
    47  
    48  	// PPOutcomeDelete means that the file and the upload should be deleted
    49  	PPOutcomeDelete PostprocessingOutcome = "delete"
    50  	// PPOutcomeAbort means that the upload is cancelled but the bytes are being kept in the upload folder
    51  	PPOutcomeAbort PostprocessingOutcome = "abort"
    52  	// PPOutcomeContinue means that the upload is moved to its final destination (eventually being marked with pp results)
    53  	PPOutcomeContinue PostprocessingOutcome = "continue"
    54  	// PPOutcomeRetry means that there was a temporary issue and the postprocessing should be retried at a later point in time
    55  	PPOutcomeRetry PostprocessingOutcome = "retry"
    56  )
    57  
    58  // BytesReceived is emitted by the server when it received all bytes of an upload
    59  type BytesReceived struct {
    60  	UploadID          string
    61  	SpaceOwner        *user.UserId
    62  	ExecutingUser     *user.User
    63  	ResourceID        *provider.ResourceId
    64  	Filename          string
    65  	Filesize          uint64
    66  	URL               string
    67  	Timestamp         *types.Timestamp
    68  	ImpersonatingUser *user.User
    69  }
    70  
    71  // Unmarshal to fulfill umarshaller interface
    72  func (BytesReceived) Unmarshal(v []byte) (interface{}, error) {
    73  	e := BytesReceived{}
    74  	err := json.Unmarshal(v, &e)
    75  	return e, err
    76  }
    77  
    78  // StartPostprocessingStep can be issued by the server to start a postprocessing step
    79  type StartPostprocessingStep struct {
    80  	UploadID      string
    81  	URL           string
    82  	ExecutingUser *user.User
    83  	Filename      string
    84  	Filesize      uint64
    85  	Token         string               // for file retrieval in after upload case
    86  	ResourceID    *provider.ResourceId // for file retrieval in after upload case
    87  	RevaToken     string               // for file retrieval in after upload case
    88  
    89  	StepToStart       Postprocessingstep
    90  	Timestamp         *types.Timestamp
    91  	ImpersonatingUser *user.User
    92  }
    93  
    94  // Unmarshal to fulfill umarshaller interface
    95  func (StartPostprocessingStep) Unmarshal(v []byte) (interface{}, error) {
    96  	e := StartPostprocessingStep{}
    97  	err := json.Unmarshal(v, &e)
    98  	return e, err
    99  }
   100  
   101  // PostprocessingStepFinished can be issued by the server when a postprocessing step is finished
   102  type PostprocessingStepFinished struct {
   103  	UploadID      string
   104  	ExecutingUser *user.User
   105  	Filename      string
   106  
   107  	FinishedStep Postprocessingstep    // name of the step
   108  	Result       interface{}           // result information see VirusscanResult for example
   109  	Error        error                 // possible error of the step
   110  	Outcome      PostprocessingOutcome // some services may cause postprocessing to stop
   111  	Timestamp    *types.Timestamp
   112  }
   113  
   114  // Unmarshal to fulfill umarshaller interface
   115  func (PostprocessingStepFinished) Unmarshal(v []byte) (interface{}, error) {
   116  	e := PostprocessingStepFinished{}
   117  	err := json.Unmarshal(v, &e)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	switch e.FinishedStep {
   123  	case PPStepAntivirus:
   124  		var res VirusscanResult
   125  		b, _ := json.Marshal(e.Result)
   126  		err = json.Unmarshal(b, &res)
   127  		e.Result = res
   128  	case PPStepPolicies:
   129  		// nothing to do, but this makes the linter happy
   130  	}
   131  	return e, err
   132  }
   133  
   134  // VirusscanResult is the Result of a PostprocessingStepFinished event from the antivirus
   135  type VirusscanResult struct {
   136  	Infected    bool
   137  	Description string
   138  	Scandate    time.Time
   139  	ResourceID  *provider.ResourceId
   140  	ErrorMsg    string // empty when no error
   141  	Timestamp   *types.Timestamp
   142  }
   143  
   144  // PostprocessingFinished is emitted by *some* service which can decide that
   145  type PostprocessingFinished struct {
   146  	UploadID          string
   147  	Filename          string
   148  	SpaceOwner        *user.UserId
   149  	ExecutingUser     *user.User
   150  	Result            map[Postprocessingstep]interface{} // it is a map[step]Event
   151  	Outcome           PostprocessingOutcome
   152  	Timestamp         *types.Timestamp
   153  	ImpersonatingUser *user.User
   154  }
   155  
   156  // Unmarshal to fulfill umarshaller interface
   157  func (PostprocessingFinished) Unmarshal(v []byte) (interface{}, error) {
   158  	e := PostprocessingFinished{}
   159  	err := json.Unmarshal(v, &e)
   160  	return e, err
   161  }
   162  
   163  // PostprocessingRetry is emitted by *some* service which can decide that
   164  type PostprocessingRetry struct {
   165  	UploadID        string
   166  	Filename        string
   167  	ExecutingUser   *user.User
   168  	Failures        int
   169  	BackoffDuration time.Duration
   170  }
   171  
   172  // Unmarshal to fulfill umarshaller interface
   173  func (PostprocessingRetry) Unmarshal(v []byte) (interface{}, error) {
   174  	e := PostprocessingRetry{}
   175  	err := json.Unmarshal(v, &e)
   176  	return e, err
   177  }
   178  
   179  // UploadReady is emitted by the storage provider when postprocessing is finished
   180  type UploadReady struct {
   181  	UploadID          string
   182  	Filename          string
   183  	SpaceOwner        *user.UserId
   184  	ExecutingUser     *user.User
   185  	ImpersonatingUser *user.User
   186  	FileRef           *provider.Reference
   187  	Timestamp         *types.Timestamp
   188  	Failed            bool
   189  	IsVersion         bool
   190  	// add reference here? We could use it to inform client pp is finished
   191  }
   192  
   193  // Unmarshal to fulfill umarshaller interface
   194  func (UploadReady) Unmarshal(v []byte) (interface{}, error) {
   195  	e := UploadReady{}
   196  	err := json.Unmarshal(v, &e)
   197  	return e, err
   198  }
   199  
   200  // ResumePostprocessing can be emitted to repair broken postprocessing
   201  type ResumePostprocessing struct {
   202  	UploadID  string
   203  	Step      Postprocessingstep
   204  	Timestamp *types.Timestamp
   205  }
   206  
   207  // Unmarshal to fulfill umarshaller interface
   208  func (ResumePostprocessing) Unmarshal(v []byte) (interface{}, error) {
   209  	e := ResumePostprocessing{}
   210  	err := json.Unmarshal(v, &e)
   211  	return e, err
   212  }
   213  
   214  // RestartPostprocessing will be emitted by postprocessing service if it doesn't know about an upload
   215  type RestartPostprocessing struct {
   216  	UploadID  string
   217  	Timestamp *types.Timestamp
   218  }
   219  
   220  // Unmarshal to fulfill umarshaller interface
   221  func (RestartPostprocessing) Unmarshal(v []byte) (interface{}, error) {
   222  	e := RestartPostprocessing{}
   223  	err := json.Unmarshal(v, &e)
   224  	return e, err
   225  }