Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add endpoint for multipart too

Neeraj Gupta9ccc49fa26db
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 4a44b59ffa..c22e689837 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -716,6 +716,7 @@ func main() {
publicCollectionAPI.GET("/upload-urls", publicCollectionHandler.GetUploadUrls)
publicCollectionAPI.POST("/upload-url", publicCollectionHandler.GetUploadURLV2)
publicCollectionAPI.GET("/multipart-upload-urls", publicCollectionHandler.GetMultipartUploadURLs)
+ publicCollectionAPI.POST("/multipart-upload-url", publicCollectionHandler.GetMultipartUploadURLV2)
publicCollectionAPI.POST("/file", publicCollectionHandler.CreateFile)
publicCollectionAPI.POST("/verify-password", publicCollectionHandler.VerifyPassword)
publicCollectionAPI.GET("/social/diff", publicSocialHandler.SocialDiff)
diff --git a/server/pkg/api/public_collection.go b/server/pkg/api/public_collection.go
index 5d51163f16..39edf6165e 100644
--- a/server/pkg/api/public_collection.go
+++ b/server/pkg/api/public_collection.go
@@ -179,6 +179,27 @@ func (h *PublicCollectionHandler) GetMultipartUploadURLs(c *gin.Context) {
})
}
+// GetMultipartUploadURLV2 returns multipart upload URLs for a single object with enforced metadata
+func (h *PublicCollectionHandler) GetMultipartUploadURLV2(c *gin.Context) {
+ enteApp := auth.GetApp(c)
+ var req ente.MultipartUploadURLRequest
+ if err := c.ShouldBindJSON(&req); err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ collection, err := h.Controller.GetPublicCollection(c, true)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ upload, err := h.FileCtrl.GetMultipartUploadURLWithMetadata(c, collection.Owner.ID, req, enteApp)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ c.JSON(http.StatusOK, upload)
+}
+
// CreateFile create a new file inside the collection corresponding to the public accessToken
func (h *PublicCollectionHandler) CreateFile(c *gin.Context) {
var file ente.File
diff --git a/server/pkg/middleware/rate_limit.go b/server/pkg/middleware/rate_limit.go
index 32467fdfb7..e39204eddd 100644
--- a/server/pkg/middleware/rate_limit.go
+++ b/server/pkg/middleware/rate_limit.go
@@ -8,6 +8,7 @@ import (
"sync/atomic"
"time"
+ "github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/controller/discord"
util "github.com/ente-io/museum/pkg/utils"
"github.com/ente-io/museum/pkg/utils/auth"
@@ -24,6 +25,7 @@ type RateLimitMiddleware struct {
reset time.Duration
ticker *time.Ticker
limit10ReqPerMin *limiter.Limiter
+ limit50ReqPerMin *limiter.Limiter
limit300ReqPerMin *limiter.Limiter
limit200ReqPerMin *limiter.Limiter
limit200ReqPerSec *limiter.Limiter
@@ -33,6 +35,7 @@ type RateLimitMiddleware struct {
func NewRateLimitMiddleware(discordCtrl *discord.DiscordController, limit int64, reset time.Duration) *RateLimitMiddleware {
rl := &RateLimitMiddleware{
limit10ReqPerMin: util.NewRateLimiter("10-M"),
+ limit50ReqPerMin: util.NewRateLimiter("50-M"),
limit300ReqPerMin: util.NewRateLimiter("300-M"),
limit200ReqPerMin: util.NewRateLimiter("200-M"),
limit200ReqPerSec: util.NewRateLimiter("200-S"),
@@ -101,7 +104,7 @@ func (r *RateLimitMiddleware) APIRateLimitMiddleware(urlSanitizer func(_ *gin.Co
rateLimiter := r.getLimiter(requestPath, c.Request.Method)
if rateLimiter != nil {
- key := fmt.Sprintf("%s-%s", network.GetClientIP(c), requestPath)
+ key := r.getRateLimitKey(c, requestPath)
limitContext, err := rateLimiter.Get(c, key)
if err != nil {
log.Error("Failed to check rate limit", err)
@@ -160,6 +163,30 @@ func (r *RateLimitMiddleware) getGlobalLimiter(reqPath string, reqMethod string)
return nil
}
+func (r *RateLimitMiddleware) getRateLimitKey(c *gin.Context, reqPath string) string {
+ if !isPublicCollectionUploadURLPath(reqPath) {
+ return fmt.Sprintf("%s-%s", network.GetClientIP(c), reqPath)
+ }
+ value, ok := c.Get(auth.PublicAccessKey)
+ if !ok {
+ log.WithField("path", reqPath).Warn("public access context missing for collection scoped rate limit")
+ return fmt.Sprintf("%s-%s", network.GetClientIP(c), reqPath)
+ }
+ accessContext, ok := value.(ente.PublicAccessContext)
+ if !ok {
+ log.WithField("path", reqPath).Warn("invalid public access context for collection scoped rate limit")
+ return fmt.Sprintf("%s-%s", network.GetClientIP(c), reqPath)
+ }
+ return fmt.Sprintf("collection:%d-%s", accessContext.CollectionID, reqPath)
+}
+
+func isPublicCollectionUploadURLPath(reqPath string) bool {
+ return reqPath == "/public-collection/upload-urls" ||
+ reqPath == "/public-collection/upload-url" ||
+ reqPath == "/public-collection/multipart-upload-urls" ||
+ reqPath == "/public-collection/multipart-upload-url"
+}
+
// getLimiter, based on reqPath & reqMethod, return instance of limiter.Limiter which needs to
// be applied for a request. It returns nil if the request is not rate limited
func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limiter.Limiter {
@@ -199,5 +226,8 @@ func (r *RateLimitMiddleware) getLimiter(reqPath string, reqMethod string) *limi
(reqMethod == http.MethodPost || reqMethod == http.MethodPut || reqMethod == http.MethodDelete) {
return r.limit200ReqPerMin
}
+ if isPublicCollectionUploadURLPath(reqPath) {
+ return r.limit50ReqPerMin
+ }
return nil
}