Add http server
This commit is contained in:
parent
ef328697b8
commit
9b177cbd6d
6 changed files with 278 additions and 85 deletions
|
|
@ -5,27 +5,19 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/saunaclub/inkpot-cli/epd"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
var width int
|
||||
var height int
|
||||
var infile string
|
||||
var outfile string
|
||||
|
||||
func Foobar () (io.Writer, error) {
|
||||
return os.Stdout, nil
|
||||
}
|
||||
|
||||
// convertCmd represents the convert command
|
||||
var convertCmd = &cobra.Command{
|
||||
Use: "convert <file|->",
|
||||
|
|
@ -50,9 +42,9 @@ Pass "-" as the filename to read from stdin.`,
|
|||
defer input.Close()
|
||||
}
|
||||
|
||||
// write to given outfile, default to stdout
|
||||
var output io.Writer
|
||||
if outfile == "" {
|
||||
// write to given outfile, default to stdout
|
||||
var output io.Writer
|
||||
if outfile == "" || outfile == "-" {
|
||||
output = os.Stdout
|
||||
} else {
|
||||
file, err := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0644)
|
||||
|
|
@ -63,7 +55,7 @@ Pass "-" as the filename to read from stdin.`,
|
|||
output = file
|
||||
}
|
||||
|
||||
result, err := convertImage(input, width, height)
|
||||
result, err := epd.ConvertImage(input, width, height)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not convert image: %v", err)
|
||||
}
|
||||
|
|
@ -78,58 +70,3 @@ func init() {
|
|||
convertCmd.Flags().IntVarP(&height, "height", "y", 960, "target height")
|
||||
convertCmd.Flags().StringVarP(&outfile, "output", "o", "", "file to write the result to (default stdout)")
|
||||
}
|
||||
|
||||
// Returns a new Rectangle that is resized and centered in `dst`
|
||||
func fitRectInto(src *image.Rectangle, dst *image.Rectangle) image.Rectangle {
|
||||
var targetWidth int
|
||||
var targetHeight int
|
||||
var scale float64
|
||||
|
||||
srcRatio := float64(src.Max.X) / float64(src.Max.Y)
|
||||
dstRatio := float64(dst.Max.X) / float64(dst.Max.Y)
|
||||
|
||||
if srcRatio < dstRatio {
|
||||
// center horizontally, scale vertically
|
||||
scale = float64(dst.Max.Y) / float64(src.Max.Y)
|
||||
} else {
|
||||
// center vertically, scale horizontally
|
||||
scale = float64(dst.Max.X) / float64(src.Max.X)
|
||||
}
|
||||
|
||||
targetWidth = int(float64(src.Max.X) * scale)
|
||||
targetHeight = int(float64(src.Max.Y) * scale)
|
||||
|
||||
targetX := (dst.Max.X - targetWidth) / 2
|
||||
targetY := (dst.Max.Y - targetHeight) / 2
|
||||
|
||||
return image.Rect(targetX, targetY, targetWidth+targetX, targetHeight+targetY)
|
||||
}
|
||||
|
||||
func convertImage(input io.Reader, width int, height int) ([]byte, error) {
|
||||
src, _, err := image.Decode(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dst := image.NewGray(image.Rect(0, 0, width, height))
|
||||
|
||||
srcBounds := src.Bounds()
|
||||
targetRect := fitRectInto(&srcBounds, &dst.Rect)
|
||||
|
||||
draw.Draw(dst, dst.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
|
||||
draw.CatmullRom.Scale(dst, targetRect, src, src.Bounds(), draw.Over, nil)
|
||||
|
||||
// the actual conversion works by packing two nibbles together in a byte
|
||||
var result = make([]byte, (width*height+1)/2)
|
||||
for i, p := range dst.Pix {
|
||||
res := uint8((uint16(p) + 8) / 16)
|
||||
if i%2 == 0 {
|
||||
result[i/2] = (res << 4)
|
||||
} else {
|
||||
// note that integer division makes sure we're writing at the same
|
||||
// index for odd and even indices
|
||||
result[i/2] = result[i/2] | res
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
|||
16
cmd/root.go
16
cmd/root.go
|
|
@ -22,8 +22,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "inkpot-cli",
|
||||
|
|
@ -32,9 +30,6 @@ var rootCmd = &cobra.Command{
|
|||
resizing them, rotating them and converting them to 16-color
|
||||
grayscale so they can be comfortably displayed on e-ink
|
||||
displays powered by the epdiy driver.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
|
@ -47,15 +42,4 @@ func Execute() {
|
|||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.inkpot-cli.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
165
cmd/serve.go
Normal file
165
cmd/serve.go
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/saunaclub/inkpot-cli/epd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var defaultWidth int = 540
|
||||
var defaultHeight int = 960
|
||||
var port int
|
||||
|
||||
type Params struct {
|
||||
Width int `form:"height"`
|
||||
Height int `form:"width"`
|
||||
Url string `form:"url"`
|
||||
}
|
||||
|
||||
// serveCmd represents the serve command
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "Run a webserver to convert images via HTTP",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
router := gin.Default()
|
||||
// Set a lower memory limit for multipart forms (default is 32 MiB)
|
||||
router.MaxMultipartMemory = 8 << 20 // 8 MiB
|
||||
router.GET("/", getIndex)
|
||||
router.GET("/convert", getConvert)
|
||||
router.PUT("/convert", putConvert)
|
||||
router.Run(fmt.Sprintf(":%d", port))
|
||||
},
|
||||
}
|
||||
|
||||
func getIndex(c *gin.Context) {
|
||||
usage := `# inkpot-convert
|
||||
|
||||
A webserver to convert GIFs, PNGs and JPEGs to 4-bit grayscale images.
|
||||
|
||||
## Routes
|
||||
|
||||
- PUT /convert can be used to convert a file on your filesystem
|
||||
- GET /convert/[url] can be used to convert a file publically accessible via URL
|
||||
|
||||
Both routes accept a "width" and a "height" parameter to configure the output size.
|
||||
|
||||
## Examples
|
||||
|
||||
Via curl:
|
||||
|
||||
curl -X PUT http://localhost:8080/convert \
|
||||
-F "file=@my_cat.jpeg" \
|
||||
-H "Content-Type: multipart/form-data"
|
||||
|
||||
Via httpie:
|
||||
|
||||
http --form PUT :8080/convert file@my_cat.jpg
|
||||
`
|
||||
|
||||
c.String(http.StatusOK, usage)
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func getConvert(c *gin.Context) {
|
||||
var params Params
|
||||
err := c.ShouldBindQuery(¶ms)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
if params.Url == "" {
|
||||
c.String(http.StatusBadRequest, "Please supply an image URL.")
|
||||
return
|
||||
}
|
||||
|
||||
width := min(params.Width, 2000)
|
||||
height := min(params.Height, 2000)
|
||||
if width <= 0 {
|
||||
width = defaultWidth
|
||||
}
|
||||
if height <= 0 {
|
||||
height = defaultHeight
|
||||
}
|
||||
|
||||
response, err := http.Get(params.Url)
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
c.Status(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
reader := response.Body
|
||||
defer reader.Close()
|
||||
|
||||
converted, err := epd.ConvertImage(reader, width, height)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
extraHeaders := map[string]string{
|
||||
"X-Image-Width": fmt.Sprintf("%d", width),
|
||||
"X-Image-height": fmt.Sprintf("%d", height),
|
||||
}
|
||||
|
||||
c.DataFromReader(http.StatusOK, int64(len(converted)), "x-image/inkpot-epd", bytes.NewReader(converted), extraHeaders)
|
||||
}
|
||||
|
||||
func putConvert(c *gin.Context) {
|
||||
var params Params
|
||||
err := c.ShouldBindQuery(¶ms)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
width := min(params.Width, 2000)
|
||||
height := min(params.Height, 2000)
|
||||
if width <= 0 {
|
||||
width = defaultWidth
|
||||
}
|
||||
if height <= 0 {
|
||||
height = defaultHeight
|
||||
}
|
||||
|
||||
// Single file
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
reader, err := file.Open()
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
converted, err := epd.ConvertImage(reader, width, height)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
|
||||
extraHeaders := map[string]string{
|
||||
"X-Image-Width": fmt.Sprintf("%d", width),
|
||||
"X-Image-height": fmt.Sprintf("%d", height),
|
||||
}
|
||||
|
||||
c.DataFromReader(http.StatusOK, int64(len(converted)), "x-image/inkpot-epd", bytes.NewReader(converted), extraHeaders)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "port to bind to")
|
||||
// gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue