Compare commits

...

No commits in common. "7883b71c76f7a776f28b45d7cc80f94b2764c5d9" and "master" have entirely different histories.

22 changed files with 808 additions and 395 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
resource_manager
.idea/
.env

218
api/api.go Normal file
View File

@ -0,0 +1,218 @@
package api
import (
"bytes"
"encoding/base64"
"github.com/gin-gonic/gin"
"log"
"net/http"
"resource-manager/domain/resize"
"resource-manager/domain/resource"
"strings"
"time"
)
type UploadRequest struct {
Content string `json:"content"`
Path string `json:"path"`
Properties struct {
Overwrite bool `json:"overwrite"`
MimeType string `json:"mimeType"`
} `json:"properties"`
Resize *struct {
Width int `json:"width"`
Height int `json:"height"`
Type string `json:"type"`
} `json:"resize"`
}
func NoMethod() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"status": 404,
"created": time.Now(),
"message": "no handler for method '" + c.Request.Method + "'",
})
}
}
func NoRoute() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"status": 404,
"created": time.Now(),
"message": "no handler for " + c.Request.Method + " '" + c.Request.URL.RequestURI() + "'",
})
}
}
func HandleUpload(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
var request UploadRequest
if err := c.BindJSON(&request); err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
return
}
if !isValidPath(request.Path) {
c.AbortWithStatusJSON(400, gin.H{"error": "path cannot start with ../ or contain /../"})
return
}
content, err := base64.StdEncoding.DecodeString(request.Content)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
return
}
if resize.IsResizable(readMimeType(request.Path, request.Properties.MimeType)) && request.Resize != nil {
content, err = resize.ResizeImage(content, resize.Resize{
Height: request.Resize.Height,
Width: request.Resize.Width,
Type: mapResizeType(request.Resize.Type),
})
if err != nil {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
return
}
}
log.Printf("Uploading '%s'...", request.Path)
resourceManager.Upload(resource.UploadRequest{
Buffer: bytes.NewBuffer(content),
Path: request.Path,
MimeType: request.Properties.MimeType,
Overwrite: request.Properties.Overwrite,
})
log.Printf("Uploaded '%s'", request.Path)
// we return this as success
c.Status(http.StatusNoContent)
}
}
func isValidPath(currentPath string) bool {
if strings.HasPrefix(currentPath, "../") || strings.Contains(currentPath, "/../") {
return false
}
return true
}
func HandleCopy(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
from := c.Query("from")
to := c.Query("to")
overwrite := c.Query("overwrite") == "true"
log.Printf("Copying from '%s' to '%s' (overwrite=%v)...", from, to, overwrite)
if err := resourceManager.Copy(from, to, overwrite); err != nil {
log.Println(err)
c.AbortWithStatus(500)
} else {
log.Printf("Done copyting from '%s' to '%s' (overwrite=%v)", from, to, overwrite)
c.Status(201)
}
}
}
func HandlePresign(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
if !isValidPath(path) {
c.AbortWithStatusJSON(400, gin.H{"error": "path cannot start with ../ or contain /../"})
return
}
url, err := resourceManager.Presign(c, path)
if err != nil {
c.AbortWithStatus(404)
return
}
log.Printf("Presigned '%s'", path)
if c.Query("redirect") == "true" {
c.Redirect(http.StatusTemporaryRedirect, url)
} else {
c.JSON(200, gin.H{"url": url})
}
}
}
func HandleDownload(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := ""
if c.GetString("secure") == "token" {
path = c.GetString("path")
} else {
path = c.Query("path")
}
if !isValidPath(path) {
c.AbortWithStatusJSON(400, gin.H{"error": "path cannot start with ../ or contain /../"})
return
}
log.Printf("Downloading '%s'...", path)
data, err := resourceManager.Download(c, path)
if err == nil {
c.Header("content-disposition", "inline; filename=\""+filename(path)+"\"")
c.Data(200, readMimeType(path, ""), data)
log.Printf("Downloaded '%s'", path)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
}
}
func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
if !isValidPath(path) {
c.AbortWithStatusJSON(400, gin.H{"error": "path cannot start with ../ or contain /../"})
return
}
log.Printf("Deleting '%s'...", path)
if err := resourceManager.Delete(path); err != nil {
_ = c.AbortWithError(400, err)
} else {
log.Printf("Deleted '%s'", path)
c.Status(204)
}
}
}
var mimeTypes = map[string]string{
"jpg": "image/jpg",
"jpeg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"json": "application/json",
"pdf": "application/pdf",
"html": "text/html",
"xml": "application/xml",
}
func readMimeType(path string, mimeType string) string {
if mimeType != "" {
return mimeType
}
parts := strings.Split(path, ".")
fileType := strings.ToLower(parts[len(parts)-1])
if value, exists := mimeTypes[fileType]; exists {
return value
}
return "application/octet-stream"
}
func mapResizeType(resizeType string) resize.ResizeType {
switch resizeType {
case "cover":
return resize.Cover
case "contain":
return resize.Contain
case "exact_height":
return resize.ExactHeight
case "exact_width":
return resize.ExactWidth
default:
return resize.Exact
}
}
func filename(path string) string {
substrings := strings.Split(path, "/")
return substrings[len(substrings)-1]
}

