From eccf6be48c9845cb1ae428ffa1dbe756b5c4a996 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Fri, 12 Jan 2024 15:44:04 +0300 Subject: [PATCH 1/2] add graphics + update vendors --- app/go.sum | 2 +- auth/go.sum | 4 +- examples/go.mod | 48 ++++++------ examples/go.sum | 2 +- go.sum | 2 +- go.work | 1 + go.work.sum | 27 ------- graphics/go.mod | 15 ++++ graphics/go.sum | 2 + graphics/images.go | 184 +++++++++++++++++++++++++++++++++++++++++++++ iofile/go.mod | 2 +- iofile/go.sum | 4 +- orm/go.sum | 2 +- ormmysql/go.sum | 2 +- ormpgsql/go.sum | 2 +- ormsqlite/go.sum | 2 +- 16 files changed, 238 insertions(+), 63 deletions(-) create mode 100644 graphics/go.mod create mode 100644 graphics/go.sum create mode 100644 graphics/images.go diff --git a/app/go.sum b/app/go.sum index 74b48c5..8cb8eac 100644 --- a/app/go.sum +++ b/app/go.sum @@ -8,7 +8,7 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= go.osspkg.com/algorithms v1.3.1 h1:Weh0Z4dMzHFRJTfMva/mU6Uk95wSRGwt1Saj2sfz3Wc= go.osspkg.com/algorithms v1.3.1/go.mod h1:kj+oVK7UDQlXMKleMTYzbEEpUAXC2tzRBtki7F5EbXc= diff --git a/auth/go.sum b/auth/go.sum index 7d1828d..9f1c48e 100644 --- a/auth/go.sum +++ b/auth/go.sum @@ -15,8 +15,8 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -go.osspkg.com/goppy/syscall v0.1.2 h1:g1qvlqHPBSDkF0un3gC2hpBqHZJfq9uwhSjmNRxbj0c= -go.osspkg.com/goppy/syscall v0.1.2/go.mod h1:8MsNFOYAzNzGI6FE+0hmqLINQ5cxVkhqHUyirzENG9A= +go.osspkg.com/goppy/syscall v0.1.3 h1:22P17r+oBbHuSKWjhayfjTZlcDcOPFqu1TrSLrMzeMA= +go.osspkg.com/goppy/syscall v0.1.3/go.mod h1:8MsNFOYAzNzGI6FE+0hmqLINQ5cxVkhqHUyirzENG9A= go.osspkg.com/static v1.4.0 h1:2uy4/11c0QP+QLMucKQZbAU+e6lhVHKw5dWJPTk/DBg= go.osspkg.com/static v1.4.0/go.mod h1:94YydVU3qUtb1J534486lpm+qg6CviQjqtxKlkpSppM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/examples/go.mod b/examples/go.mod index c67ad32..5bfa574 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -37,26 +37,26 @@ replace ( ) require ( - go.osspkg.com/goppy v0.15.14 - go.osspkg.com/goppy/app v0.1.10 - go.osspkg.com/goppy/auth v0.1.0 - go.osspkg.com/goppy/console v0.1.1 - go.osspkg.com/goppy/geoip v0.1.0 + go.osspkg.com/goppy v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/app v0.1.11 + go.osspkg.com/goppy/auth v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/console v0.1.2 + go.osspkg.com/goppy/geoip v0.0.0-00010101000000-000000000000 go.osspkg.com/goppy/iosync v0.1.5 go.osspkg.com/goppy/metrics v0.0.0-00010101000000-000000000000 - go.osspkg.com/goppy/ormmysql v0.1.0 - go.osspkg.com/goppy/ormpgsql v0.1.0 - go.osspkg.com/goppy/ormsqlite v0.1.0 + go.osspkg.com/goppy/ormmysql v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/ormpgsql v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/ormsqlite v0.0.0-00010101000000-000000000000 go.osspkg.com/goppy/plugins v0.1.2 - go.osspkg.com/goppy/routine v0.1.5 - go.osspkg.com/goppy/syscall v0.1.2 - go.osspkg.com/goppy/tcp v0.0.4 - go.osspkg.com/goppy/udp v0.0.2 - go.osspkg.com/goppy/unixsocket v0.1.0 - go.osspkg.com/goppy/web v0.1.10 - go.osspkg.com/goppy/ws v0.1.0 + go.osspkg.com/goppy/routine v0.1.6 + go.osspkg.com/goppy/syscall v0.1.3 + go.osspkg.com/goppy/tcp v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/udp v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/unixsocket v0.0.0-00010101000000-000000000000 + go.osspkg.com/goppy/web v0.1.11 + go.osspkg.com/goppy/ws v0.0.0-00010101000000-000000000000 go.osspkg.com/goppy/xc v0.1.1 - go.osspkg.com/goppy/xdns v0.1.0 + go.osspkg.com/goppy/xdns v0.0.0-00010101000000-000000000000 go.osspkg.com/goppy/xlog v0.1.7 ) @@ -81,15 +81,15 @@ require ( github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect go.osspkg.com/algorithms v1.3.1 // indirect - go.osspkg.com/goppy/encryption v0.1.5 // indirect - go.osspkg.com/goppy/env v0.1.1 // indirect - go.osspkg.com/goppy/errors v0.1.1 // indirect - go.osspkg.com/goppy/iofile v0.1.5 // indirect - go.osspkg.com/goppy/ioutil v0.1.2 // indirect - go.osspkg.com/goppy/orm v0.1.7 // indirect + go.osspkg.com/goppy/encryption v0.1.6 // indirect + go.osspkg.com/goppy/env v0.1.2 // indirect + go.osspkg.com/goppy/errors v0.1.2 // indirect + go.osspkg.com/goppy/iofile v0.1.6 // indirect + go.osspkg.com/goppy/ioutil v0.1.3 // indirect + go.osspkg.com/goppy/orm v0.1.8 // indirect go.osspkg.com/goppy/random v0.1.2 // indirect - go.osspkg.com/goppy/sqlcommon v0.1.7 // indirect - go.osspkg.com/goppy/xnet v0.1.2 // indirect + go.osspkg.com/goppy/sqlcommon v0.1.8 // indirect + go.osspkg.com/goppy/xnet v0.1.3 // indirect go.osspkg.com/static v1.4.0 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.20.0 // indirect diff --git a/examples/go.sum b/examples/go.sum index 1d9cc8e..f33c478 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -44,7 +44,7 @@ github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lne github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= go.osspkg.com/algorithms v1.3.1 h1:Weh0Z4dMzHFRJTfMva/mU6Uk95wSRGwt1Saj2sfz3Wc= go.osspkg.com/algorithms v1.3.1/go.mod h1:kj+oVK7UDQlXMKleMTYzbEEpUAXC2tzRBtki7F5EbXc= diff --git a/go.sum b/go.sum index 7ae9d10..2556bc7 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= go.osspkg.com/algorithms v1.3.1 h1:Weh0Z4dMzHFRJTfMva/mU6Uk95wSRGwt1Saj2sfz3Wc= go.osspkg.com/algorithms v1.3.1/go.mod h1:kj+oVK7UDQlXMKleMTYzbEEpUAXC2tzRBtki7F5EbXc= diff --git a/go.work b/go.work index 3d231b0..b77731a 100644 --- a/go.work +++ b/go.work @@ -13,6 +13,7 @@ use ( ./errors ./examples ./geoip + ./graphics ./iofile ./iosync ./ioutil diff --git a/go.work.sum b/go.work.sum index 5702c6c..42972bc 100644 --- a/go.work.sum +++ b/go.work.sum @@ -25,10 +25,7 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= @@ -37,8 +34,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= @@ -46,31 +41,11 @@ github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= -golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= -golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= -golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= -golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= google.golang.org/api v0.126.0 h1:q4GJq+cAdMAC7XP7njvQ4tvohGLiSlytuL4BQxbIZ+o= google.golang.org/api v0.126.0/go.mod h1:mBwVAtz+87bEN6CbA1GtZPDOqY2R5ONPqJeIlvyo4Aw= @@ -82,7 +57,5 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1: google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 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/graphics/go.mod b/graphics/go.mod new file mode 100644 index 0000000..ac95d6d --- /dev/null +++ b/graphics/go.mod @@ -0,0 +1,15 @@ +module go.osspkg.com/goppy/graphics + +go 1.18 + +replace ( + go.osspkg.com/goppy/errors => ../errors + go.osspkg.com/goppy/syscall => ../syscall +) + +require ( + go.osspkg.com/goppy/errors v0.1.2 + golang.org/x/image v0.15.0 +) + +require go.osspkg.com/goppy/syscall v0.1.3 // indirect diff --git a/graphics/go.sum b/graphics/go.sum new file mode 100644 index 0000000..939e5ae --- /dev/null +++ b/graphics/go.sum @@ -0,0 +1,2 @@ +golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8= +golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= diff --git a/graphics/images.go b/graphics/images.go new file mode 100644 index 0000000..b57a856 --- /dev/null +++ b/graphics/images.go @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2023 Mikhail Knyazhev . All rights reserved. + * Use of this source code is governed by a BSD-3-Clause license that can be found in the LICENSE file. + */ + +package graphics + +import ( + "crypto/sha1" + "fmt" + "image" + "image/jpeg" + "image/png" + "io" + "os" + "path/filepath" + "strings" + "sync" + + "go.osspkg.com/goppy/errors" + "golang.org/x/image/bmp" + "golang.org/x/image/draw" + "golang.org/x/image/tiff" + "golang.org/x/image/webp" +) + +var ( + ExtNotSupported = errors.New("ext is not supported") +) + +type ( + ImageScale struct { + folder string + cache map[string]ImageInfo + mux sync.Mutex + } + + ImageInfo struct { + Hash string + Origin string + Scale string + Thumb string + } +) + +var decoders = map[string]func(r io.Reader) (image.Image, error){ + ".jpg": jpeg.Decode, + ".jpeg": jpeg.Decode, + ".png": png.Decode, + ".webp": webp.Decode, + ".bmp": bmp.Decode, + ".tiff": tiff.Decode, +} + +func NewImageScale() *ImageScale { + return &ImageScale{ + folder: "", + cache: make(map[string]ImageInfo, 100), + } +} + +func (v *ImageScale) SetFolder(dir string) error { + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("create image folder: %w", err) + } + v.folder = dir + return nil +} + +func (v *ImageScale) Build(filename string, scale, thumb int) (*ImageInfo, error) { + var err error + img := &ImageInfo{} + + if img.Hash, err = v.getHash(filename); err != nil { + return nil, err + } + + v.mux.Lock() + i, ok := v.cache[img.Hash] + v.mux.Unlock() + if ok { + return &i, nil + } + + if img.Origin, err = v.resize(filename, img.Hash+".orig", 0); err != nil { + return nil, err + } + if img.Scale, err = v.resize(filename, img.Hash+".scale", scale); err != nil { + return nil, err + } + if img.Thumb, err = v.resize(filename, img.Hash+".thumb", thumb); err != nil { + return nil, err + } + + v.mux.Lock() + v.cache[img.Hash] = *img + v.mux.Unlock() + + return img, nil +} + +func (v *ImageScale) resize(filename, suffix string, width int) (string, error) { + src, name, err := v.readFile(filename) + if err != nil { + return "", err + } + x, y := v.scaleFactor(src.Bounds().Max.X, src.Bounds().Max.Y, width) + dst := image.NewNRGBA(image.Rect(0, 0, x, y)) + draw.CatmullRom.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil) + newFilename := fmt.Sprintf("%s-%s.png", name, suffix) + return newFilename, v.writeFile(v.folder+"/"+newFilename, dst) +} + +func (v *ImageScale) scaleFactor(oW, oH, width int) (int, int) { + if width == 0 { + return oW, oH + } + oWF, oHF := float64(oW), float64(oH) + nWidth := float64(width) + scale := oWF / nWidth + return int(oWF / scale), int(oHF / scale) +} + +func (v *ImageScale) writeFile(filename string, img image.Image) error { + file, err := os.Create(filename) + if err != nil { + return fmt.Errorf("write image `%s`: %w", filename, err) + } + if err = png.Encode(file, img); err != nil { + return errors.Wrap( + fmt.Errorf("encode image `%s`: %w", filename, err), + file.Close(), + ) + } + return file.Close() +} + +func (v *ImageScale) readFile(filename string) (image.Image, string, error) { + ext := filepath.Ext(filename) + dec, ok := decoders[ext] + if !ok { + return nil, "", ExtNotSupported + } + file, err := os.Open(filename) + if err != nil { + return nil, "", fmt.Errorf("read image `%s`: %w", filename, err) + } + img, err := dec(file) + if err != nil { + return nil, "", errors.Wrap( + fmt.Errorf("decode image `%s`: %w", filename, err), + file.Close(), + ) + } + fi, err := file.Stat() + if err != nil { + return nil, "", errors.Wrap( + fmt.Errorf("info image `%s`: %w", filename, err), + file.Close(), + ) + } + if err = file.Close(); err != nil { + return nil, "", fmt.Errorf("close image `%s`: %w", filename, err) + } + return img, strings.Replace(fi.Name(), ext, "", 1), nil +} + +func (v *ImageScale) getHash(filename string) (string, error) { + file, err := os.Open(filename) + if err != nil { + return "", fmt.Errorf("read image `%s`: %w", filename, err) + } + h := sha1.New() + if _, err = io.Copy(h, file); err != nil { + return "", errors.Wrap( + fmt.Errorf("calc hash image `%s`: %w", filename, err), + file.Close(), + ) + } + if err = file.Close(); err != nil { + return "", fmt.Errorf("close image `%s`: %w", filename, err) + } + return fmt.Sprintf("%x", h.Sum(nil)), nil +} diff --git a/iofile/go.mod b/iofile/go.mod index 21d41e1..df866d5 100644 --- a/iofile/go.mod +++ b/iofile/go.mod @@ -18,7 +18,7 @@ require ( require ( github.com/kr/pretty v0.3.1 // indirect - github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/rogpeppe/go-internal v1.10.0 // indirect go.osspkg.com/goppy/syscall v0.1.3 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/iofile/go.sum b/iofile/go.sum index d2f07f0..151a66c 100644 --- a/iofile/go.sum +++ b/iofile/go.sum @@ -8,8 +8,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/orm/go.sum b/orm/go.sum index b2cf95d..4300d46 100644 --- a/orm/go.sum +++ b/orm/go.sum @@ -6,7 +6,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/ormmysql/go.sum b/ormmysql/go.sum index 19c2362..a5c4615 100644 --- a/ormmysql/go.sum +++ b/ormmysql/go.sum @@ -6,7 +6,7 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/ormpgsql/go.sum b/ormpgsql/go.sum index 0d099ce..300ecf7 100644 --- a/ormpgsql/go.sum +++ b/ormpgsql/go.sum @@ -6,7 +6,7 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/ormsqlite/go.sum b/ormsqlite/go.sum index e38e71f..f9a9b6d 100644 --- a/ormsqlite/go.sum +++ b/ormsqlite/go.sum @@ -6,7 +6,7 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From b85e376fa0978547fbeb281307508a727ab112e5 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Fri, 12 Jan 2024 16:14:11 +0300 Subject: [PATCH 2/2] update vendors --- app/go.mod | 2 +- go.mod | 4 ++-- orm/go.mod | 2 +- ormmysql/go.mod | 4 ++-- ormpgsql/go.mod | 4 ++-- ormsqlite/go.mod | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/go.mod b/app/go.mod index 16a5ee7..a30435d 100644 --- a/app/go.mod +++ b/app/go.mod @@ -19,7 +19,7 @@ require ( go.osspkg.com/goppy/console v0.1.2 go.osspkg.com/goppy/env v0.1.2 go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/iofile v0.1.6 + go.osspkg.com/goppy/iofile v0.1.7 go.osspkg.com/goppy/iosync v0.1.5 go.osspkg.com/goppy/syscall v0.1.3 go.osspkg.com/goppy/xc v0.1.1 diff --git a/go.mod b/go.mod index 773fbda..1ea704e 100644 --- a/go.mod +++ b/go.mod @@ -17,11 +17,11 @@ replace ( ) require ( - go.osspkg.com/goppy/app v0.1.11 + go.osspkg.com/goppy/app v0.1.12 go.osspkg.com/goppy/console v0.1.2 go.osspkg.com/goppy/env v0.1.2 go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/iofile v0.1.6 + go.osspkg.com/goppy/iofile v0.1.7 go.osspkg.com/goppy/plugins v0.1.2 go.osspkg.com/goppy/xlog v0.1.7 gopkg.in/yaml.v3 v3.0.1 diff --git a/orm/go.mod b/orm/go.mod index f491a21..7590c59 100644 --- a/orm/go.mod +++ b/orm/go.mod @@ -15,7 +15,7 @@ replace ( require ( go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/iofile v0.1.6 + go.osspkg.com/goppy/iofile v0.1.7 go.osspkg.com/goppy/sqlcommon v0.1.8 go.osspkg.com/goppy/xc v0.1.1 go.osspkg.com/goppy/xlog v0.1.7 diff --git a/ormmysql/go.mod b/ormmysql/go.mod index b951a6e..63ed11a 100644 --- a/ormmysql/go.mod +++ b/ormmysql/go.mod @@ -19,7 +19,7 @@ replace ( require ( github.com/go-sql-driver/mysql v1.7.1 go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/orm v0.1.8 + go.osspkg.com/goppy/orm v0.1.9 go.osspkg.com/goppy/plugins v0.1.2 go.osspkg.com/goppy/routine v0.1.6 go.osspkg.com/goppy/sqlcommon v0.1.8 @@ -30,7 +30,7 @@ require ( require ( github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect - go.osspkg.com/goppy/iofile v0.1.6 // indirect + go.osspkg.com/goppy/iofile v0.1.7 // indirect go.osspkg.com/goppy/iosync v0.1.5 // indirect go.osspkg.com/goppy/syscall v0.1.3 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/ormpgsql/go.mod b/ormpgsql/go.mod index ef014ef..390f285 100644 --- a/ormpgsql/go.mod +++ b/ormpgsql/go.mod @@ -19,7 +19,7 @@ replace ( require ( github.com/lib/pq v1.10.9 go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/orm v0.1.8 + go.osspkg.com/goppy/orm v0.1.9 go.osspkg.com/goppy/plugins v0.1.2 go.osspkg.com/goppy/routine v0.1.6 go.osspkg.com/goppy/sqlcommon v0.1.8 @@ -30,7 +30,7 @@ require ( require ( github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect - go.osspkg.com/goppy/iofile v0.1.6 // indirect + go.osspkg.com/goppy/iofile v0.1.7 // indirect go.osspkg.com/goppy/iosync v0.1.5 // indirect go.osspkg.com/goppy/syscall v0.1.3 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/ormsqlite/go.mod b/ormsqlite/go.mod index 83d1613..6362691 100644 --- a/ormsqlite/go.mod +++ b/ormsqlite/go.mod @@ -19,8 +19,8 @@ replace ( require ( github.com/mattn/go-sqlite3 v1.14.19 go.osspkg.com/goppy/errors v0.1.2 - go.osspkg.com/goppy/iofile v0.1.6 - go.osspkg.com/goppy/orm v0.1.8 + go.osspkg.com/goppy/iofile v0.1.7 + go.osspkg.com/goppy/orm v0.1.9 go.osspkg.com/goppy/plugins v0.1.2 go.osspkg.com/goppy/routine v0.1.6 go.osspkg.com/goppy/sqlcommon v0.1.8