From bceaa3b6dfb026774499d56a77558dd3f0acf796 Mon Sep 17 00:00:00 2001 From: Borna Rajkovic Date: Fri, 19 Nov 2021 19:14:43 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + cache/impl.go | 73 +++++++++++ cache/manager.go | 24 ++++ go.mod | 33 +++++ go.sum | 165 +++++++++++++++++++++++++ handlers.go | 109 +++++++++++++++++ image.go | 57 +++++++++ main.go | 59 +++++++++ resource/impl.go | 288 ++++++++++++++++++++++++++++++++++++++++++++ resource/manager.go | 35 ++++++ 10 files changed, 844 insertions(+) create mode 100644 .gitignore create mode 100644 cache/impl.go create mode 100644 cache/manager.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 handlers.go create mode 100644 image.go create mode 100644 main.go create mode 100644 resource/impl.go create mode 100644 resource/manager.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b198d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +resource_manager \ No newline at end of file diff --git a/cache/impl.go b/cache/impl.go new file mode 100644 index 0000000..80e1e91 --- /dev/null +++ b/cache/impl.go @@ -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) +} diff --git a/cache/manager.go b/cache/manager.go new file mode 100644 index 0000000..18d85a8 --- /dev/null +++ b/cache/manager.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2b6c483 --- /dev/null +++ b/go.mod @@ -0,0 +1,33 @@ +module git.bbr-dev.info/brajkovic/resource_manager + +go 1.17 + +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/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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9f89301 --- /dev/null +++ b/go.sum @@ -0,0 +1,165 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/anthonynsimon/bild v0.13.0 h1:mN3tMaNds1wBWi1BrJq0ipDBhpkooYfu7ZFSMhXt1C8= +github.com/anthonynsimon/bild v0.13.0/go.mod h1:tpzzp0aYkAsMi1zmfhimaDyX1xjn2OUc1AJZK/TF0AE= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/aws/aws-sdk-go v1.42.7 h1:Ee7QC4Y/eGebVGO/5IGN3fSXXSrheesZYYj2pYJG7Zk= +github.com/aws/aws-sdk-go v1.42.7/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= +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/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/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/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= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +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/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= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +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/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/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= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +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/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/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/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/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/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= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +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= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +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/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= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +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/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= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +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/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= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +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/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/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= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..b36a025 --- /dev/null +++ b/handlers.go @@ -0,0 +1,109 @@ +package main + +import ( + "bytes" + "encoding/base64" + "git.bbr-dev.info/brajkovic/resource_manager/resource" + "github.com/gin-gonic/gin" + "log" + "net/http" + "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) + } + } +} diff --git a/image.go b/image.go new file mode 100644 index 0000000..09436cf --- /dev/null +++ b/image.go @@ -0,0 +1,57 @@ +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 +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3452e37 --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "git.bbr-dev.info/brajkovic/resource_manager/cache" + "git.bbr-dev.info/brajkovic/resource_manager/resource" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" + "log" + "net/http" + "os" + "strings" + "time" +) + +func init() { + log.SetPrefix("[ReM] ") + godotenv.Load() +} + +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) + 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)) +} diff --git a/resource/impl.go b/resource/impl.go new file mode 100644 index 0000000..2d43fdf --- /dev/null +++ b/resource/impl.go @@ -0,0 +1,288 @@ +package resource + +import ( + "bytes" + "context" + "errors" + "git.bbr-dev.info/brajkovic/resource_manager/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) + 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"))}, + ) + 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 +} diff --git a/resource/manager.go b/resource/manager.go new file mode 100644 index 0000000..e2f8b33 --- /dev/null +++ b/resource/manager.go @@ -0,0 +1,35 @@ +package resource + +import ( + "bytes" + "context" + "git.bbr-dev.info/brajkovic/resource_manager/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 +}