79
api/middleware.go Normal file
View File

@ -0,0 +1,79 @@
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"resource-manager/security"
"strings"
"time"
)
func Auth() gin.HandlerFunc {
basicAuth, hasBasicAuth := os.LookupEnv("BASIC_AUTH_CREDENTIALS")
var apiAuths []string
if list, hasApiAuth := os.LookupEnv("API_CREDENTIALS"); hasApiAuth {
apiAuths = strings.Split(list, ",")
}
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 {
c.Set("secure", security.TypeBasic)
return
}
}
if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth {
key := strings.TrimPrefix(authHeader, "Api ")
if hasValidApiKey(apiAuths, key) {
c.Set("secure", security.TypeApi)
return
}
}
abort(c, nil, http.StatusUnauthorized, "missing auth")
}
}
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.(security.Type)
for _, t := range types {
if t == securityType {
return
}
}
abort(c, nil, http.StatusUnauthorized, fmt.Sprintf("bad security: received %s type", securityType))
}
}
}
func hasValidApiKey(auths []string, key string) bool {
for _, a := range auths {
if a == key {
return true
}
}
return false
}
func abort(c *gin.Context, err error, statusCode int, message string) {
log.Printf("Aborted with error: %v", err)
c.AbortWithStatusJSON(statusCode, gin.H{"status": statusCode, "created": time.Now(), "message": message})
}

53
api/routes.go Normal file
View File

@ -0,0 +1,53 @@
package api
import (
"github.com/gin-gonic/gin"
"log"
"os"
"resource-manager/domain/cache"
"resource-manager/domain/resource"
"resource-manager/security"
"time"
)
func SetupServer() *gin.Engine {
server := createServer()
RegisterRoutes(server)
return server
}
func createServer() *gin.Engine {
server := gin.New()
server.NoRoute(NoRoute())
server.NoMethod(NoMethod())
server.Use(gin.Recovery(), Auth())
return server
}
func RegisterRoutes(server *gin.Engine) {
cacheManager := cache.NewManager()
expiration := loadExpiration()
log.Println("Presign | expiration set to " + expiration.String())
resourceManager := resource.NewManager(cacheManager, expiration)
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(security.TypeBasic, security.TypeApi, security.TypeToken), HandleDownload(resourceManager))
}
func loadExpiration() time.Duration {
if value := os.Getenv("PRESIGN_DURATION"); value != "" {
duration, err := time.ParseDuration(value)
if err != nil {
return duration
}
}
// default duration
return 1 * time.Hour
}

73
cache/impl.go vendored
View File

@ -1,73 +0,0 @@
package cache
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
"os"
"time"
)
func newRedisCache() Manager {
redisClient := redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_URL"),
Username: os.Getenv("REDIS_USERNAME"),
Password: os.Getenv("REDIS_PASSWORD"),
})
return &redisCache{redisClient}
}
type redisCache struct {
client *redis.Client
}
func (r *redisCache) Set(key, value string, expiration time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
defer cancel()
return r.client.Set(ctx, key, value, expiration).Err()
}
func (r *redisCache) Get(key string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
defer cancel()
cacheValue := r.client.Get(ctx, key)
return cacheValue.Val(), cacheValue.Err()
}
func (r *redisCache) Remove(key string) {
ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
defer cancel()
r.client.Del(ctx, key)
}
func newInMemCache() Manager {
return &inMemCache{make(map[string]inMemEntry)}
}
type inMemEntry struct {
value string
expiration time.Time
}
type inMemCache struct {
entries map[string]inMemEntry
}
func (i *inMemCache) Set(key, value string, expiration time.Duration) error {
i.entries[key] = inMemEntry{
value: value,
expiration: time.Now().Add(expiration),
}
return nil
}
func (i *inMemCache) Get(key string) (string, error) {
if val, exists := i.entries[key]; exists {
if val.expiration.Before(time.Now()) {
delete(i.entries, key)
} else {
return val.value, nil
}
}
return "", errors.New("no value for given key")
}
func (i *inMemCache) Remove(key string) {
delete(i.entries, key)
}

