Code cleanup

This commit is contained in:
Borna Rajković 2024-01-06 14:17:22 +01:00
parent 20cc4db097
commit f5cdbe1bd3
6 changed files with 27 additions and 19 deletions

View File

@ -143,7 +143,7 @@ func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
path := c.Query("path") path := c.Query("path")
log.Printf("Deleting '%s'...", path) log.Printf("Deleting '%s'...", path)
if err := resourceManager.Delete(path); err != nil { if err := resourceManager.Delete(path); err != nil {
c.AbortWithError(400, err) _ = c.AbortWithError(400, err)
} else { } else {
log.Printf("Deleted '%s'", path) log.Printf("Deleted '%s'", path)
c.Status(204) c.Status(204)

View File

@ -32,14 +32,14 @@ func Auth() gin.HandlerFunc {
authHeader := c.GetHeader("Authorization") authHeader := c.GetHeader("Authorization")
if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth { if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth {
if strings.TrimPrefix(authHeader, "Basic ") == basicAuth { if strings.TrimPrefix(authHeader, "Basic ") == basicAuth {
c.Set("secure", "basic") c.Set("secure", security.TypeBasic)
return return
} }
} }
if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth { if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth {
key := strings.TrimPrefix(authHeader, "Api ") key := strings.TrimPrefix(authHeader, "Api ")
if hasValidApiKey(apiAuths, key) { if hasValidApiKey(apiAuths, key) {
c.Set("secure", "api") c.Set("secure", security.TypeApi)
return return
} }
} }
@ -47,13 +47,13 @@ func Auth() gin.HandlerFunc {
} }
} }
func Secure(types ...string) gin.HandlerFunc { func Secure(types ...security.Type) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
value, exists := c.Get("secure") value, exists := c.Get("secure")
if !exists { if !exists {
abort(c, nil, http.StatusUnauthorized, "missing auth") abort(c, nil, http.StatusUnauthorized, "missing auth")
} else { } else {
securityType := value.(string) securityType := security.Type(value.(string))
for _, t := range types { for _, t := range types {
if t == securityType { if t == securityType {
return return

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"resource-manager/domain/cache" "resource-manager/domain/cache"
"resource-manager/domain/resource" "resource-manager/domain/resource"
"resource-manager/security"
"time" "time"
) )
@ -31,13 +32,13 @@ func RegisterRoutes(server *gin.Engine) {
resourceManager := resource.NewManager(cacheManager, expiration) resourceManager := resource.NewManager(cacheManager, expiration)
resourcesGroup := server.Group("/api/v1/resources", Secure("basic", "api")) resourcesGroup := server.Group("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi))
resourcesGroup.POST("", HandleUpload(resourceManager)) resourcesGroup.POST("", HandleUpload(resourceManager))
resourcesGroup.GET("presign", HandlePresign(resourceManager)) resourcesGroup.GET("presign", HandlePresign(resourceManager))
resourcesGroup.PUT("copy", HandleCopy(resourceManager)) resourcesGroup.PUT("copy", HandleCopy(resourceManager))
resourcesGroup.DELETE("", HandleDelete(resourceManager)) resourcesGroup.DELETE("", HandleDelete(resourceManager))
server.GET("/api/v1/resources", Secure("basic", "api", "token"), HandleDownload(resourceManager)) server.GET("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi, security.TypeToken), HandleDownload(resourceManager))
} }
func loadExpiration() time.Duration { func loadExpiration() time.Duration {

View File

@ -2,7 +2,6 @@ package resource
import ( import (
"context" "context"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -46,9 +45,9 @@ func checkFileExists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
return err == nil return err == nil
} }
func (f *fileManager) Download(ctx context.Context, path string) (file []byte, err error) { func (f *fileManager) Download(_ context.Context, path string) (file []byte, err error) {
fullPath := filepath.Join(f.path, path) fullPath := filepath.Join(f.path, path)
file, err = ioutil.ReadFile(fullPath) file, err = os.ReadFile(fullPath)
if err != nil { if err != nil {
log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error()) log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error())
} }
@ -99,11 +98,11 @@ func copyFolder(source, destination string, overwrite bool) error {
doesExist = true doesExist = true
} }
if !doesExist || overwrite { if !doesExist || overwrite {
var data, err1 = ioutil.ReadFile(filepath.Join(source, relPath)) var data, err1 = os.ReadFile(filepath.Join(source, relPath))
if err1 != nil { if err1 != nil {
return err1 return err1
} }
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0777) return os.WriteFile(filepath.Join(destination, relPath), data, 0777)
} else { } else {
return nil return nil
} }

View File

@ -66,22 +66,22 @@ func (a *awsManager) Upload(request UploadRequest) {
a.addToCurrentlyUploading(request.Path) a.addToCurrentlyUploading(request.Path)
a.requests <- request a.requests <- request
} }
func (a *awsManager) Presign(ctx context.Context, path string) (string, error) { func (a *awsManager) Presign(_ context.Context, path string) (string, error) {
if !a.waitForUploadComplete(path) { if !a.waitForUploadComplete(path) {
return "", errors.New("failed upload") return "", errors.New("failed upload")
} }
// ask cache for latest url // ask cache for latest url
if url, err := a.cache.Get(path); err == nil { if uri, err := a.cache.Get(path); err == nil {
return url, nil return uri, nil
} }
// if there is no value in cache, presign url and add it to cache // if there is no value in cache, presign url and add it to cache
url, err := a.presign(path, a.expiration) uri, err := a.presign(path, a.expiration)
if err != nil { if err != nil {
a.cache.Set(path, url, a.expiration) _ = a.cache.Set(path, uri, a.expiration)
} }
return url, err return uri, err
} }
func (a *awsManager) Download(ctx context.Context, path string) (file []byte, err error) { func (a *awsManager) Download(_ context.Context, path string) (file []byte, err error) {
return a.download(path) return a.download(path)
} }
func (a *awsManager) Copy(from string, to string, overwrite bool) error { func (a *awsManager) Copy(from string, to string, overwrite bool) error {

View File

@ -11,6 +11,14 @@ type PresignToken struct {
Path string Path string
} }
type Type string
const (
TypeBasic Type = "basic"
TypeApi Type = "api"
TypeToken Type = "token"
)
func CreateToken(tokenInfo PresignToken, duration time.Duration) (string, error) { func CreateToken(tokenInfo PresignToken, duration time.Duration) (string, error) {
// jwt token // jwt token
atClaims := jwt.MapClaims{} atClaims := jwt.MapClaims{}