160 lines
3.3 KiB
Go
160 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/urfave/cli/v2"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
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{
|
|
Name: "get-job-result",
|
|
Aliases: []string{"gjr"},
|
|
Usage: "Get the result of a job",
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "job-id",
|
|
Aliases: []string{"id"},
|
|
Usage: "The id of the job to get the result of",
|
|
Required: true,
|
|
},
|
|
},
|
|
Action: func(c *cli.Context) error {
|
|
// get the job id
|
|
jobID := c.String("job-id")
|
|
|
|
// get the output file
|
|
output := c.String("output")
|
|
|
|
// get the job result
|
|
err := job(jobID, output)
|
|
if err != nil {
|
|
return fmt.Errorf("error running job result checker | %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|