add automatic image resizing
This commit is contained in:
@ -3,19 +3,129 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
//func startCmd() *cli.Command {
|
||||
// return &cli.Command{
|
||||
// Name: "start",
|
||||
// Aliases: []string{"s"},
|
||||
// Usage: "Start the application service",
|
||||
// Action: func(c *cli.Context) error {
|
||||
//
|
||||
// return nil
|
||||
// },
|
||||
// }
|
||||
//}
|
||||
func resizeCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "resize",
|
||||
Aliases: []string{"r"},
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "output",
|
||||
Value: "./resized.jpg",
|
||||
Usage: "path to output resized file (should be a jpg)",
|
||||
Aliases: []string{
|
||||
"o",
|
||||
},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "format",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "The format to resize the image to wide, tall, or square",
|
||||
Value: "wide",
|
||||
},
|
||||
},
|
||||
Usage: "Resize an image to a supported size for the API",
|
||||
Action: func(c *cli.Context) error {
|
||||
var needsCleanup bool
|
||||
// get the image
|
||||
// use the first argument as the file name
|
||||
fileLocation, err := getFileLocation(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting file location | %w", err)
|
||||
}
|
||||
|
||||
// get the format
|
||||
format := c.String("format")
|
||||
if format != "wide" && format != "tall" && format != "square" {
|
||||
return fmt.Errorf("invalid format %s", format)
|
||||
}
|
||||
|
||||
// get output file
|
||||
output := c.String("output")
|
||||
|
||||
// resize the image
|
||||
tempLocation, err := resizeImage(fileLocation, format)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error resizing image | %w", err)
|
||||
}
|
||||
|
||||
if tempLocation != fileLocation {
|
||||
fmt.Printf("Resized image to %s\n", tempLocation)
|
||||
needsCleanup = true
|
||||
}
|
||||
|
||||
// copy the temp image to the output file
|
||||
err = copyFile(tempLocation, output, 1024)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error copying file | %w", err)
|
||||
}
|
||||
|
||||
if needsCleanup {
|
||||
// remove the temp file
|
||||
err = cleanUpTempFile(tempLocation)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error removing temp file | %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// copyFile copies a file from src to dst
|
||||
// BUFFERSIZE is the size of the buffer to use when copying
|
||||
// https://opensource.com/article/18/6/copying-files-go
|
||||
func copyFile(src, dst string, BUFFERSIZE int64) error {
|
||||
sourceFileStat, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !sourceFileStat.Mode().IsRegular() {
|
||||
return fmt.Errorf("%s is not a regular file.", src)
|
||||
}
|
||||
|
||||
source, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
_, err = os.Stat(dst)
|
||||
if err == nil {
|
||||
return fmt.Errorf("File %s already exists.", dst)
|
||||
}
|
||||
|
||||
destination, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destination.Close()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
buf := make([]byte, BUFFERSIZE)
|
||||
for {
|
||||
n, err := source.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
if _, err := destination.Write(buf[:n]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func getJobResultCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
|
Reference in New Issue
Block a user