Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File events #2639

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/file-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: File Events

Adds file based events. See `pkg/events/files.go` for full list

https://github.com/cs3org/reva/pull/2639
58 changes: 58 additions & 0 deletions internal/grpc/interceptors/eventsmiddleware/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package eventsmiddleware
import (
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
)

Expand Down Expand Up @@ -141,3 +142,60 @@ func LinkRemoved(r *link.RemovePublicShareResponse, req *link.RemovePublicShareR
ShareToken: req.Ref.GetToken(),
}
}

// FileUploaded converts the response to an event
func FileUploaded(r *provider.InitiateFileUploadResponse, req *provider.InitiateFileUploadRequest) events.FileUploaded {
return events.FileUploaded{
FileID: req.Ref,
}
}

// FileDownloaded converts the response to an event
func FileDownloaded(r *provider.InitiateFileDownloadResponse, req *provider.InitiateFileDownloadRequest) events.FileDownloaded {
return events.FileDownloaded{
FileID: req.Ref,
}
}

// ItemTrashed converts the response to an event
func ItemTrashed(r *provider.DeleteResponse, req *provider.DeleteRequest) events.ItemTrashed {
return events.ItemTrashed{
FileID: req.Ref,
}
}

// ItemMoved converts the response to an event
func ItemMoved(r *provider.MoveResponse, req *provider.MoveRequest) events.ItemMoved {
return events.ItemMoved{
FileID: req.Destination,
OldReference: req.Source,
}
}

// ItemPurged converts the response to an event
func ItemPurged(r *provider.PurgeRecycleResponse, req *provider.PurgeRecycleRequest) events.ItemPurged {
return events.ItemPurged{
FileID: req.Ref,
}
}

// ItemRestored converts the response to an event
func ItemRestored(r *provider.RestoreRecycleItemResponse, req *provider.RestoreRecycleItemRequest) events.ItemRestored {
ref := req.Ref
if req.RestoreRef != nil {
ref = req.RestoreRef
}
return events.ItemRestored{
FileID: ref,
OldReference: req.Ref,
Key: req.Key,
}
}

// FileVersionRestored converts the response to an event
func FileVersionRestored(r *provider.RestoreFileVersionResponse, req *provider.RestoreFileVersionRequest) events.FileVersionRestored {
return events.FileVersionRestored{
FileID: req.Ref,
Key: req.Key,
}
}
29 changes: 29 additions & 0 deletions internal/grpc/interceptors/eventsmiddleware/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
v1beta12 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/server"
"github.com/cs3org/reva/v2/pkg/rgrpc"
Expand Down Expand Up @@ -94,6 +95,34 @@ func NewUnary(m map[string]interface{}) (grpc.UnaryServerInterceptor, int, error
} else {
ev = LinkAccessFailed(v, req.(*link.GetPublicShareByTokenRequest))
}
case *provider.InitiateFileUploadResponse:
if isSuccess(v) {
ev = FileUploaded(v, req.(*provider.InitiateFileUploadRequest))
}
case *provider.InitiateFileDownloadResponse:
if isSuccess(v) {
ev = FileDownloaded(v, req.(*provider.InitiateFileDownloadRequest))
}
case *provider.DeleteResponse:
if isSuccess(v) {
ev = ItemTrashed(v, req.(*provider.DeleteRequest))
}
case *provider.MoveResponse:
if isSuccess(v) {
ev = ItemMoved(v, req.(*provider.MoveRequest))
}
case *provider.PurgeRecycleResponse:
if isSuccess(v) {
ev = ItemPurged(v, req.(*provider.PurgeRecycleRequest))
}
case *provider.RestoreRecycleItemResponse:
if isSuccess(v) {
ev = ItemRestored(v, req.(*provider.RestoreRecycleItemRequest))
}
case *provider.RestoreFileVersionResponse:
if isSuccess(v) {
ev = FileVersionRestored(v, req.(*provider.RestoreFileVersionRequest))
}
}

if ev != nil {
Expand Down
121 changes: 121 additions & 0 deletions pkg/events/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2018-2022 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package events

import (
"encoding/json"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)

// FileUploaded is emitted when a file is uploaded
type FileUploaded struct {
FileID *provider.Reference
Owner *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (FileUploaded) Unmarshal(v []byte) (interface{}, error) {
e := FileUploaded{}
err := json.Unmarshal(v, &e)
return e, err
}

// FileDownloaded is emitted when a file is downloaded
type FileDownloaded struct {
FileID *provider.Reference
Owner *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (FileDownloaded) Unmarshal(v []byte) (interface{}, error) {
e := FileDownloaded{}
err := json.Unmarshal(v, &e)
return e, err
}

// ItemTrashed is emitted when a file or folder is trashed
type ItemTrashed struct {
FileID *provider.Reference
Owner *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (ItemTrashed) Unmarshal(v []byte) (interface{}, error) {
e := ItemTrashed{}
err := json.Unmarshal(v, &e)
return e, err
}

// ItemMoved is emitted when a file or folder is moved
type ItemMoved struct {
FileID *provider.Reference
Owner *user.UserId
OldReference *provider.Reference
}

// Unmarshal to fulfill umarshaller interface
func (ItemMoved) Unmarshal(v []byte) (interface{}, error) {
e := ItemMoved{}
err := json.Unmarshal(v, &e)
return e, err
}

// ItemPurged is emitted when a file or folder is removed from trashbin
type ItemPurged struct {
FileID *provider.Reference
Owner *user.UserId
}

// Unmarshal to fulfill umarshaller interface
func (ItemPurged) Unmarshal(v []byte) (interface{}, error) {
e := ItemPurged{}
err := json.Unmarshal(v, &e)
return e, err
}

// ItemRestored is emitted when a file or folder is restored from trashbin
type ItemRestored struct {
FileID *provider.Reference
Owner *user.UserId
OldReference *provider.Reference
Key string
}

// Unmarshal to fulfill umarshaller interface
func (ItemRestored) Unmarshal(v []byte) (interface{}, error) {
e := ItemRestored{}
err := json.Unmarshal(v, &e)
return e, err
}

// FileVersionRestored is emitted when a file version is restored
type FileVersionRestored struct {
FileID *provider.Reference
Owner *user.UserId
Key string
}

// Unmarshal to fulfill umarshaller interface
func (FileVersionRestored) Unmarshal(v []byte) (interface{}, error) {
e := FileVersionRestored{}
err := json.Unmarshal(v, &e)
return e, err
}
File renamed without changes.