π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[server] add generic attachment blob endpoints
Neeraj Gupta28e797b283ba
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.goindex 4709334968..de55c4b689 100644--- a/server/cmd/museum/main.go+++ b/server/cmd/museum/main.go@@ -951,6 +951,11 @@ func main() {privateAPI.GET("/contacts/diff", contactHandler.GetDiff)privateAPI.PUT("/contacts/:id", contactHandler.Update)privateAPI.DELETE("/contacts/:id", contactHandler.Delete)+ privateAPI.POST("/attachments/:type/upload-url", contactHandler.GetAttachmentUploadURL)+ privateAPI.GET("/attachments/:type/:attachmentID", contactHandler.GetAttachment)+ privateAPI.PUT("/contacts/:id/attachments/:type", contactHandler.AttachContactAttachment)+ privateAPI.GET("/contacts/:id/attachments/:type", contactHandler.GetCurrentContactAttachment)+ privateAPI.DELETE("/contacts/:id/attachments/:type", contactHandler.DeleteContactAttachment)privateAPI.POST("/contacts/:id/profile-picture/upload-url", contactHandler.GetProfilePictureUploadURL)privateAPI.PUT("/contacts/:id/profile-picture", contactHandler.AttachProfilePicture)privateAPI.GET("/contacts/:id/profile-picture", contactHandler.GetProfilePicture)diff --git a/server/ente/contact/contact.go b/server/ente/contact/contact.goindex ebbe0c1400..f5feb0ad87 100644--- a/server/ente/contact/contact.go+++ b/server/ente/contact/contact.go@@ -19,6 +19,20 @@ const (MaxProfilePictureEncryptedSize int64 = 512 * 1024)+type AttachmentPolicy struct {+ Type AttachmentType+ SlotColumn string+ MaxEncryptedSize int64+}++var attachmentPolicies = map[AttachmentType]AttachmentPolicy{+ ProfilePicture: {+ Type: ProfilePicture,+ SlotColumn: "profile_picture_attachment_id",+ MaxEncryptedSize: MaxProfilePictureEncryptedSize,+ },+}+type Entity struct {ID string `json:"id" binding:"required"`UserID int64 `json:"userID" binding:"required"`@@ -71,18 +85,18 @@ type DiffRequest struct {Limit int16 `form:"limit" binding:"required"`}-type ProfilePictureUploadURLRequest struct {+type AttachmentUploadURLRequest struct {ContentLength int64 `json:"contentLength" binding:"required"`ContentMD5 string `json:"contentMD5" binding:"required"`}-func (r ProfilePictureUploadURLRequest) Validate() error {+func (r AttachmentUploadURLRequest) Validate(policy AttachmentPolicy) error {if r.ContentLength <= 0 {return ente.NewBadRequestWithMessage("contentLength must be greater than 0")}- if r.ContentLength > MaxProfilePictureEncryptedSize {+ if r.ContentLength > policy.MaxEncryptedSize {return ente.NewBadRequestWithMessage(- fmt.Sprintf("profile picture must be <= %d bytes", MaxProfilePictureEncryptedSize),+ fmt.Sprintf("%s must be <= %d bytes", policy.Type, policy.MaxEncryptedSize),)}if strings.TrimSpace(r.ContentMD5) == "" {@@ -91,26 +105,26 @@ func (r ProfilePictureUploadURLRequest) Validate() error {return nil}-type ProfilePictureUploadURL struct {+type AttachmentUploadURL struct {AttachmentID string `json:"attachmentID" binding:"required"`URL string `json:"url" binding:"required"`}-type CommitProfilePictureRequest struct {+type CommitAttachmentRequest struct {AttachmentID string `json:"attachmentID" binding:"required"`Size int64 `json:"size" binding:"required"`}-func (r CommitProfilePictureRequest) Validate() error {+func (r CommitAttachmentRequest) Validate(policy AttachmentPolicy) error {if !IsValidAttachmentID(r.AttachmentID) {return ente.NewBadRequestWithMessage("invalid attachmentID")}if r.Size <= 0 {return ente.NewBadRequestWithMessage("size must be greater than 0")}- if r.Size > MaxProfilePictureEncryptedSize {+ if r.Size > policy.MaxEncryptedSize {return ente.NewBadRequestWithMessage(- fmt.Sprintf("profile picture must be <= %d bytes", MaxProfilePictureEncryptedSize),+ fmt.Sprintf("%s must be <= %d bytes", policy.Type, policy.MaxEncryptedSize),)}return nil@@ -141,13 +155,30 @@ func (a Attachment) ObjectKey() string {}func (at AttachmentType) IsValid() error {- switch at {- case ProfilePicture:+ if _, ok := attachmentPolicies[at]; ok {return nil}return ente.NewBadRequestWithMessage(fmt.Sprintf("invalid attachment type: %s", at))}+func (at AttachmentType) Policy() (AttachmentPolicy, error) {+ policy, ok := attachmentPolicies[at]+ if !ok {+ return AttachmentPolicy{}, ente.NewBadRequestWithMessage(+ fmt.Sprintf("invalid attachment type: %s", at),+ )+ }+ return policy, nil+}++func ParseAttachmentType(raw string) (AttachmentType, error) {+ attachmentType := AttachmentType(raw)+ if err := attachmentType.IsValid(); err != nil {+ return "", err+ }+ return attachmentType, nil+}+func NewContactID() string {return base.MustNewID(contactIDPrefix)}diff --git a/server/pkg/api/contact.go b/server/pkg/api/contact.goindex 4ef9662f2c..02ac548e8f 100644--- a/server/pkg/api/contact.go+++ b/server/pkg/api/contact.go@@ -75,36 +75,62 @@ func (h *ContactHandler) Delete(c *gin.Context) {c.Status(http.StatusOK)}-func (h *ContactHandler) GetProfilePictureUploadURL(c *gin.Context) {- var request contactmodel.ProfilePictureUploadURLRequest+func (h *ContactHandler) GetAttachmentUploadURL(c *gin.Context) {+ var request contactmodel.AttachmentUploadURLRequestif err := c.ShouldBindJSON(&request); err != nil {handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("request binding failed %s", err)))return}- resp, err := h.Controller.GetProfilePictureUploadURL(c, c.Param("id"), request)+ resp, err := h.Controller.GetAttachmentUploadURL(c, c.Param("type"), request)if err != nil {- handler.Error(c, stacktrace.Propagate(err, "failed to create profile picture upload url"))+ handler.Error(c, stacktrace.Propagate(err, "failed to create attachment upload url"))return}c.JSON(http.StatusOK, resp)}-func (h *ContactHandler) AttachProfilePicture(c *gin.Context) {- var request contactmodel.CommitProfilePictureRequest+func (h *ContactHandler) GetProfilePictureUploadURL(c *gin.Context) {+ c.Params = append(c.Params, gin.Param{Key: "type", Value: string(contactmodel.ProfilePicture)})+ h.GetAttachmentUploadURL(c)+}++func (h *ContactHandler) AttachContactAttachment(c *gin.Context) {+ var request contactmodel.CommitAttachmentRequestif err := c.ShouldBindJSON(&request); err != nil {handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("request binding failed %s", err)))return}- resp, err := h.Controller.AttachProfilePicture(c, c.Param("id"), request)+ resp, err := h.Controller.AttachContactAttachment(c, c.Param("id"), c.Param("type"), request)+ if err != nil {+ handler.Error(c, stacktrace.Propagate(err, "failed to attach contact attachment"))+ return+ }+ c.JSON(http.StatusOK, resp)+}++func (h *ContactHandler) AttachProfilePicture(c *gin.Context) {+ c.Params = append(c.Params, gin.Param{Key: "type", Value: string(contactmodel.ProfilePicture)})+ h.AttachContactAttachment(c)+}++func (h *ContactHandler) GetAttachment(c *gin.Context) {+ resp, err := h.Controller.GetAttachmentURL(c, c.Param("type"), c.Param("attachmentID"))if err != nil {- handler.Error(c, stacktrace.Propagate(err, "failed to attach profile picture"))+ handler.Error(c, stacktrace.Propagate(err, "failed to get attachment url"))return}c.JSON(http.StatusOK, resp)}func (h *ContactHandler) GetProfilePicture(c *gin.Context) {- resp, err := h.Controller.GetProfilePictureURL(c, c.Param("id"))+ attachmentID := c.Query("attachmentID")+ if attachmentID != "" {+ c.Params = append(c.Params, gin.Param{Key: "type", Value: string(contactmodel.ProfilePicture)})+ c.Params = append(c.Params, gin.Param{Key: "attachmentID", Value: attachmentID})+ h.GetAttachment(c)+ return+ }+ resp, err := h.Controller.GetCurrentContactAttachmentURL(c, c.Param("id"), string(contactmodel.ProfilePicture))if err != nil {handler.Error(c, stacktrace.Propagate(err, "failed to get profile picture url"))return@@ -112,10 +138,24 @@ func (h *ContactHandler) GetProfilePicture(c *gin.Context) {c.JSON(http.StatusOK, resp)}+func (h *ContactHandler) DeleteContactAttachment(c *gin.Context) {+ resp, err := h.Controller.DeleteContactAttachment(c, c.Param("id"), c.Param("type"))+ if err != nil {+ handler.Error(c, stacktrace.Propagate(err, "failed to delete contact attachment"))+ return+ }+ c.JSON(http.StatusOK, resp)+}+func (h *ContactHandler) DeleteProfilePicture(c *gin.Context) {- resp, err := h.Controller.DeleteProfilePicture(c, c.Param("id"))+ c.Params = append(c.Params, gin.Param{Key: "type", Value: string(contactmodel.ProfilePicture)})+ h.DeleteContactAttachment(c)+}++func (h *ContactHandler) GetCurrentContactAttachment(c *gin.Context) {+ resp, err := h.Controller.GetCurrentContactAttachmentURL(c, c.Param("id"), c.Param("type"))if err != nil {- handler.Error(c, stacktrace.Propagate(err, "failed to delete profile picture"))+ handler.Error(c, stacktrace.Propagate(err, "failed to get current contact attachment url"))return}c.JSON(http.StatusOK, resp)