View File

@ -0,0 +1,8 @@
version: '3.1'
services:
rem:
image: registry.bbr-dev.info/resource-manager/backend:latest-dev
restart: on-failure
ports:
- 5201:5201

19
dockerfile Normal file
View File

@ -0,0 +1,19 @@
### STAGE 1.a: Prepare certs ###
FROM alpine:latest as certs
RUN apk --update add ca-certificates
### STAGE 1.b: Build ###
FROM golang as go-build
WORKDIR /root
COPY go.mod go.sum ./
# install packages
RUN go mod download
COPY . .
ENV CGO_ENABLED=0
RUN go build -tags timetzdata resource-manager
### Stage 2: Run ###
FROM scratch
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=go-build /root/resource-manager /usr/bin/resource-manager
ENTRYPOINT ["resource-manager"]

40
domain/cache/in_mem_cache.go vendored Normal file
View File

@ -0,0 +1,40 @@
package cache
import (
"errors"
"time"
)
func newInMemCache() Manager {
return &inMemCache{make(map[string]inMemEntry)}
}
type inMemEntry struct {
value string
expiration time.Time
}
type inMemCache struct {
entries map[string]inMemEntry
}
func (i *inMemCache) Set(key, value string, expiration time.Duration) error {
i.entries[key] = inMemEntry{
value: value,
expiration: time.Now().Add(expiration),
}
return nil
}
func (i *inMemCache) Get(key string) (string, error) {
if val, exists := i.entries[key]; exists {
if val.expiration.Before(time.Now()) {
delete(i.entries, key)
} else {
return val.value, nil
}
}
return "", errors.New("no value for given key")
}
func (i *inMemCache) Remove(key string) {
delete(i.entries, key)
}

38
domain/cache/redis_cache.go vendored Normal file
View File

@ -0,0 +1,38 @@
package cache
import (
"context"
"github.com/go-redis/redis/v8"
"os"
"time"
)
func newRedisCache() Manager {
redisClient := redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_URL"),
Username: os.Getenv("REDIS_USERNAME"),
Password: os.Getenv("REDIS_PASSWORD"),
})
return &redisCache{redisClient}
}
type redisCache struct {
client *redis.Client
}
func (r *redisCache) Set(key, value string, expiration time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
return r.client.Set(ctx, key, value, expiration).Err()
}
func (r *redisCache) Get(key string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
cacheValue := r.client.Get(ctx, key)
return cacheValue.Val(), cacheValue.Err()
}
func (r *redisCache) Remove(key string) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
r.client.Del(ctx, key)
}

105
domain/resize/resize.go Normal file
View File

