diff options
| author | Max Resnick <max@ofmax.li> | 2020-04-19 23:13:51 -0700 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2020-05-21 22:24:27 -0700 |
| commit | 4e77ad5762539d8f9edf40d2668a998c38e834d3 (patch) | |
| tree | 91da3dfce1fa77ee04aa9045908f6142712d60f5 | |
| parent | ff1a5122005017d729fea29ec86851609d2991b8 (diff) | |
| download | iserv-4e77ad5762539d8f9edf40d2668a998c38e834d3.tar.gz | |
post w/ some meta working
Diffstat (limited to '')
| -rw-r--r-- | cmd/web/main.go | 32 | ||||
| -rw-r--r-- | go.mod | 20 | ||||
| -rw-r--r-- | go.sum | 141 | ||||
| -rw-r--r-- | internal/db/redis/image.go | 35 | ||||
| -rw-r--r-- | internal/db/redis/redis.go | 20 | ||||
| -rw-r--r-- | internal/image/handler.go | 71 | ||||
| -rw-r--r-- | internal/image/image_test.go | 152 | ||||
| -rw-r--r-- | internal/image/model.go | 8 | ||||
| -rw-r--r-- | internal/image/repo.go | 6 | ||||
| -rw-r--r-- | internal/image/service.go | 55 | ||||
| -rw-r--r-- | internal/image/t.pdf | bin | 0 -> 1700 bytes | |||
| -rw-r--r-- | internal/image/t.png | bin | 0 -> 275 bytes |
12 files changed, 540 insertions, 0 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..3f16dec --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + "net/http" + "os" + + "github.com/go-chi/chi" + + "git.ofmax.li/iserv/internal/db/redis" + "git.ofmax.li/iserv/internal/image" +) + +func main() { + connPool := redis.CreatePool("localhost:6379") + db := redis.NewRedisImageRepo(connPool) + + storagePath, err := os.Getwd() + if err != nil { + log.Fatal("couldn't find directory to write images to") + } + + imageService := image.NewService(db, storagePath) + imageHandler := image.NewHandler(imageService) + + r := chi.NewRouter() + + r.Get("/i", imageHandler.GetImage) + r.Post("/i", imageHandler.PostImage) + log.Print("starting imageserv") + log.Fatal(http.ListenAndServe(":8080", r)) +} @@ -0,0 +1,20 @@ +module git.ofmax.li/iserv + +go 1.14 + +require ( + github.com/alexedwards/scs/v2 v2.3.0 + github.com/brianvoe/gofakeit v3.18.0+incompatible // indirect + github.com/bwmarrin/snowflake v0.3.0 + github.com/gbrlsnchs/jwt/v3 v3.0.0-rc.2 // indirect + github.com/go-chi/chi v4.1.0+incompatible + github.com/gomodule/redigo v2.0.0+incompatible + github.com/matoous/go-nanoid v1.3.0 + github.com/muyo/sno v1.1.0 + github.com/rafaeljusto/redigomock v2.3.0+incompatible + github.com/speps/go-hashids v2.0.0+incompatible + github.com/stretchr/testify v1.5.1 + gitlab.com/grumps/environ v0.0.0-20190605051324-730aa37373e1 + golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect + google.golang.org/api v0.21.0 // indirect +) @@ -0,0 +1,141 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo= +cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alexedwards/scs v1.4.1 h1:/5L5a07IlqApODcEfZyMsu8Smd1S7Q4nBjEyKxIRTp0= +github.com/alexedwards/scs/v2 v2.3.0 h1:V8rtn2P5QGh8C9S7T/ikBo/AdA27vDoQJPbiAaOCmFg= +github.com/alexedwards/scs/v2 v2.3.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8= +github.com/brianvoe/gofakeit v1.2.0 h1:GGbzCqQx9ync4ObAUhRa3F/M73eL9VZL3X09WoTwphM= +github.com/brianvoe/gofakeit v3.18.0+incompatible h1:wDOmHc9DLG4nRjUVVaxA+CEglKOW72Y5+4WNxUIkjM8= +github.com/brianvoe/gofakeit v3.18.0+incompatible/go.mod h1:kfwdRA90vvNhPutZWfH7WPaDzUjz+CZFqG+rPkOjGOc= +github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= +github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/gbrlsnchs/jwt v1.1.0 h1:Gh2CoXcIfk8/LxV8ks0GDOmUDCpVIrw8Oa34Ozmw/10= +github.com/gbrlsnchs/jwt/v3 v3.0.0-rc.2 h1:3t7jvTkeQfk1FdP0noXSNiM6AdBokLz7QmZDmnCHAAA= +github.com/gbrlsnchs/jwt/v3 v3.0.0-rc.2/go.mod h1:AncDcjXz18xetI3A6STfXq2w+LuTx8pQ8bGEwRN8zVM= +github.com/go-chi/chi v1.0.0 h1:s/kv1cTXfivYjdKJdyUzNGyAWZ/2t7duW1gKn5ivu+c= +github.com/go-chi/chi v4.1.0+incompatible h1:ETj3cggsVIY2Xao5ExCu6YhEh5MD6JTfcBzS37R260w= +github.com/go-chi/chi v4.1.0+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= +github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gomodule/redigo v1.7.0 h1:ZKld1VOtsGhAe37E7wMxEDgAlGM5dvFY+DiOhSkhP9Y= +github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= +github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= +github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= +github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= +github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM= +github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/matoous/go-nanoid v1.3.0 h1:ynznZVSo9t0E8BTYLZx9geceRYZr8yLIrkOv3C/CU8M= +github.com/matoous/go-nanoid v1.3.0/go.mod h1:fvGBnhcQ+zcrB3qJIG32PAN11J/y1IYkGX2/VeHzuH0= +github.com/muyo/sno v1.1.0 h1:WLsHreJnNFtc29Z2grb9Y9A6nXLrDJS0KqpWZzTYGLg= +github.com/muyo/sno v1.1.0/go.mod h1:Hjr5WzLleYD7bJYvQGo5UK7hpE2kSvMbihc0bMWF2bU= +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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rafaeljusto/redigomock v2.3.0+incompatible h1:mW+5Fc1qpEgyPBIsT1ZdYAqoC/hRgq9bxUGiToBMr6A= +github.com/rafaeljusto/redigomock v2.3.0+incompatible/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY= +github.com/speps/go-hashids v1.0.0 h1:jdFC07PrExRM4Og5Ev4411Tox75aFpkC77NlmutadNI= +github.com/speps/go-hashids v2.0.0+incompatible h1:kSfxGfESueJKTx0mpER9Y/1XHl+FVQjtCqRyYcviFbw= +github.com/speps/go-hashids v2.0.0+incompatible/go.mod h1:P7hqPzMdnZOfyIk+xrlG1QaSMw+gCBdHKsBDnhpaZvc= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gitlab.com/grumps/environ v0.0.0-20190605051324-730aa37373e1 h1:Jjns+kL26DLZhyrxqBZHJn1ymX33jtfnmv2xufSXOaA= +gitlab.com/grumps/environ v0.0.0-20190605051324-730aa37373e1/go.mod h1:GPGILReyQ0kLhYL5BcbmvTUGCmHC+upQgUZBQMqvcDY= +go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= +go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190927123631-a832865fa7ad/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/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-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= +google.golang.org/api v0.21.0 h1:zS+Q/CJJnVlXpXQVIz+lH0ZT2lBuT2ac7XD8Y/3w6hY= +google.golang.org/api v0.21.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/db/redis/image.go b/internal/db/redis/image.go new file mode 100644 index 0000000..6814fb7 --- /dev/null +++ b/internal/db/redis/image.go @@ -0,0 +1,35 @@ +package redis + +import ( + "fmt" + "log" + + "github.com/gomodule/redigo/redis" + + "git.ofmax.li/iserv/internal/image" +) + +const V1FilePathFmt = "filepath:up/%s" + +// ImageRepo deps. for storage +type ImageRepo struct { + db *redis.Pool +} + +// NewRedisImageRepo +func NewRedisImageRepo(conn *redis.Pool) *ImageRepo { + return &ImageRepo{ + conn, + } +} + +func (r *ImageRepo) AddNewFile(filename string, meta *image.PostMeta, timeout int) error { + conn := r.db.Get() + defer conn.Close() + key := fmt.Sprintf(V1FilePathFmt, filename) + _, err := conn.Do("HMSET", redis.Args{}.Add(key).AddFlat(meta)...) + if err != nil { + log.Fatal(err) + } + return err +} diff --git a/internal/db/redis/redis.go b/internal/db/redis/redis.go new file mode 100644 index 0000000..d769bb4 --- /dev/null +++ b/internal/db/redis/redis.go @@ -0,0 +1,20 @@ +package redis + +import ( + "time" + + "github.com/gomodule/redigo/redis" + + "gitlab.com/grumps/environ" +) + +var redisServer = environ.GetEnv("AUTH_REDIS", "localhost:6379") + +// CreatePool redis connection pool +func CreatePool(addr string) *redis.Pool { + return &redis.Pool{ + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr) }, + } +} diff --git a/internal/image/handler.go b/internal/image/handler.go new file mode 100644 index 0000000..8988a58 --- /dev/null +++ b/internal/image/handler.go @@ -0,0 +1,71 @@ +package image + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" +) + +var fileTypes = map[string]string{ + "image/jpeg": "jpg", + "image/png": "png", +} + +// Handler image handler interface +type Handler interface { + GetImage(w http.ResponseWriter, r *http.Request) + PostImage(w http.ResponseWriter, r *http.Request) +} + +// NewHandler create image handler struct +func NewHandler(service Servicer) Handler { + return &imageHandler{service} +} + +type imageHandler struct { + service Servicer +} + +func (h *imageHandler) GetImage(w http.ResponseWriter, r *http.Request) { + log.Print("serving image") + http.ServeFile(w, r, "foo.jpg") +} + +// PostImage handler for creating an image post +func (h *imageHandler) PostImage(w http.ResponseWriter, r *http.Request) { + // max size + r.ParseMultipartForm(10 << 20) + file, handler, err := r.FormFile("file") + if err != nil { + log.Printf("%s", err) + } + defer file.Close() + fileBytes, err := ioutil.ReadAll(file) + if err != nil { + log.Printf("%s", err) + log.Printf("unsupported filetype") + w.WriteHeader(400) + w.Write([]byte("Incorrect Content Type")) + return + } + fileType := http.DetectContentType(fileBytes) + if fileType != handler.Header.Get("Content-Type") { + log.Printf("file type and content type do not match") + w.WriteHeader(400) + w.Write([]byte("Incorrect Content Type")) + return + } + extension, exists := fileTypes[fileType] + if !exists { + log.Printf("unsupported filetype") + w.WriteHeader(400) + w.Write([]byte("Incorrect Content Type")) + return + } + fileID, err := h.service.NewID() + fileName := fmt.Sprintf("%s.%s", fileID, extension) + h.service.AddFile(fileName, fileBytes) + w.WriteHeader(201) + w.Write([]byte("ok")) +} diff --git a/internal/image/image_test.go b/internal/image/image_test.go new file mode 100644 index 0000000..4a1aea9 --- /dev/null +++ b/internal/image/image_test.go @@ -0,0 +1,152 @@ +package image_test + +import ( + "bytes" + "fmt" + "io/ioutil" + "mime/multipart" + "net/http" + "net/http/httptest" + "net/textproto" + "os" + "path" + "path/filepath" + "strings" + "testing" + + "github.com/gomodule/redigo/redis" + "github.com/rafaeljusto/redigomock" + _ "github.com/stretchr/testify/mock" + + db "git.ofmax.li/iserv/internal/db/redis" + "git.ofmax.li/iserv/internal/image" +) + +// from go lang src https://golang.org/src/mime/multipart/writer.go +// there seems to be no way to set the content type. +var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") + +func escapeQuotes(s string) string { + + return quoteEscaper.Replace(s) +} + +// end go lang src + +func createHeader(fieldname, filename, contentType string) textproto.MIMEHeader { + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, escapeQuotes(fieldname), escapeQuotes(filename))) + h.Set("Content-Type", escapeQuotes(contentType)) + return h +} + +func prepareRequest(t *testing.T, filename, mimeType string) (*multipart.Writer, *bytes.Buffer, error) { + fileDir, _ := os.Getwd() + fileName := filename + filePath := path.Join(fileDir, fileName) + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + header := createHeader("file", filePath, mimeType) + part, err := writer.CreatePart(header) + if err != nil { + t.Errorf("wtf %s", err) + } + file, err := os.Open(filePath) + if err != nil { + t.Errorf("couldnt open: %s", err) + } + defer file.Close() + fileBytes, err := ioutil.ReadAll(file) + if err != nil { + t.Errorf("couldn't read buffer: %s", fileBytes) + } + + part.Write(fileBytes) + writer.Close() + return writer, body, err +} + +func TestImage(t *testing.T) { + // setup redis for tests + conn := redigomock.NewConn() + pool := &redis.Pool{ + // Return the same connection mock for each Get() call. + Dial: func() (redis.Conn, error) { return conn, nil }, + MaxIdle: 10, + } + conn.Command("HMSET", redigomock.NewAnyData(), redigomock.NewAnyData(), redigomock.NewAnyData(), redigomock.NewAnyData(), redigomock.NewAnyData(), redigomock.NewAnyData(), redigomock.NewAnyData()) + repo := db.NewRedisImageRepo(pool) + + // setup image package + imageBuildDir, err := ioutil.TempDir("", "test-images") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(imageBuildDir) + imageService := image.NewService(repo, imageBuildDir) + imageHandler := image.NewHandler(imageService) + handler := http.HandlerFunc(imageHandler.PostImage) + + // prep test with png + writer, body, err := prepareRequest(t, "t.png", "image/png") + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + r, err := http.NewRequest("POST", "http://example.com", body) + if err != nil { + t.Errorf("%s", err) + } + r.Header.Add("Content-Type", writer.FormDataContentType()) + handler.ServeHTTP(w, r) + response := w.Result() + if response.StatusCode != http.StatusCreated { + t.Errorf("image create status 201") + + } + + // incorrect mime + writer, body, err = prepareRequest(t, "t.pdf", "application/pdf") + if err != nil { + t.Fatal(err) + } + w = httptest.NewRecorder() + r, err = http.NewRequest("POST", "http://example.com", body) + if err != nil { + t.Fatal(err) + } + r.Header.Add("Content-Type", writer.FormDataContentType()) + handler.ServeHTTP(w, r) + response = w.Result() + if response.StatusCode != http.StatusBadRequest { + t.Errorf("image create not 400") + } + + // sneaky mimetype check + writer, body, err = prepareRequest(t, "t.png", "image/jpeg") + if err != nil { + t.Fatal(err) + } + w = httptest.NewRecorder() + r, err = http.NewRequest("POST", "http://example.com", body) + if err != nil { + t.Errorf("%s", err) + } + r.Header.Add("Content-Type", writer.FormDataContentType()) + handler.ServeHTTP(w, r) + response = w.Result() + if response.StatusCode != http.StatusBadRequest { + t.Errorf("image create not 400") + } + + // test image write path && ext + // TODO + pngs, err := filepath.Glob(filepath.Join(imageBuildDir)) + if err != nil { + t.Fatal(err) + } + if len(pngs) != 1 { + t.Error("expected only 1 image to be found") + } +} diff --git a/internal/image/model.go b/internal/image/model.go new file mode 100644 index 0000000..002ad8a --- /dev/null +++ b/internal/image/model.go @@ -0,0 +1,8 @@ +package image + +// PostMeta hold data for an image post +type PostMeta struct { + FilePath string `redis:"file_path"` + CreatedAt string `redis:"created_at"` + UserID string `redis:"user_id"` +} diff --git a/internal/image/repo.go b/internal/image/repo.go new file mode 100644 index 0000000..a65ad49 --- /dev/null +++ b/internal/image/repo.go @@ -0,0 +1,6 @@ +package image + +// Repo storage interface +type Repo interface { + AddNewFile(filename string, meta *PostMeta, timeout int) error +} diff --git a/internal/image/service.go b/internal/image/service.go new file mode 100644 index 0000000..dabbfe7 --- /dev/null +++ b/internal/image/service.go @@ -0,0 +1,55 @@ +package image + +import ( + "io/ioutil" + "log" + "path" + "time" + + "github.com/matoous/go-nanoid" +) + +// Servicer image management +type Servicer interface { + NewID() (string, error) + AddFile(filename string, fileBytes []byte) error +} + +// NewService new image service +func NewService(repo Repo, storagePath string) *Service { + return &Service{repo, + storagePath, + } +} + +// Service a container for working with images +type Service struct { + db Repo + storagePath string +} + +// NewID create an uniqueish ID +func (is *Service) NewID() (string, error) { + return gonanoid.Nanoid() +} + +// AddFile writes to disk, writes meta to db +func (is *Service) AddFile(fileName string, fileBytes []byte) error { + filePath := path.Join(is.storagePath, fileName) + if err := ioutil.WriteFile(filePath, fileBytes, 0750); err != nil { + log.Fatal(err) + return err + } + t := time.Now().UTC() + postMeta := &PostMeta{ + FilePath: fileName, + CreatedAt: t.Format(time.RFC3339), + UserID: "1", + } + is.db.AddNewFile(fileName, postMeta, 946080000) + if err := is.db.AddNewFile(fileName, postMeta, 946080000); err != nil { + log.Fatal(err) + return err + } + return nil +} diff --git a/internal/image/t.pdf b/internal/image/t.pdf Binary files differnew file mode 100644 index 0000000..e1eb5d1 --- /dev/null +++ b/internal/image/t.pdf diff --git a/internal/image/t.png b/internal/image/t.png Binary files differnew file mode 100644 index 0000000..50b107e --- /dev/null +++ b/internal/image/t.png |