├── .gitignore ├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── bin └── main.go ├── config.go ├── glide.lock ├── glide.yaml ├── pool.go ├── processor.go ├── run.sh ├── s3.go ├── server.go ├── source.go └── utils.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | vendor 26 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.4 4 | before_install: 5 | - sudo add-apt-repository ppa:rwky/graphicsmagick -y 6 | - sudo apt-get update -qq 7 | - sudo apt-get install graphicsmagick -y 8 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | 3 | WORKDIR /tmp 4 | ENV LIBVIPS_VERSION_MAJOR 8 5 | ENV LIBVIPS_VERSION_MINOR 2 6 | ENV LIBVIPS_VERSION_PATCH 3 7 | ENV LIBVIPS_VERSION $LIBVIPS_VERSION_MAJOR.$LIBVIPS_VERSION_MINOR.$LIBVIPS_VERSION_PATCH 8 | 9 | RUN \ 10 | # Install dependencies 11 | apt-get update && \ 12 | DEBIAN_FRONTEND=noninteractive apt-get install -y \ 13 | automake build-essential curl \ 14 | gobject-introspection gtk-doc-tools libglib2.0-dev libjpeg-turbo8-dev \ 15 | libpng12-dev libwebp-dev libtiff5-dev libexif-dev libxml2-dev swig libmagickwand-dev libpango1.0-dev \ 16 | libmatio-dev libopenslide-dev libcfitsio3-dev && \ 17 | 18 | # Build libvips 19 | curl -O http://www.vips.ecs.soton.ac.uk/supported/$LIBVIPS_VERSION_MAJOR.$LIBVIPS_VERSION_MINOR/vips-$LIBVIPS_VERSION.tar.gz && \ 20 | tar zvxf vips-$LIBVIPS_VERSION.tar.gz && \ 21 | cd vips-$LIBVIPS_VERSION && \ 22 | ./configure --enable-debug=no --without-python --without-orc --without-fftw --without-gsf $1 && \ 23 | make && \ 24 | make install && \ 25 | ldconfig && \ 26 | 27 | # Clean up 28 | apt-get remove -y curl automake build-essential && \ 29 | apt-get autoremove -y && \ 30 | apt-get autoclean && \ 31 | apt-get clean && \ 32 | rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* 33 | 34 | # gcc for cgo 35 | RUN apt-get update && apt-get install -y --no-install-recommends \ 36 | g++ \ 37 | gcc \ 38 | libc6-dev \ 39 | make \ 40 | pkg-config \ 41 | curl \ 42 | git \ 43 | && rm -rf /var/lib/apt/lists/* 44 | 45 | ENV GOLANG_VERSION 1.7.1 46 | ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz 47 | ENV GOLANG_DOWNLOAD_SHA256 43ad621c9b014cde8db17393dc108378d37bc853aa351a6c74bf6432c1bbd182 48 | 49 | RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \ 50 | && echo "$GOLANG_DOWNLOAD_SHA256 golang.tar.gz" | sha256sum -c - \ 51 | && tar -C /usr/local -xzf golang.tar.gz \ 52 | && rm golang.tar.gz 53 | 54 | ENV GOPATH /go 55 | ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH 56 | 57 | RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" 58 | 59 | RUN curl https://glide.sh/get | sh 60 | 61 | COPY . $GOPATH/src/github.com/plimble/ivy 62 | WORKDIR $GOPATH/src/github.com/plimble/ivy 63 | 64 | RUN glide install && \ 65 | ./run.sh -b 66 | 67 | CMD ["./app"] 68 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ivy [![godoc badge](http://godoc.org/github.com/plimble/ivy?status.png)](http://godoc.org/github.com/plimble/ivy) [![gocover badge](http://gocover.io/_badge/github.com/plimble/ivy?t=10)](http://gocover.io/github.com/plimble/ivy) [![Build Status](https://api.travis-ci.org/plimble/ivy.svg?branch=master&t=10)](https://travis-ci.org/plimble/ivy) [![Go Report Card](http://goreportcard.com/badge/plimble/ivy?t=10)](http:/goreportcard.com/report/plimble/ivy) 2 | ========= 3 | 4 | Assets & Image processing on the fly by libvips 5 | 6 | ### Installation 7 | `go get -u github.com/plimble/ivy` 8 | 9 | #####libvips 10 | 11 | OSX 12 | ```shell 13 | brew install pkg-config 14 | brew tap homebrew/science 15 | brew install vips 16 | ``` 17 | 18 | ### Documentation 19 | - [GoDoc](http://godoc.org/github.com/plimble/ivy) 20 | 21 | ### Sources 22 | 23 | ##### File System 24 | 25 | ```go 26 | source := ivy.NewFileSystemSource("/path/to/asset") 27 | cache := ivy.NewFileSystemCache("/path/to/cache") 28 | processor := ivy.NewGMProcessor() 29 | 30 | config := &ivy.Config{ 31 | IsDevelopment: false, //If false, Enable cache 32 | HTTPCache: 66000, //If > 0, Enable HTTP Cache 33 | } 34 | 35 | iv := ivy.New(source, cache, processor, config) 36 | ``` 37 | 38 | ##### AWS S3 39 | 40 | ```go 41 | source := ivy.NewS3Source("accessKey", "secretKey") 42 | cache := ivy.NewFileSystemCache("/path/to/cache") 43 | processor := ivy.NewGMProcessor() 44 | 45 | config := &ivy.Config{ 46 | IsDevelopment: false, //If false, Enable cache 47 | HTTPCache: 66000, //If > 0, Enable HTTP Cache 48 | } 49 | 50 | iv := ivy.New(source, cache, processor, config) 51 | ``` 52 | 53 | ### Cache 54 | 55 | You can use file system for caching or set `nil` for CDN like Cloudfront or disable caching 56 | 57 | 58 | ### Server 59 | 60 | You can run built-in server (ivy folder) or implement in your server 61 | 62 | For more config 63 | 64 | ```shell 65 | ./ivy -h 66 | 67 | NAME: 68 | Ivy - Assets & Image processing on the fly 69 | 70 | USAGE: 71 | Ivy [global options] command [command options] [arguments...] 72 | 73 | COMMANDS: 74 | help, h Shows a list of commands or help for one command 75 | 76 | GLOBAL OPTIONS: 77 | --url, -u ":4900" server port 78 | --httpcache, -t "0" if cache enable this is http cache in second 79 | --cache, -c enable cache, specific eg, file 80 | --source, -s "file" source of image eg, file, s3 81 | --s3key if source is s3, AWS S3 access key [$AWS_ACCESS_KEY] 82 | --s3secret if source is s3, AWS S3 secret key [$AWS_SECRET_KEY] 83 | --sourceroot if source is file, specific root path of image 84 | --cacheroot if cache is file, specific root path of cache 85 | --help, -h show help 86 | --version, -v print the version 87 | ``` 88 | 89 | ##### [Ace](https://github.com/plimble/ace) Example 90 | 91 | ```go 92 | a := ace.New() 93 | a.GET("/:bucket/:params/*path", func(c *ace.C) { 94 | iv.Get( 95 | c.Params.ByName("bucket"), 96 | c.Params.ByName("params"), 97 | c.Params.ByName("path"), 98 | c.Writer, 99 | c.Request, 100 | ) 101 | }) 102 | 103 | a.Run(":3000") 104 | ``` 105 | 106 | ###Customs 107 | 108 | ##### Custom Source 109 | ```go 110 | type Source interface { 111 | Load(bucket string, filename string) (*bytes.Buffer, error) 112 | GetFilePath(bucket string, filename string) string 113 | } 114 | ``` 115 | 116 | ##### Custom Cache 117 | ```go 118 | type Cache interface { 119 | Save(bucket, filename string, params *Params, file []byte) error 120 | Load(bucket, filename string, params *Params) (*bytes.Buffer, error) 121 | Delete(bucket, filename string, params *Params) error 122 | Flush(bucket string) error 123 | } 124 | ``` 125 | 126 | ##### Custom Processor 127 | ```go 128 | type Processor interface { 129 | Process(params *Params, path string, file *bytes.Buffer) (*bytes.Buffer, error) 130 | } 131 | ``` 132 | 133 | ### Example Request Image 134 | 135 | Get image with original size or non image 136 | 137 | ```url 138 | http://localhost:3000/bucket/_/test.jpg 139 | http://localhost:3000/bucket/0/test.jpg 140 | ``` 141 | Original image | After image 142 | --- | --- 143 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/4cm.jpg) 144 | 145 | 146 | #####Resize 100x100 147 | ``` 148 | http://localhost:3000/bucket/r_100x100/test.jpg 149 | ``` 150 | Original image | After image 151 | --- | --- 152 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/4cv.jpg) 153 | 154 | 155 | #####Resize width 100px aspect ratio 156 | ```url 157 | http://localhost:3000/bucket/r_100x0/test.jpg 158 | ``` 159 | Original image | After image 160 | --- | --- 161 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/4cn.jpg) 162 | 163 | 164 | #####Crop image 200x200 with default gravity (NorthWest) 165 | ```url 166 | http://localhost:3000/bucket/c_200x200/test.jpg 167 | ``` 168 | Original image | After image 169 | --- | --- 170 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/3kc.jpg) 171 | 172 | 173 | #####Crop with gravity East image 200x200 174 | ```url 175 | http://localhost:3000/bucket/c_200x200,g_e/test.jpg 176 | ``` 177 | Original image | After image 178 | --- | --- 179 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/3k4.jpg) 180 | 181 | 182 | #####Resize 400x400 then crop 200x200 and gravity center 183 | ```url 184 | http://localhost:3000/bucket/r_400x400,c_200x200,g_c/test.jpg 185 | ``` 186 | Original image | After image 187 | --- | --- 188 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/3kd.jpg) 189 | 190 | 191 | #####Quality 100 192 | ```url 193 | http://localhost:3000/bucket/q_100/test.jpg 194 | ``` 195 | Original image | After image 196 | --- | --- 197 | ![before](http://postto.me/18/4cm.jpg) | ![after](http://postto.me/18/3k5.jpg) 198 | 199 | 200 | 201 | ###Params Table 202 | 203 | | Param | Description | 204 | |---------------------|----------------------------------------| 205 | | r_{width}x{height} | Resize image, if 0 is aspect ratio | 206 | | c_{width}x{height} | Crop image | 207 | | g_{direction} | Gravity image | 208 | | q_{quality} | Quality image maximum 100 | 209 | 210 | ###Gravity position 211 | 212 | | Param | Description | 213 | |-------|----------------------------------------| 214 | | nw | North West | 215 | | n | North | 216 | | ne | North East | 217 | | w | West | 218 | | c | Center | 219 | | e | East | 220 | | sw | South West | 221 | | s | South | 222 | | se | South East | 223 | 224 | 225 | ###Contributing 226 | 227 | If you'd like to help out with the project. You can put up a Pull Request. 228 | 229 | -------------------------------------------------------------------------------- /bin/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | log "github.com/Sirupsen/logrus" 5 | "github.com/kataras/iris" 6 | "github.com/plimble/ivy" 7 | ) 8 | 9 | func main() { 10 | config, err := ivy.GetConfig() 11 | if err != nil { 12 | panic(err) 13 | } 14 | 15 | server, err := ivy.NewServer(config) 16 | if err != nil { 17 | panic(err) 18 | } 19 | 20 | log.Infof("Iris %s Running at %s", iris.Version, config.Addr) 21 | 22 | server.Listen(config.Addr) 23 | } 24 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import "github.com/plimble/envconfig" 4 | 5 | var ( 6 | config *Config 7 | ) 8 | 9 | type Config struct { 10 | Addr string `default:":20000" required:"true"` 11 | SourceAwsId string 12 | SourceAwsSecret string 13 | SourceAwsS3Bucket string 14 | SourceAwsS3Region string 15 | } 16 | 17 | func GetConfig() (*Config, error) { 18 | config := &Config{} 19 | err := envconfig.Process("ivy", config) 20 | 21 | return config, err 22 | } 23 | -------------------------------------------------------------------------------- /glide.lock: -------------------------------------------------------------------------------- 1 | hash: 4f1f189ea1668e96dee3f5d1268c7532b3c42c1b96fde53f0cfd2477effc7760 2 | updated: 2016-09-28T21:20:00.323550762+07:00 3 | imports: 4 | - name: github.com/ajg/form 5 | version: 523a5da1a92f01b01f840b61689c0340a0243532 6 | - name: github.com/aws/aws-sdk-go 7 | version: 87a2cff6c2601f69429f336067d1deb7f713d1c6 8 | subpackages: 9 | - aws 10 | - aws/awserr 11 | - aws/awsutil 12 | - aws/client 13 | - aws/client/metadata 14 | - aws/corehandlers 15 | - aws/credentials 16 | - aws/credentials/ec2rolecreds 17 | - aws/credentials/endpointcreds 18 | - aws/credentials/stscreds 19 | - aws/defaults 20 | - aws/ec2metadata 21 | - aws/request 22 | - aws/session 23 | - aws/signer/v4 24 | - private/endpoints 25 | - private/protocol 26 | - private/protocol/query 27 | - private/protocol/query/queryutil 28 | - private/protocol/rest 29 | - private/protocol/restxml 30 | - private/protocol/xml/xmlutil 31 | - private/waiter 32 | - service/s3 33 | - service/s3/s3iface 34 | - service/s3/s3manager 35 | - service/sts 36 | - name: github.com/davecgh/go-spew 37 | version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d 38 | subpackages: 39 | - spew 40 | - name: github.com/fatih/structs 41 | version: dc3312cb1a4513a366c4c9e622ad55c32df12ed3 42 | - name: github.com/gavv/gojsondiff 43 | version: 36046c6e558e7f854ebd3fd97d1e9812ebe8709b 44 | subpackages: 45 | - formatter 46 | - name: github.com/gavv/httpexpect 47 | version: 445f10d0ea3df01f78f350cbb01dd4a360d56d93 48 | - name: github.com/gavv/monotime 49 | version: 259cd7b345f5aa080eff16f21d7866ef7dea9528 50 | - name: github.com/go-ini/ini 51 | version: 6e4869b434bd001f6983749881c7ead3545887d8 52 | - name: github.com/google/go-github 53 | version: 94a3cd9f531888fe4a03487276c0adb887428e77 54 | subpackages: 55 | - github 56 | - name: github.com/google/go-querystring 57 | version: 9235644dd9e52eeae6fa48efd539fdc351a0af53 58 | subpackages: 59 | - query 60 | - name: github.com/gorilla/websocket 61 | version: 2d1e4548da234d9cb742cc3628556fef86aafbac 62 | - name: github.com/h2non/bimg 63 | version: 45e4ce45fd138eccd91760d527db901f35f8d000 64 | - name: github.com/hashicorp/go-version 65 | version: deeb027c13a95d56c7585df3fe29207208c6706e 66 | - name: github.com/imdario/mergo 67 | version: 50d4dbd4eb0e84778abe37cefef140271d96fade 68 | - name: github.com/imkira/go-interpol 69 | version: 5accad8134979a6ac504d456a6c7f1c53da237ca 70 | - name: github.com/iris-contrib/formBinder 71 | version: 32009bbc76eda475a6d1eb24787d1fdcdaf80e71 72 | - name: github.com/iris-contrib/letsencrypt 73 | version: 2a9ed749d24d8d2a8ece891b7b6242f0b3192d62 74 | - name: github.com/iris-contrib/websocket 75 | version: 5822d5825abd388d92ecdf42024dc2ad1a5ce1ba 76 | - name: github.com/jmespath/go-jmespath 77 | version: bd40a432e4c76585ef6b72d3fd96fb9b6dc7b68d 78 | - name: github.com/kardianos/osext 79 | version: c2c54e542fb797ad986b31721e1baedf214ca413 80 | - name: github.com/kardianos/service 81 | version: 7a88211485df5d3da6ec68f4f8a5ff9bec297327 82 | - name: github.com/kataras/go-errors 83 | version: 0f977b82cc78d5d31bb75fb6f903ad9e852c8bbd 84 | - name: github.com/kataras/go-fs 85 | version: 4aa4a72b7801e6a9486568dfd21d812354d27899 86 | - name: github.com/kataras/go-options 87 | version: 23b556c1b935c594ec6d71ff81ead4dbeec3aa8d 88 | - name: github.com/kataras/go-serializer 89 | version: 959c7b8c27718d5a7a3d83ad05d677ae234e1152 90 | subpackages: 91 | - data 92 | - json 93 | - jsonp 94 | - markdown 95 | - text 96 | - xml 97 | - name: github.com/kataras/go-sessions 98 | version: 869154f9f1046ba798da02a88aa1c5dfa4117164 99 | - name: github.com/kataras/go-template 100 | version: b99158ff1faa4a4857d17c3bb9bf2a1e7beab3be 101 | subpackages: 102 | - html 103 | - name: github.com/kataras/go-websocket 104 | version: bfb19718e7dca519a9eb48e277c33f45a24b63b8 105 | - name: github.com/kataras/iris 106 | version: def1643c691b56ee4828d781ba6a41fcce5035eb 107 | subpackages: 108 | - context 109 | - utils 110 | - name: github.com/klauspost/compress 111 | version: d0763f13d86e630f5d3ea9fa848a6ecc68255297 112 | subpackages: 113 | - flate 114 | - gzip 115 | - zlib 116 | - name: github.com/klauspost/cpuid 117 | version: 09cded8978dc9e80714c4d85b0322337b0a1e5e0 118 | - name: github.com/klauspost/crc32 119 | version: 19b0b332c9e4516a6370a0456e6182c3b5036720 120 | - name: github.com/microcosm-cc/bluemonday 121 | version: 7d0cad0ac7ef5e3afd74816444b44b56327422a4 122 | - name: github.com/miekg/dns 123 | version: db96a2b759cdef4f11a34506a42eb8d1290c598e 124 | - name: github.com/moul/http2curl 125 | version: b1479103caacaa39319f75e7f57fc545287fca0d 126 | - name: github.com/plimble/envconfig 127 | version: ca2b88252414b92b1f5eaba41c619b9cc8c4c2c8 128 | - name: github.com/plimble/errors 129 | version: cdd78537482af7df73a9fdc8edd311478e2a3b05 130 | - name: github.com/pmezard/go-difflib 131 | version: d8ed2627bdf02c080bf22230dbb337003b7aba2d 132 | subpackages: 133 | - difflib 134 | - name: github.com/russross/blackfriday 135 | version: 35eb537633d9950afc8ae7bdf0edb6134584e9fc 136 | - name: github.com/sergi/go-diff 137 | version: 97b2266dfe4bd4ea1b81a463322f04f8b724801e 138 | subpackages: 139 | - diffmatchpatch 140 | - name: github.com/shurcooL/sanitized_anchor_name 141 | version: 1dba4b3954bc059efc3991ec364f9f9a35f597d2 142 | - name: github.com/Sirupsen/logrus 143 | version: 3ec0642a7fb6488f65b06f9040adc67e3990296a 144 | - name: github.com/stretchr/testify 145 | version: d77da356e56a7428ad25149ca77381849a6a5232 146 | subpackages: 147 | - assert 148 | - require 149 | - name: github.com/tj/go-debug 150 | version: ff4a55a20a86994118644bbddc6a216da193cc13 151 | - name: github.com/valyala/bytebufferpool 152 | version: e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7 153 | - name: github.com/valyala/fasthttp 154 | version: d03a22720925ae6fd6f81db717d9288fc2771d79 155 | subpackages: 156 | - fasthttpadaptor 157 | - fasthttputil 158 | - name: github.com/xeipuuv/gojsonpointer 159 | version: e0fe6f68307607d540ed8eac07a342c33fa1b54a 160 | - name: github.com/xeipuuv/gojsonreference 161 | version: e02fc20de94c78484cd5ffb007f8af96be030a45 162 | - name: github.com/xeipuuv/gojsonschema 163 | version: 00f9fafb54d2244d291b86ab63d12c38bd5c3886 164 | - name: github.com/xenolf/lego 165 | version: 82ac43327b01319544c050d5d78a4edeff9565d2 166 | subpackages: 167 | - acme 168 | - name: github.com/yalp/jsonpath 169 | version: 31a79c7593bb93eb10b163650d4a3e6ca190e4dc 170 | - name: github.com/yudai/golcs 171 | version: d1c525dea8ce39ea9a783d33cf08932305373f2c 172 | - name: golang.org/x/crypto 173 | version: 8e06e8ddd9629eb88639aba897641bff8031f1d3 174 | subpackages: 175 | - curve25519 176 | - ed25519 177 | - ed25519/internal/edwards25519 178 | - ocsp 179 | - ssh 180 | - ssh/terminal 181 | - name: golang.org/x/net 182 | version: 6d3beaea10370160dea67f5c9327ed791afd5389 183 | subpackages: 184 | - context 185 | - html 186 | - html/atom 187 | - publicsuffix 188 | - name: golang.org/x/sys 189 | version: 8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9 190 | subpackages: 191 | - unix 192 | - windows 193 | - windows/registry 194 | - windows/svc 195 | - windows/svc/eventlog 196 | - windows/svc/mgr 197 | - name: golang.org/x/time 198 | version: 2bc1b4fbcc7c3eda9a26f6e523cac1196c6fecd7 199 | subpackages: 200 | - rate 201 | - name: gopkg.in/square/go-jose.v1 202 | version: aa2e30fdd1fe9dd3394119af66451ae790d50e0d 203 | subpackages: 204 | - cipher 205 | - json 206 | testImports: [] 207 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/plimble/ivy 2 | import: 3 | - package: github.com/Sirupsen/logrus 4 | - package: github.com/aws/aws-sdk-go 5 | subpackages: 6 | - aws 7 | - aws/awserr 8 | - aws/credentials 9 | - aws/session 10 | - service/s3 11 | - service/s3/s3manager 12 | - package: github.com/h2non/bimg 13 | - package: github.com/kataras/iris 14 | - package: github.com/plimble/envconfig 15 | - package: github.com/plimble/errors 16 | -------------------------------------------------------------------------------- /pool.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import ( 4 | "bytes" 5 | ) 6 | 7 | type bufferPool struct { 8 | list chan *bytes.Buffer 9 | } 10 | 11 | func newBufferPool(poolSize int) *bufferPool { 12 | b := &bufferPool{ 13 | list: make(chan *bytes.Buffer, poolSize), 14 | } 15 | 16 | return b 17 | } 18 | 19 | func (p *bufferPool) Put(b *bytes.Buffer) { 20 | b.Reset() 21 | select { 22 | case p.list <- b: 23 | default: 24 | } 25 | } 26 | 27 | func (p *bufferPool) Get() *bytes.Buffer { 28 | select { 29 | case b := <-p.list: 30 | return b 31 | default: 32 | return &bytes.Buffer{} 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /processor.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import ( 4 | "github.com/h2non/bimg" 5 | "github.com/plimble/errors" 6 | ) 7 | 8 | type Processor interface { 9 | Process(path string, opt *bimg.Options) ([]byte, bimg.ImageType, error) 10 | } 11 | 12 | type processor struct { 13 | source Source 14 | } 15 | 16 | func NewProcessor(source Source) Processor { 17 | return &processor{source} 18 | } 19 | 20 | func (p *processor) Process(path string, opt *bimg.Options) ([]byte, bimg.ImageType, error) { 21 | data, err := p.source.Get(path) 22 | if err != nil { 23 | return nil, 0, err 24 | } 25 | 26 | result, err := bimg.NewImage(data).Process(*opt) 27 | imgType := bimg.DetermineImageType(result) 28 | 29 | return result, imgType, errors.InternalServerErrorErr(err) 30 | } 31 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | help() { 6 | echo ' -g\trun go generate [PATH] default $(glide novendor)' 7 | echo ' -it\trun integration test' 8 | echo ' -t\trun unit test [PATH] default $(glide novendor)' 9 | echo ' -b\tcreate binary' 10 | echo ' -r\trun docker-compose' 11 | echo ' -i\trun godep save' 12 | } 13 | 14 | if [ -z $1 ]; then 15 | echo 'Please action' 16 | help 17 | exit 0 18 | fi 19 | 20 | case "$1" in 21 | -g) 22 | if [ -z $2 ]; then 23 | set -x 24 | go generate $(glide novendor) 25 | else 26 | shift 27 | set -x 28 | go generate $@ 29 | fi 30 | ;; 31 | -it) 32 | if [ -z $2 ]; then 33 | set -x 34 | go test -v -tags integration $(glide novendor) 35 | else 36 | shift 37 | set -x 38 | go test -v -tags integration $@ 39 | fi 40 | ;; 41 | -b) 42 | set -x 43 | go build -o app ./bin 44 | ;; 45 | -r) 46 | set -x 47 | go run *.go 48 | ;; 49 | -t) 50 | if [ -z $2 ]; then 51 | set -x 52 | go test -v $(glide novendor) 53 | else 54 | shift 55 | set -x 56 | go test -v $@ 57 | fi 58 | ;; 59 | -h | --help) 60 | help 61 | ;; 62 | *) 63 | help 64 | ;; 65 | esac 66 | 67 | exit $? 68 | -------------------------------------------------------------------------------- /s3.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import ( 4 | "github.com/aws/aws-sdk-go/aws" 5 | "github.com/aws/aws-sdk-go/aws/awserr" 6 | "github.com/aws/aws-sdk-go/aws/credentials" 7 | "github.com/aws/aws-sdk-go/aws/session" 8 | "github.com/aws/aws-sdk-go/service/s3" 9 | "github.com/aws/aws-sdk-go/service/s3/s3manager" 10 | "github.com/plimble/errors" 11 | ) 12 | 13 | type S3 struct { 14 | s3 *s3manager.Downloader 15 | bucket string 16 | } 17 | 18 | func NewS3(awsId, awsSecret, s3Bucket, s3Region string) Source { 19 | cred := credentials.NewStaticCredentials(awsId, awsSecret, "") 20 | session := session.New(&aws.Config{Region: aws.String(s3Region), Credentials: cred}) 21 | 22 | s3manager.NewDownloader(session) 23 | 24 | return &S3{s3manager.NewDownloader(session), s3Bucket} 25 | } 26 | 27 | func (s *S3) Get(path string) ([]byte, error) { 28 | b := aws.NewWriteAtBuffer(nil) 29 | _, err := s.s3.Download(b, &s3.GetObjectInput{ 30 | Bucket: aws.String(s.bucket), 31 | Key: aws.String(path), 32 | }) 33 | 34 | if err != nil { 35 | cerr := err.(awserr.Error) 36 | if cerr.Code() == "NoSuchKey" { 37 | return nil, errors.NotFound("not found") 38 | } 39 | 40 | return nil, errors.InternalServerErrorErr(err) 41 | } 42 | 43 | return b.Bytes(), nil 44 | } 45 | -------------------------------------------------------------------------------- /server.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import ( 4 | "github.com/h2non/bimg" 5 | "github.com/kataras/iris" 6 | "github.com/plimble/errors" 7 | ) 8 | 9 | func NewServer(config *Config) (*iris.Framework, error) { 10 | ir := iris.New( 11 | iris.OptionDisableBanner(true), 12 | ) 13 | 14 | source := NewS3(config.SourceAwsId, config.SourceAwsSecret, config.SourceAwsS3Bucket, config.SourceAwsS3Region) 15 | 16 | p := NewProcessor(source) 17 | 18 | ir.Get("/*path", handler(p)) 19 | 20 | return ir, nil 21 | } 22 | 23 | func handler(p Processor) iris.HandlerFunc { 24 | return func(ctx *iris.Context) { 25 | path := ctx.Param("path") 26 | if path == "" { 27 | ctx.SetStatusCode(404) 28 | return 29 | } 30 | 31 | opt := &bimg.Options{} 32 | opt.Width, opt.Height = splitWidthHeightString(ctx.FormValueString("r")) 33 | opt.Crop = getBoolFromString(ctx.FormValueString("c")) 34 | 35 | g := getGravityFromString(ctx.FormValueString("g")) 36 | if g > -1 { 37 | opt.Gravity = g 38 | } 39 | 40 | f := getBoolFromString(ctx.FormValueString("f")) 41 | if f { 42 | opt.Force = true 43 | } else { 44 | opt.Embed = true 45 | } 46 | 47 | opt.Quality = stringToInt(ctx.FormValueString("q")) 48 | opt.Type = stringToImageType(ctx.FormValueString("t")) 49 | 50 | img, imgType, err := p.Process(path, opt) 51 | if err != nil { 52 | status, err := errors.ErrorStatus(err) 53 | ctx.Text(status, err.Error()) 54 | return 55 | } 56 | 57 | ctx.Response.Header.SetContentType(getContentType(imgType)) 58 | ctx.Response.Header.SetContentLength(len(img)) 59 | ctx.Response.AppendBody(img) 60 | ctx.SetStatusCode(200) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /source.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | type Source interface { 4 | Get(path string) ([]byte, error) 5 | } 6 | -------------------------------------------------------------------------------- /utils.go: -------------------------------------------------------------------------------- 1 | package ivy 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | 7 | "github.com/h2non/bimg" 8 | ) 9 | 10 | func stringToInt(s string) int { 11 | i, err := strconv.Atoi(s) 12 | if err != nil { 13 | return 0 14 | } 15 | 16 | return i 17 | } 18 | 19 | func splitWidthHeightString(s string) (int, int) { 20 | splits := strings.Split(s, "x") 21 | if len(splits) != 2 { 22 | return 0, 0 23 | } 24 | 25 | w, err := strconv.Atoi(splits[0]) 26 | if err != nil { 27 | w = 0 28 | } 29 | 30 | h, err := strconv.Atoi(splits[1]) 31 | if err != nil { 32 | h = 0 33 | } 34 | 35 | return w, h 36 | } 37 | 38 | func getGravityFromString(s string) bimg.Gravity { 39 | switch s { 40 | case "n": 41 | return bimg.GravityNorth 42 | case "e": 43 | return bimg.GravityEast 44 | case "s": 45 | return bimg.GravitySouth 46 | case "w": 47 | return bimg.GravityWest 48 | case "c": 49 | return bimg.GravityCentre 50 | default: 51 | return -1 52 | } 53 | } 54 | 55 | func getContentType(s bimg.ImageType) string { 56 | switch s { 57 | case bimg.JPEG: 58 | return "image/jpg" 59 | case bimg.PNG: 60 | return "image/png" 61 | case bimg.GIF: 62 | return "image/gif" 63 | case bimg.WEBP: 64 | return "image/webp" 65 | default: 66 | return "application/octet-stream" 67 | } 68 | } 69 | 70 | func getBoolFromString(s string) bool { 71 | if s == "true" || s == "1" { 72 | return true 73 | } 74 | 75 | return false 76 | } 77 | 78 | func stringToImageType(s string) bimg.ImageType { 79 | switch s { 80 | case "jpg": 81 | return bimg.JPEG 82 | case "png": 83 | return bimg.PNG 84 | case "gif": 85 | return bimg.GIF 86 | case "webp": 87 | return bimg.WEBP 88 | default: 89 | return bimg.UNKNOWN 90 | } 91 | } 92 | --------------------------------------------------------------------------------