@ -0,0 +1,105 @@
package resize
import (
"bytes"
"github.com/anthonynsimon/bild/transform"
"image"
"image/jpeg"
"image/png"
"io"
)
var mimeTypes = map[string]string{
"image/jpg": "image/jpg",
"image/jpeg": "image/jpeg",
"image/png": "image/png",
}
func IsResizable(mimeType string) bool {
_, present := mimeTypes[mimeType]
return present
}
type ResizeType int
const (
// Cover - resize preserving image aspect
// - resizes to the smallest image where both width and height are larger or equal to given size
Cover ResizeType = iota
// Contain - resize preserving image aspect
// - resizes to the largest image where both width and height are smaller or equal to given size
Contain ResizeType = iota
// ExactHeight - resize preserving image aspect
// - resizes to the image with given height
ExactHeight ResizeType = iota
// ExactWidth - resize preserving image aspect
// - resizes to the image with given width
ExactWidth ResizeType = iota
// Exact - resize without preserving image aspect
// - resizes to exact size defined in request
Exact ResizeType = iota
)
type Resize struct {
Height int
Width int
Type ResizeType
}
func ResizeImage(imageBytes []byte, resize Resize) ([]byte, error) {
img, format, err := image.Decode(bytes.NewReader(imageBytes))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
writer := io.Writer(&buffer)
resizeWidth, resizeHeight := calculateResizedDimensions(img.Bounds(), resize)
img = transform.Resize(img, resizeWidth, resizeHeight, transform.Gaussian)
switch format {
case "png":
err = png.Encode(writer, img)
case "jpeg":
err = jpeg.Encode(writer, img, nil)
default:
err = jpeg.Encode(writer, img, nil)
}
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func calculateResizedDimensions(bounds image.Rectangle, resize Resize) (width int, height int) {
width = bounds.Dx()
height = bounds.Dy()
switch resize.Type {
case Cover:
rWidth := resize.Height * width / height
if rWidth >= resize.Width {
return rWidth, resize.Height
}
rHeight := resize.Width * height / width
if rHeight >= resize.Height {
return resize.Width, rHeight
}
case Contain:
rWidth := resize.Height * width / height
if rWidth <= resize.Width {
return rWidth, resize.Height
}
rHeight := resize.Width * height / width
if rHeight <= resize.Height {
return resize.Width, rHeight
}
case ExactHeight:
rWidth := resize.Height * width / height
return rWidth, resize.Height
case ExactWidth:
rHeight := resize.Width * height / width
return resize.Width, rHeight
}
return
}

View File

@ -0,0 +1,112 @@
package resource
import (
"context"
"log"
"os"
"path/filepath"
"resource-manager/domain/cache"
"resource-manager/security"
"strings"
"time"
)
func newLocalFolder(cacheManager cache.Manager, expiration time.Duration) Manager {
path := os.Getenv("LOCAL_PATH_PREFIX")
// if path isn't set use local
if path == "" {
path, _ = os.Getwd()
}
log.Println("Manager | using path (" + path + ") and expiration of " + expiration.String())
manager := fileManager{cacheManager, path, expiration}
return &manager
}
type fileManager struct {
cache cache.Manager
path string
expiration time.Duration
}
func (f *fileManager) Upload(request UploadRequest) {
fullPath := filepath.Join(f.path, request.Path)
createFolder(fullPath)
if checkFileExists(fullPath) && !request.Overwrite {
log.Println("Manager | cannot upload file as file on same path already exists")
return
}
log.Println("Manager | uploading to (" + request.Path + ")")
if err := os.WriteFile(fullPath, request.Buffer.Bytes(), 0o644); err != nil {
log.Println("Manager | failed uploading (" + request.Path + ") cause: " + err.Error())
}
}
func checkFileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func (f *fileManager) Download(_ context.Context, path string) (file []byte, err error) {
fullPath := filepath.Join(f.path, path)
file, err = os.ReadFile(fullPath)
if err != nil {
log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error())
}
return
}
func (f *fileManager) Presign(_ context.Context, path string) (string, error) {
token, err := security.CreateToken(security.PresignToken{Path: path}, f.expiration)
if err != nil {
return "", err
}
return "/api/v1/resources?token=" + token, nil
}
func (f *fileManager) Copy(from string, to string, overwrite bool) error {
fromPath := filepath.Join(f.path, from)
toPath := filepath.Join(f.path, to)
log.Println("Manager | copying from (" + fromPath + ") to (" + toPath + ")")
createFolder(filepath.Join(toPath, "file"))
return copyFolder(fromPath, toPath, overwrite)
}
func (f *fileManager) Delete(path string) error {
log.Println("Manager | deleting " + path)
fullPath := filepath.Join(f.path, path)
if err := os.Remove(fullPath); err != nil {
log.Println("Manager | failed deleting (" + path + ") cause: " + err.Error())
return err
}
f.cache.Remove(path)
return nil
}
func createFolder(path string) {
paths := strings.Split(path, "/")
folderPath := "/" + filepath.Join(paths[:len(paths)-1]...)
if err := os.MkdirAll(folderPath, 0o755); err != nil {
log.Println("[error] ", err)
}
}
func copyFolder(source, destination string, overwrite bool) error {
var err = filepath.WalkDir(source, func(path string, d os.DirEntry, err error) error {
relPath := strings.Replace(path, source, "", 1)
if relPath == "" {
return nil
}
if d.IsDir() {
return os.Mkdir(filepath.Join(destination, relPath), 0755)
} else {
doesExist := false
if _, err := os.Stat(filepath.Join(destination, relPath)); !os.IsNotExist(err) {
doesExist = true
}
if !doesExist || overwrite {
var data, err1 = os.ReadFile(filepath.Join(source, relPath))
if err1 != nil {
return err1
}
return os.WriteFile(filepath.Join(destination, relPath), data, 0777)
} else {
return nil
}
}
})
return err
}

