Put different transforms into different functions
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
2ac1c8ba5f
commit
14ca478ecc
83
main.go
83
main.go
|
@ -1,10 +1,12 @@
|
||||||
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
// (c) 2018 Karthik Karanth (https://github.com/medakk/gotiny)
|
||||||
|
// (c) 2018-2021 Andreas Neue <an@dnix.de>
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -23,7 +25,7 @@ func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func resizeHandler(w http.ResponseWriter, r *http.Request) {
|
func transform(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get the query parameters from the request URL
|
// Get the query parameters from the request URL
|
||||||
path := r.URL.EscapedPath()
|
path := r.URL.EscapedPath()
|
||||||
parm := strings.Split(path, "/")
|
parm := strings.Split(path, "/")
|
||||||
|
@ -43,9 +45,42 @@ func resizeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := []byte{}
|
||||||
|
errMsg := ""
|
||||||
|
errCode := 200
|
||||||
|
|
||||||
switch method {
|
switch method {
|
||||||
|
case "pass":
|
||||||
|
break
|
||||||
|
|
||||||
case "resize":
|
case "resize":
|
||||||
|
buf, errMsg, errCode = doResize(resp.Body, args)
|
||||||
|
if buf == nil {
|
||||||
|
writeError(w, imgPath, method, errMsg, errCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "thumbnail":
|
||||||
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
|
writeError(w, imgPath, method, "unknown method", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(buf)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, imgPath, method, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func doResize(body io.ReadCloser, args string) ([]byte, string, int) {
|
||||||
allowed := strings.Split(*sizesFilter, ",")
|
allowed := strings.Split(*sizesFilter, ",")
|
||||||
forbidden := true
|
forbidden := true
|
||||||
if allowed[0] == "*" {
|
if allowed[0] == "*" {
|
||||||
|
@ -59,65 +94,43 @@ func resizeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if forbidden {
|
if forbidden {
|
||||||
writeError(w, imgPath, method, "size is not allowed", http.StatusForbidden)
|
return nil, "size is not allowed", http.StatusForbidden
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size := strings.Split(args, "x")
|
size := strings.Split(args, "x")
|
||||||
if len(size) < 2 {
|
if len(size) < 2 {
|
||||||
writeError(w, imgPath, method, "url, width, and height are required", http.StatusBadRequest)
|
return nil, "width and height are required", http.StatusBadRequest
|
||||||
return
|
|
||||||
}
|
}
|
||||||
width := size[0]
|
width := size[0]
|
||||||
height := size[1]
|
height := size[1]
|
||||||
|
|
||||||
// Validate that all three required fields are present
|
// Validate that all three required fields are present
|
||||||
if imgPath == "" || width == "" || height == "" {
|
if width == "" || height == "" {
|
||||||
writeError(w, imgPath, method, "url, width, and height are required", http.StatusBadRequest)
|
return nil, "width and height are required", http.StatusBadRequest
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert width and height to integers
|
// Convert width and height to integers
|
||||||
iw, errW := strconv.Atoi(width)
|
iw, errW := strconv.Atoi(width)
|
||||||
ih, errH := strconv.Atoi(height)
|
ih, errH := strconv.Atoi(height)
|
||||||
if errW != nil || errH != nil {
|
if errW != nil || errH != nil {
|
||||||
writeError(w, imgPath, method, "width and height must be integers", http.StatusBadRequest)
|
return nil, "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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read from response body, convert and write to output buffer
|
// Read from response body, convert and write to output buffer
|
||||||
img, err := vips.NewImageFromReader(resp.Body)
|
img, err := vips.NewImageFromReader(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, imgPath, method, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError
|
||||||
return
|
|
||||||
}
|
}
|
||||||
err = img.Thumbnail(iw, ih, 0)
|
err = img.Thumbnail(iw, ih, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, imgPath, method, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError
|
||||||
return
|
|
||||||
}
|
}
|
||||||
ep := vips.NewDefaultExportParams() // default export params are sufficient
|
ep := vips.NewDefaultExportParams() // default export params are sufficient
|
||||||
buf, _, _ := img.Export(ep)
|
buf, _, _ := img.Export(ep)
|
||||||
_, err = w.Write(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, imgPath, method, fmt.Sprintf("%v", err), http.StatusInternalServerError)
|
return nil, fmt.Sprintf("%v", err), http.StatusInternalServerError
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return buf, "", 0
|
||||||
|
|
||||||
case "thumbnail":
|
|
||||||
|
|
||||||
default:
|
|
||||||
writeError(w, imgPath, method, "unknown method", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeError(w http.ResponseWriter, imgPath, method, err string, status int) {
|
func writeError(w http.ResponseWriter, imgPath, method, err string, status int) {
|
||||||
|
@ -130,6 +143,6 @@ func main() {
|
||||||
vips.Startup(nil)
|
vips.Startup(nil)
|
||||||
defer vips.Shutdown()
|
defer vips.Shutdown()
|
||||||
|
|
||||||
http.HandleFunc("/", resizeHandler)
|
http.HandleFunc("/", transform)
|
||||||
log.Fatal(http.ListenAndServe(*listenAddr, nil))
|
log.Fatal(http.ListenAndServe(*listenAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue