Implemented presign logic for file manager

This commit is contained in:
Borna Rajković committed 2024-01-06 14:10:24 +01:00
1 parent fdd46f5086
commit 20cc4db097
11 files changed
+234 -141

No files matched your search

+7 -2
View File
@@ -120,13 +120,18 @@ func HandlePresign(resourceManager resource.Manager) gin.HandlerFunc {
func HandleDownload(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
path := ""
if c.GetString("secure") == "token" {
path = c.GetString("path")
} else {
path = c.Query("path")
}
log.Printf("Downloading '%s'...", path)
data, err := resourceManager.Download(c, path)
if err == nil {
log.Printf("Downloaded '%s'", path)
c.Header("content-disposition", "inline; filename=\""+filename(path)+"\"")
c.Data(200, readMimeType(path, ""), data)
log.Printf("Downloaded '%s'", path)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
+12 -1
View File
@@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"resource-manager/security"
"strings"
"time"
)
@@ -18,6 +19,16 @@ func Auth() gin.HandlerFunc {
}
return func(c *gin.Context) {
if rawToken := c.Query("token"); rawToken != "" {
token, err := security.ParseToken(rawToken)
if err != nil {
abort(c, nil, http.StatusUnauthorized, "invalid token")
return
}
c.Set("secure", "token")
c.Set("path", token.Path)
return
}
authHeader := c.GetHeader("Authorization")
if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth {
if strings.TrimPrefix(authHeader, "Basic ") == basicAuth {
@@ -63,6 +74,6 @@ func hasValidApiKey(auths []string, key string) bool {
}
func abort(c *gin.Context, err error, statusCode int, message string) {
log.Printf("Aborted: %v", err.Error())
log.Printf("Aborted with error: %v", err)
c.AbortWithStatusJSON(statusCode, gin.H{"status": statusCode, "created": time.Now(), "message": message})
}
+6 -6
View File
@@ -31,13 +31,13 @@ func RegisterRoutes(server *gin.Engine) {
resourceManager := resource.NewManager(cacheManager, expiration)
v1Group := server.Group("/api/v1/resources", Secure("basic", "api"))
resourcesGroup := server.Group("/api/v1/resources", Secure("basic", "api"))
resourcesGroup.POST("", HandleUpload(resourceManager))
resourcesGroup.GET("presign", HandlePresign(resourceManager))
resourcesGroup.PUT("copy", HandleCopy(resourceManager))
resourcesGroup.DELETE("", HandleDelete(resourceManager))
v1Group.POST("", HandleUpload(resourceManager))
v1Group.GET("", HandleDownload(resourceManager))
v1Group.GET("presign", HandlePresign(resourceManager))
v1Group.PUT("copy", HandleCopy(resourceManager))
v1Group.DELETE("", HandleDelete(resourceManager))
server.GET("/api/v1/resources", Secure("basic", "api", "token"), HandleDownload(resourceManager))
}
func loadExpiration() time.Duration {