View File

@ -5,7 +5,7 @@ import (
"context"
"log"
"os"
"resource_manager/cache"
"resource-manager/domain/cache"
"strings"
"time"
)
@ -22,6 +22,7 @@ func NewManager(cacheManager cache.Manager, expiration time.Duration) Manager {
log.Println("Manager | using S3 for data storage")
return newS3Bucket(cacheManager, expiration)
} else {
log.Println("Manager | using local file for data storage")
return newLocalFolder(cacheManager, expiration)
}
}

View File

@ -8,104 +8,16 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"resource_manager/cache"
"resource-manager/domain/cache"
"strconv"
"strings"
"sync"
"time"
)
func newLocalFolder(cacheManager cache.Manager, expiration time.Duration) Manager {
path := os.Getenv("UPLOAD_PATH")
// if path isn't set use local
if path == "" {
path, _ = os.Getwd()
}
log.Println("Manager | using local file system for data storage (" + path + ")")
manager := fileManager{cacheManager, path}
return &manager
}
type fileManager struct {
cache cache.Manager
path string
}
func (f *fileManager) Upload(request UploadRequest) {
fullPath := filepath.Join(f.path, request.Path)
createFolder(fullPath)
log.Println("Manager | uploading to (" + request.Path + ")")
if err := ioutil.WriteFile(fullPath, request.Buffer.Bytes(), 0o644); err != nil {
log.Println("Manager | failed uploading (" + request.Path + ") cause: " + err.Error())
}
}
func (f *fileManager) Download(ctx context.Context, path string) (file []byte, err error) {
fullPath := filepath.Join(f.path, path)
file, err = ioutil.ReadFile(fullPath)
if err != nil {
log.Println("Manager | failed downloading (" + path + ") cause: " + err.Error())
}
return
}
func (f *fileManager) Presign(_ context.Context, path string) (string, error) {
return "/api/v1/get?path=" + path, nil
}
func (f *fileManager) Copy(from string, to string, overwrite bool) error {
fromPath := filepath.Join(f.path, from)
toPath := filepath.Join(f.path, to)
log.Println("Manager | copying from (" + fromPath + ") to (" + toPath + ")")
createFolder(filepath.Join(toPath, "file"))
return copyFolder(fromPath, toPath, overwrite)
}
func (f *fileManager) Delete(path string) error {
log.Println("Manager | deleting " + path)
fullPath := filepath.Join(f.path, path)
if err := os.Remove(fullPath); err != nil {
log.Println("Manager | failed deleting (" + path + ") cause: " + err.Error())
return err
}
f.cache.Remove(path)
return nil
}
func createFolder(path string) {
paths := strings.Split(path, "/")
folderPath := "/" + filepath.Join(paths[:len(paths)-1]...)
if err := os.MkdirAll(folderPath, 0o755); err != nil {
log.Println("[error] ", err)
}
}
func copyFolder(source, destination string, overwrite bool) error {
var err = filepath.WalkDir(source, func(path string, d os.DirEntry, err error) error {
relPath := strings.Replace(path, source, "", 1)
if relPath == "" {
return nil
}
if d.IsDir() {
return os.Mkdir(filepath.Join(destination, relPath), 0755)
} else {
doesExist := false
if _, err := os.Stat(filepath.Join(destination, relPath)); !os.IsNotExist(err) {
doesExist = true
}
if !doesExist || overwrite {
var data, err1 = ioutil.ReadFile(filepath.Join(source, relPath))
if err1 != nil {
return err1
}
return ioutil.WriteFile(filepath.Join(destination, relPath), data, 0777)
} else {
return nil
}
}
})
return err
}
func newS3Bucket(cacheManager cache.Manager, expiration time.Duration) Manager {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(os.Getenv("AWS_REGION_NAME"))},
@ -154,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 {

22
go.mod
View File

@ -1,32 +1,32 @@
module resource_manager
module resource-manager
go 1.17
go 1.18
require (
github.com/anthonynsimon/bild v0.13.0
github.com/aws/aws-sdk-go v1.42.7
github.com/gin-gonic/gin v1.7.4
github.com/go-redis/redis/v8 v8.11.4
github.com/joho/godotenv v1.4.0
)
require (
github.com/anthonynsimon/bild v0.13.0 // indirect
github.com/aws/aws-sdk-go v1.42.7 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.7.4 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/spf13/cobra v0.0.5 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 // indirect
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

22
go.sum
View File

@ -11,15 +11,18 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@ -30,6 +33,8 @@ github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
@ -45,14 +50,15 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
@ -70,31 +76,33 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
@ -106,7 +114,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 h1:uc17S921SPw5F2gJo7slQ3aqvr2RwpL7eb3+DZncu3s=
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -115,6 +122,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -137,6 +145,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@ -144,6 +153,7 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
@ -154,8 +164,10 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -1,109 +0,0 @@
package main
import (
"bytes"
"encoding/base64"
"github.com/gin-gonic/gin"
"log"
"net/http"
"resource_manager/resource"
"strings"
)
type LegacySave struct {
Content string `json:"content"`
Path string `json:"path"`
Properties struct {
Height int `json:"height"`
Overwrite bool `json:"overwrite"`
MimeType string `json:"mimeType"`
} `json:"properties"`
}
func HandleLegacySave(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
var legacySave LegacySave
if err := c.ShouldBindJSON(&legacySave); err == nil {
// removing image/(png/jpeg/...); start
if strings.HasPrefix(legacySave.Content, "data:") {
legacySave.Content = strings.Split(legacySave.Content, ";")[1]
}
if imageBytes, err := base64.StdEncoding.DecodeString(legacySave.Content); err == nil {
if legacySave.Properties.Height > 0 {
imageBytes, err = resizeImage(imageBytes, legacySave.Properties.Height)
}
mimeType := readMimeType(legacySave.Path, legacySave.Properties.MimeType)
if err == nil {
// request is sent to uplader service after which it is being uploaded
resourceManager.Upload(resource.UploadRequest{
Buffer: bytes.NewBuffer(imageBytes),
Path: legacySave.Path,
MimeType: mimeType,
})
// we return this as success
c.Status(http.StatusNoContent)
} else {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
}
} else {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
}
} else {
c.AbortWithStatusJSON(400, gin.H{"error": "bad request"})
}
}
}
func HandleCopy(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
from := c.Query("from")
to := c.Query("to")
overwrite := c.Query("overwrite") == "true"
if err := resourceManager.Copy(from, to, overwrite); err != nil {
log.Println(err)
c.AbortWithStatus(500)
} else {
c.Status(201)
}
}
}
func HandlePresign(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
url, err := resourceManager.Presign(c, path)
if err != nil {
c.AbortWithStatus(404)
return
}
if c.Query("redirect") == "true" {
c.Redirect(http.StatusTemporaryRedirect, url)
} else {
c.JSON(200, url)
}
}
}
func HandleGet(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
data, err := resourceManager.Download(c, path)
if err == nil {
c.Data(200, readMimeType(path, ""), data)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
}
}
func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Query("path")
if err := resourceManager.Delete(path); err != nil {
c.AbortWithError(400, err)
} else {
c.Status(204)
}
}
}

