Skip to content

Commit

Permalink
Merge pull request #77 from osspkg/add-images
Browse files Browse the repository at this point in the history
Add images
  • Loading branch information
markus621 authored Jan 12, 2024
2 parents c20632b + b85e376 commit dc3e9a6
Show file tree
Hide file tree
Showing 22 changed files with 248 additions and 73 deletions.
2 changes: 1 addition & 1 deletion app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions auth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
48 changes: 24 additions & 24 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use (
./errors
./examples
./geoip
./graphics
./iofile
./iosync
./ioutil
Expand Down
27 changes: 0 additions & 27 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -37,40 +34,18 @@ 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=
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=
Expand All @@ -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=
15 changes: 15 additions & 0 deletions graphics/go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions graphics/go.sum
Original file line number Diff line number Diff line change
@@ -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=
184 changes: 184 additions & 0 deletions graphics/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2023 Mikhail Knyazhev <[email protected]>. 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
}
Loading

0 comments on commit dc3e9a6

Please sign in to comment.