Code cleanup
This commit is contained in:
parent
20cc4db097
commit
f5cdbe1bd3
|
@ -143,7 +143,7 @@ func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
|
|||
path := c.Query("path")
|
||||
log.Printf("Deleting '%s'...", path)
|
||||
if err := resourceManager.Delete(path); err != nil {
|
||||
c.AbortWithError(400, err)
|
||||
_ = c.AbortWithError(400, err)
|
||||
} else {
|
||||
log.Printf("Deleted '%s'", path)
|
||||
c.Status(204)
|
||||
|
|
|
@ -32,14 +32,14 @@ func Auth() gin.HandlerFunc {
|
|||
authHeader := c.GetHeader("Authorization")
|
||||
if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth {
|
||||
if strings.TrimPrefix(authHeader, "Basic ") == basicAuth {
|
||||
c.Set("secure", "basic")
|
||||
c.Set("secure", security.TypeBasic)
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth {
|
||||
key := strings.TrimPrefix(authHeader, "Api ")
|
||||
if hasValidApiKey(apiAuths, key) {
|
||||
c.Set("secure", "api")
|
||||
c.Set("secure", security.TypeApi)
|
||||
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) {
|
||||
value, exists := c.Get("secure")
|
||||
if !exists {
|
||||
abort(c, nil, http.StatusUnauthorized, "missing auth")
|
||||
} else {
|
||||
securityType := value.(string)
|
||||
securityType := security.Type(value.(string))
|
||||
for _, t := range types {
|
||||
if t == securityType {
|
||||
return
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
"resource-manager/domain/cache"
|
||||
"resource-manager/domain/resource"
|
||||
"resource-manager/security"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -31,13 +32,13 @@ func RegisterRoutes(server *gin.Engine) {
|
|||
|
||||
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.GET("presign", HandlePresign(resourceManager))
|
||||
resourcesGroup.PUT("copy", HandleCopy(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 {
|
||||
|
|
|
@ -2,7 +2,6 @@ package resource
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -46,9 +45,9 @@ func checkFileExists(path string) bool {
|
|||
_, err := os.Stat(path)
|
||||
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)
|
||||
file, err = ioutil.ReadFile(fullPath)
|
||||
file, err = os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error())
|
||||
}
|
||||
|
@ -99,11 +98,11 @@ func copyFolder(source, destination string, overwrite bool) error {
|
|||
doesExist = true
|
||||
}
|
||||
if !doesExist || overwrite {
|
||||
var data, err1 = ioutil.ReadFile(filepath.Join(source, relPath))
|
||||
var data, err1 = os.ReadFile(filepath.Join(source, relPath))
|
||||
if err1 != nil {
|
||||
return err1
|
||||
}
|
||||
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0777)
|
||||
return os.WriteFile(filepath.Join(destination, relPath), data, 0777)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -66,22 +66,22 @@ func (a *awsManager) Upload(request UploadRequest) {
|
|||
a.addToCurrentlyUploading(request.Path)
|
||||
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) {
|
||||
return "", errors.New("failed upload")
|
||||
}
|
||||
// ask cache for latest url
|
||||
if url, err := a.cache.Get(path); err == nil {
|
||||
return url, nil
|
||||
if uri, err := a.cache.Get(path); err == nil {
|
||||
return uri, nil
|
||||
}
|
||||
// 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 {
|
||||
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)
|
||||
}
|
||||
func (a *awsManager) Copy(from string, to string, overwrite bool) error {
|
||||
|
|
|
@ -11,6 +11,14 @@ type PresignToken struct {
|
|||
Path string
|
||||
}
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeBasic Type = "basic"
|
||||
TypeApi Type = "api"
|
||||
TypeToken Type = "token"
|
||||
)
|
||||
|
||||
func CreateToken(tokenInfo PresignToken, duration time.Duration) (string, error) {
|
||||
// jwt token
|
||||
atClaims := jwt.MapClaims{}
|
||||
|
|
Loading…
Reference in New Issue