Refactor
This commit is contained in:
1 parent
bceaa3b6df
commit
937216bda6
11 files changed
+350
-213
No files matched your search
Vendored
+73
@@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewManager() Manager {
|
||||
if strings.Contains(os.Getenv("PROFILE"), "redis") {
|
||||
log.Println("Cache | using redis cache")
|
||||
return newRedisCache()
|
||||
} else {
|
||||
log.Println("Cache | using in memory cache")
|
||||
return newInMemCache()
|
||||
}
|
||||
}
|
||||
|
||||
type Manager interface {
|
||||
Set(key, value string, expiration time.Duration) error
|
||||
Get(key string) (string, error)
|
||||
Remove(key string)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/cache"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"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"
|
||||
"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)
|
||||
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(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"))},
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
uploader := s3manager.NewUploader(sess)
|
||||
downloader := s3manager.NewDownloader(sess)
|
||||
upload := make(chan UploadRequest, 20)
|
||||
manager := awsManager{
|
||||
cacheManager,
|
||||
upload,
|
||||
make(map[string][]onUpload),
|
||||
sync.Mutex{},
|
||||
expiration,
|
||||
uploader,
|
||||
downloader,
|
||||
}
|
||||
go manager.uploadRunner()
|
||||
return &manager
|
||||
}
|
||||
|
||||
type onUpload chan bool
|
||||
|
||||
type awsManager struct {
|
||||
cache cache.Manager
|
||||
requests chan UploadRequest
|
||||
currentlyUploading map[string][]onUpload
|
||||
uploadSync sync.Mutex
|
||||
expiration time.Duration
|
||||
uploader *s3manager.Uploader
|
||||
downloader *s3manager.Downloader
|
||||
}
|
||||
|
||||
func (a *awsManager) uploadRunner() {
|
||||
for req := range a.requests {
|
||||
status := true
|
||||
if !a.doesFileExist(req.Path) {
|
||||
status = a.upload(req.Path, req.Buffer.Bytes(), req.MimeType)
|
||||
}
|
||||
a.notifyOnUploadComplete(req.Path, status)
|
||||
}
|
||||
}
|
||||
func (a *awsManager) Upload(request UploadRequest) {
|
||||
a.addToCurrentlyUploading(request.Path)
|
||||
a.requests <- request
|
||||
}
|
||||
func (a *awsManager) Presign(ctx 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 there is no value in cache, presign url and add it to cache
|
||||
url, err := a.presign(path, a.expiration)
|
||||
if err != nil {
|
||||
a.cache.Set(path, url, a.expiration)
|
||||
}
|
||||
return url, err
|
||||
}
|
||||
func (a *awsManager) Download(ctx context.Context, path string) (file []byte, err error) {
|
||||
return a.download(path)
|
||||
}
|
||||
func (a *awsManager) Copy(from string, to string, overwrite bool) error {
|
||||
fromPath := strings.SplitN(from, "/", 2)
|
||||
toPath := strings.SplitN(to, "/", 2)
|
||||
output, err := a.downloader.S3.ListObjectsV2(&s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(fromPath[0]),
|
||||
Prefix: aws.String(fromPath[1]),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, obj := range output.Contents {
|
||||
if overwrite || !a.doesFileExist(strings.Replace(*obj.Key, fromPath[1], toPath[1], 1)) {
|
||||
_, err = a.downloader.S3.CopyObject(&s3.CopyObjectInput{
|
||||
Bucket: aws.String(toPath[0]),
|
||||
CopySource: aws.String(url.PathEscape(from)),
|
||||
Key: aws.String(toPath[1]),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (a *awsManager) Delete(path string) error {
|
||||
paths := strings.SplitN(path, "/", 2)
|
||||
_, err := a.uploader.S3.DeleteObject(&s3.DeleteObjectInput{
|
||||
Bucket: aws.String(paths[0]),
|
||||
Key: aws.String(paths[1]),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.cache.Remove(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *awsManager) addToCurrentlyUploading(path string) {
|
||||
a.uploadSync.Lock()
|
||||
log.Println("Manager | (" + path + ") added to upload list")
|
||||
a.currentlyUploading[path] = make([]onUpload, 0)
|
||||
a.uploadSync.Unlock()
|
||||
}
|
||||
func (a *awsManager) waitForUploadComplete(path string) bool {
|
||||
a.uploadSync.Lock()
|
||||
defer a.uploadSync.Unlock()
|
||||
if _, contains := a.currentlyUploading[path]; contains {
|
||||
upload := make(onUpload)
|
||||
a.currentlyUploading[path] = append(a.currentlyUploading[path], upload)
|
||||
return <-upload
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (a *awsManager) notifyOnUploadComplete(path string, status bool) {
|
||||
a.uploadSync.Lock()
|
||||
defer a.uploadSync.Unlock()
|
||||
|
||||
log.Println("Manager | (" + path + ") finished uploading, success: " + strconv.FormatBool(status))
|
||||
|
||||
for _, upload := range a.currentlyUploading[path] {
|
||||
upload <- status
|
||||
close(upload)
|
||||
}
|
||||
delete(a.currentlyUploading, path)
|
||||
}
|
||||
|
||||
func (a *awsManager) upload(path string, data []byte, mimeType string) bool {
|
||||
log.Println("Manager | (" + path + ") uploading")
|
||||
paths := strings.SplitN(path, "/", 2)
|
||||
_, err := a.uploader.Upload(&s3manager.UploadInput{
|
||||
Bucket: aws.String(paths[0]),
|
||||
ContentType: aws.String(mimeType),
|
||||
Key: aws.String(paths[1]),
|
||||
Body: bytes.NewReader(data),
|
||||
})
|
||||
return err == nil
|
||||
}
|
||||
func (a *awsManager) presign(path string, expiration time.Duration) (string, error) {
|
||||
paths := strings.SplitN(path, "/", 2)
|
||||
requestInput := s3.GetObjectInput{
|
||||
Bucket: aws.String(paths[0]),
|
||||
Key: aws.String(paths[1]),
|
||||
}
|
||||
request, _ := a.downloader.S3.GetObjectRequest(&requestInput)
|
||||
if request.Error != nil {
|
||||
log.Println("Manager | (" + path + ") failed presiging, cause: " + request.Error.Error())
|
||||
}
|
||||
return request.Presign(expiration)
|
||||
}
|
||||
func (a *awsManager) download(path string) ([]byte, error) {
|
||||
paths := strings.SplitN(path, "/", 2)
|
||||
requestInput := s3.GetObjectInput{
|
||||
Bucket: aws.String(paths[0]),
|
||||
Key: aws.String(paths[1]),
|
||||
}
|
||||
buf := aws.NewWriteAtBuffer([]byte{})
|
||||
_, err := a.downloader.Download(buf, &requestInput)
|
||||
if err != nil {
|
||||
log.Println("Manager | (" + path + ") failed downloading, cause: " + err.Error())
|
||||
}
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
func (a *awsManager) doesFileExist(path string) bool {
|
||||
paths := strings.SplitN(path, "/", 2)
|
||||
requestInput := s3.HeadObjectInput{
|
||||
Bucket: aws.String(paths[0]),
|
||||
Key: aws.String(paths[1]),
|
||||
}
|
||||
_, err := a.downloader.S3.HeadObject(&requestInput)
|
||||
if err != nil && err.Error() == s3.ErrCodeNoSuchKey {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"git.bbr-dev.info/brajkovic/resource_manager/domain/cache"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UploadRequest struct {
|
||||
Buffer *bytes.Buffer
|
||||
Path string
|
||||
MimeType string
|
||||
Overwrite bool
|
||||
}
|
||||
|
||||
func NewManager(cacheManager cache.Manager, expiration time.Duration) Manager {
|
||||
if strings.Contains(os.Getenv("PROFILE"), "s3") {
|
||||
log.Println("Manager | using S3 for data storage")
|
||||
return newS3Bucket(cacheManager, expiration)
|
||||
} else {
|
||||
return newLocalFolder(cacheManager, expiration)
|
||||
}
|
||||
}
|
||||
|
||||
type Manager interface {
|
||||
Upload(request UploadRequest)
|
||||
Download(ctx context.Context, path string) (file []byte, err error)
|
||||
Presign(ctx context.Context, path string) (string, error)
|
||||
Copy(from string, to string, overwrite bool) error
|
||||
Delete(path string) error
|
||||
}
|
||||
Reference in new issue
Block a user