View File

@ -1,57 +0,0 @@
package main
import (
"bytes"
"github.com/anthonynsimon/bild/transform"
"image"
"image/jpeg"
"image/png"
"io"
"strings"
)
var mimeTypes = map[string]string{
"jpg": "image/jpg",
"jpeg": "image/jpeg",
"png": "image/png",
"json": "application/json",
"html": "text/html",
"xml": "application/xml",
}
func readMimeType(path string, mimeType string) string {
if mimeType != "" {
return mimeType
}
parts := strings.Split(path, ".")
fileType := parts[len(parts)-1]
if value, exists := mimeTypes[fileType]; exists {
return value
}
return "application/octet-stream"
}
func resizeImage(imageBytes []byte, resizeHeight int) ([]byte, error) {
img, format, err := image.Decode(bytes.NewReader(imageBytes))
if err != nil {
return nil, err
}
var buffer bytes.Buffer
writer := io.Writer(&buffer)
resizeWidth := img.Bounds().Dx() * resizeHeight / img.Bounds().Dy()
img = transform.Resize(img, resizeWidth, resizeHeight, transform.Gaussian)
switch format {
case "png":
err = png.Encode(writer, img)
break
case "jpeg":
err = jpeg.Encode(writer, img, nil)
break
default:
err = jpeg.Encode(writer, img, nil)
}
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}

