2019-01-08 23:05:20 +00:00
|
|
|
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-01-09 08:15:04 +00:00
|
|
|
"flag"
|
2019-01-08 23:05:20 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/davidbyttow/govips/pkg/vips"
|
|
|
|
)
|
|
|
|
|
2019-01-09 08:15:04 +00:00
|
|
|
var (
|
2019-07-31 05:06:38 +00:00
|
|
|
imgServer = flag.String("s", "https://images.example.com/", "URL prefix")
|
|
|
|
listenAddr = flag.String("l", "0.0.0.0:8080", "Listen address")
|
|
|
|
sizesFilter = flag.String("f", "", "Allowed sizes")
|
2019-01-09 08:15:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:05:20 +00:00
|
|
|
func resizeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Get the query parameters from the request URL
|
|
|
|
path := r.URL.EscapedPath()
|
|
|
|
parm := strings.Split(path, "/")
|
2019-01-13 20:09:53 +00:00
|
|
|
if len(parm) < 4 {
|
|
|
|
writeError(w, "", "", "error in arguments", http.StatusBadRequest)
|
2019-01-08 23:05:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:09:53 +00:00
|
|
|
method := parm[1]
|
|
|
|
args := parm[2]
|
|
|
|
imgPath := strings.Join(parm[3:], "/")
|
2019-01-08 23:05:20 +00:00
|
|
|
|
|
|
|
// Start fetching the image from the given url
|
2019-01-13 20:09:53 +00:00
|
|
|
resp, err := http.Get(*imgServer + imgPath)
|
2019-01-08 23:05:20 +00:00
|
|
|
if err != nil {
|
2019-01-13 20:09:53 +00:00
|
|
|
writeError(w, imgPath, method, "failed to fetch image", http.StatusNotFound)
|
2019-01-08 23:05:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2019-01-13 20:09:53 +00:00
|
|
|
switch method {
|
|
|
|
case "resize":
|
2019-07-31 05:06:38 +00:00
|
|
|
allowed := strings.Split(*sizesFilter, ",")
|
|
|
|
forbidden := true
|
|
|
|
if allowed[0] == "*" {
|
|
|
|
forbidden = false
|
|
|
|
} else {
|
|
|
|
for _, v := range allowed {
|
|
|
|
if size == v {
|
|
|
|
forbidden = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if forbidden {
|
|
|
|
writeError(w, imgPath, method, "size is not allowed", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:09:53 +00:00
|
|
|
size := strings.Split(args, "x")
|
|
|
|
if len(size) < 2 {
|
|
|
|
writeError(w, imgPath, method, "url, width, and height are required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
width := size[0]
|
|
|
|
height := size[1]
|
2019-01-08 23:05:20 +00:00
|
|
|
|
2019-01-13 20:09:53 +00:00
|
|
|
// Validate that all three required fields are present
|
|
|
|
if imgPath == "" || width == "" || height == "" {
|
|
|
|
writeError(w, imgPath, method, "url, width, and height are required", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert width and height to integers
|
|
|
|
iw, errW := strconv.Atoi(width)
|
|
|
|
ih, errH := strconv.Atoi(height)
|
|
|
|
if errW != nil || errH != nil {
|
|
|
|
writeError(w, imgPath, method, "width and height must be integers", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that a valid response was given
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
writeError(w, imgPath, method, fmt.Sprintf("failed to fetch image (%v)", resp.StatusCode), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// govips returns the output of the image as a []byte object. We don't need
|
|
|
|
// it since we are directly piping it to the ResponseWriter
|
|
|
|
_, _, err = vips.NewTransform().
|
|
|
|
Load(resp.Body).
|
2019-01-27 14:12:06 +00:00
|
|
|
ResizeStrategy(vips.ResizeStrategyAuto).
|
2019-01-13 20:09:53 +00:00
|
|
|
Resize(iw, ih).
|
|
|
|
Output(w).
|
|
|
|
Apply()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, imgPath, method, "failed to resize", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2019-01-27 14:12:06 +00:00
|
|
|
case "thumbnail":
|
2019-01-13 20:09:53 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
writeError(w, imgPath, method, "unknown method", http.StatusBadRequest)
|
2019-01-08 23:05:20 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-13 20:09:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, imgPath, method, err string, status int) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(fmt.Sprintf("Error [%s %s]: %s", method, imgPath, err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Start vips with the default configuration
|
|
|
|
vips.Startup(nil)
|
|
|
|
defer vips.Shutdown()
|
|
|
|
|
|
|
|
http.HandleFunc("/", resizeHandler)
|
|
|
|
log.Fatal(http.ListenAndServe(*listenAddr, nil))
|
2019-01-08 23:05:20 +00:00
|
|
|
}
|