diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go index c451b8b9c0..aa50d8e6c0 100644 --- a/server/cmd/museum/main.go +++ b/server/cmd/museum/main.go @@ -5,6 +5,7 @@ import ( "database/sql" b64 "encoding/base64" "fmt" + "github.com/ente-io/museum/pkg/controller/file_copy" "net/http" "os" "os/signal" @@ -389,9 +390,11 @@ func main() { timeout.WithHandler(healthCheckHandler.PingDBStats), timeout.WithResponse(timeOutResponse), )) + fileCopyCtrl := &file_copy.FileCopyController{FileController: fileController, CollectionCtrl: collectionController, S3Config: s3Config} fileHandler := &api.FileHandler{ - Controller: fileController, + Controller: fileController, + FileCopyCtrl: fileCopyCtrl, } privateAPI.GET("/files/upload-urls", fileHandler.GetUploadURLs) privateAPI.GET("/files/multipart-upload-urls", fileHandler.GetMultipartUploadURLs) @@ -400,6 +403,7 @@ func main() { privateAPI.GET("/files/preview/:fileID", fileHandler.GetThumbnail) privateAPI.GET("/files/preview/v2/:fileID", fileHandler.GetThumbnail) privateAPI.POST("/files", fileHandler.CreateOrUpdate) + privateAPI.POST("/files/copy", fileHandler.CopyFiles) privateAPI.PUT("/files/update", fileHandler.Update) privateAPI.POST("/files/trash", fileHandler.Trash) privateAPI.POST("/files/size", fileHandler.GetSize) diff --git a/server/pkg/api/file.go b/server/pkg/api/file.go index a65b9e3833..6bda9ab31a 100644 --- a/server/pkg/api/file.go +++ b/server/pkg/api/file.go @@ -1,6 +1,7 @@ package api import ( + "github.com/ente-io/museum/pkg/controller/file_copy" "net/http" "os" "strconv" @@ -20,7 +21,8 @@ import ( // FileHandler exposes request handlers for all encrypted file related requests type FileHandler struct { - Controller *controller.FileController + Controller *controller.FileController + FileCopyCtrl *file_copy.FileCopyController } // DefaultMaxBatchSize is the default maximum API batch size unless specified otherwise @@ -58,6 +60,21 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) { c.JSON(http.StatusOK, response) } +// CopyFiles copies files that are owned by another user +func (h *FileHandler) CopyFiles(c *gin.Context) { + var req ente.CopyFileSyncRequest + if err := c.ShouldBindJSON(&req); err != nil { + handler.Error(c, stacktrace.Propagate(err, "")) + return + } + response, err := h.FileCopyCtrl.CopyFiles(c, req) + if err != nil { + handler.Error(c, stacktrace.Propagate(err, "")) + return + } + c.JSON(http.StatusOK, response) +} + // Update updates already existing file func (h *FileHandler) Update(c *gin.Context) { enteApp := auth.GetApp(c) diff --git a/server/pkg/controller/collection.go b/server/pkg/controller/collection.go index 455e240194..cdfbb40808 100644 --- a/server/pkg/controller/collection.go +++ b/server/pkg/controller/collection.go @@ -464,7 +464,7 @@ func (c *CollectionController) isRemoveAllowed(ctx *gin.Context, actorUserID int return nil } -func (c *CollectionController) isCopyAllowed(ctx *gin.Context, actorUserID int64, req ente.CopyFileSyncRequest) error { +func (c *CollectionController) IsCopyAllowed(ctx *gin.Context, actorUserID int64, req ente.CopyFileSyncRequest) error { // verify that srcCollectionID is accessible by actorUserID if _, err := c.AccessCtrl.GetCollection(ctx, &access.GetCollectionParams{ CollectionID: req.SrcCollectionID, diff --git a/server/pkg/controller/file_copy/file_copy.go b/server/pkg/controller/file_copy/file_copy.go new file mode 100644 index 0000000000..6f581c5865 --- /dev/null +++ b/server/pkg/controller/file_copy/file_copy.go @@ -0,0 +1,25 @@ +package file_copy + +import ( + "github.com/ente-io/museum/ente" + "github.com/ente-io/museum/pkg/controller" + "github.com/ente-io/museum/pkg/utils/auth" + "github.com/ente-io/museum/pkg/utils/s3config" + "github.com/gin-gonic/gin" +) + +type FileCopyController struct { + S3Config *s3config.S3Config + FileController *controller.FileController + CollectionCtrl *controller.CollectionController +} + +func (fc *FileCopyController) CopyFiles(c *gin.Context, req ente.CopyFileSyncRequest) (interface{}, error) { + userID := auth.GetUserID(c.Request.Header) + err := fc.CollectionCtrl.IsCopyAllowed(c, userID, req) + if err != nil { + return nil, err + } + return nil, ente.NewInternalError("yet to implement actual copy") + +}