45
main.go
View File

@ -1,15 +1,10 @@
package main
import (
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"resource_manager/cache"
"resource_manager/resource"
"strings"
"time"
"resource-manager/api"
)
func init() {
@ -18,42 +13,6 @@ func init() {
}
func main() {
cacheManager := cache.NewManager()
expiration := loadExpiration()
log.Println("Presign | expiration set to " + expiration.String())
resourceManager := resource.NewManager(cacheManager, expiration)
server := gin.Default()
if strings.Contains(os.Getenv("PROFILE"), "legacy") {
setupLegacyEndpoints(server, resourceManager)
}
setupV1Endpoints(server, resourceManager)
server := api.SetupServer()
log.Fatalln(http.ListenAndServe(":5201", server))
}
func loadExpiration() time.Duration {
if value := os.Getenv("PRESIGN_DURATION"); value != "" {
duration, err := time.ParseDuration(value)
if err != nil {
return duration
}
}
// default duration
return 1 * time.Hour
}
func setupLegacyEndpoints(server *gin.Engine, resourceManager resource.Manager) {
server.POST("/save", HandleLegacySave(resourceManager))
server.GET("/get", HandleGet(resourceManager))
server.GET("/presign", HandlePresign(resourceManager))
server.PUT("/copy", HandleCopy(resourceManager))
}
func setupV1Endpoints(server *gin.Engine, resourceManager resource.Manager) {
server.POST("/api/v1/save", HandleLegacySave(resourceManager))
server.GET("/api/v1/get", HandleGet(resourceManager))
server.GET("/api/v1/presign", HandlePresign(resourceManager))
server.PUT("/api/v1/copy", HandleCopy(resourceManager))
server.DELETE("/api/v1/delete", HandleDelete(resourceManager))
}

33
makefile Normal file
View File

@ -0,0 +1,33 @@
# scripts for building account tracker backend
# requires go 1.18+ and git installed
VERSION := 1.1.0
serve:
go run ./...
setup:
go get
docker-dev:
docker image build -t registry.bbr-dev.info/resource-manager/backend:$(VERSION)-dev .
docker tag registry.bbr-dev.info/resource-manager/backend:$(VERSION)-dev registry.bbr-dev.info/resource-manager/backend:latest-dev
docker image push registry.bbr-dev.info/resource-manager/backend:$(VERSION)-dev
docker image push registry.bbr-dev.info/resource-manager/backend:latest-dev
docker-prod:
docker image build -t registry.bbr-dev.info/resource-manager/backend:$(VERSION) .
docker tag registry.bbr-dev.info/resource-manager/backend:$(VERSION) registry.bbr-dev.info/resource-manager/backend:latest
docker image push registry.bbr-dev.info/resource-manager/backend:$(VERSION)
docker image push registry.bbr-dev.info/resource-manager/backend:latest
release:
git tag $(VERSION)
git push origin $(VERSION)
test:
go test ./...
clean:
rm -rf resource-manager

Binary file not shown.

60
security/generator.go Normal file
View File

@ -0,0 +1,60 @@
package security
import (
"errors"
"github.com/golang-jwt/jwt"
"os"
"time"
)
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{}
atClaims["authorized"] = true
// user info
atClaims["path"] = tokenInfo.Path
// expiration
atClaims["exp"] = time.Now().Add(duration).Unix()
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
secret := getSecret()
return at.SignedString([]byte(secret))
}
func ParseToken(token string) (PresignToken, error) {
secret := getSecret()
parsedToken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return PresignToken{}, err
}
claims, ok := parsedToken.Claims.(jwt.MapClaims)
if ok {
return PresignToken{
Path: claims["path"].(string),
}, nil
} else {
return PresignToken{}, errors.New("failed parsing token")
}
}
func getSecret() string {
secret, exists := os.LookupEnv("JWT_SECRET")
if !exists {
panic("env variable JWT_SECRET not set")
}
return secret
}