You've already forked img-proxy-url-generator
This commit is contained in:
43
commands/decrypt.go
Normal file
43
commands/decrypt.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/generator"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/printer"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func DecryptCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "decrypt",
|
||||
Usage: "decrypt an image url contents",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "url",
|
||||
Aliases: []string{"u"},
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
pr := printer.NewPrinter()
|
||||
cfg, err := config.NewConfig(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ig, err := generator.NewGenerator(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
plain, err := ig.Decrypt(c.String("url"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pr.LogSuccess(plain)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
79
commands/generate.go
Normal file
79
commands/generate.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/generator"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/printer"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func GenerateCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "generate",
|
||||
Usage: "Generate an image from a URL",
|
||||
Action: runGenerate,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "image",
|
||||
Aliases: []string{"i"},
|
||||
Required: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "format",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "Convert the image to the specified format",
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "params",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Processing options to be passed to the generator ref: https://docs.imgproxy.net/usage/processing",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runGenerate(c *cli.Context) error {
|
||||
p := printer.NewPrinter()
|
||||
|
||||
_, err := config.NewConfig(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url, err := signURL(c.String("image"), c.StringSlice("params"), c.String("format"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.LogInfo("Url Generated...")
|
||||
|
||||
println(url)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func signURL(file string, params []string, formatS string) (string, error) {
|
||||
cfg := config.GetConfig()
|
||||
if cfg == nil {
|
||||
return "", fmt.Errorf("config not loaded")
|
||||
}
|
||||
|
||||
ig, err := generator.NewGenerator(cfg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
format, err := ig.StringToFormat(formatS)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
url, err := ig.GenerateUrl(file, params, format)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return url, nil
|
||||
}
|
||||
53
commands/grpc.go
Normal file
53
commands/grpc.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
proto "github.com/siteworxpro/img-proxy-url-generator/grpc"
|
||||
"github.com/urfave/cli/v2"
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GrpcCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "grpc",
|
||||
Usage: "Start a grpc service",
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "port",
|
||||
Aliases: []string{"p"},
|
||||
Usage: "Port to listen on",
|
||||
Required: false,
|
||||
Value: 9000,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
cfg, err := config.NewConfig(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := grpc.NewServer()
|
||||
addr := fmt.Sprintf(":%d", c.Int("port"))
|
||||
println("listening on", addr)
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
svc, err := proto.NewService(cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
|
||||
proto.RegisterGeneratorServer(s, svc)
|
||||
err = s.Serve(lis)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
22
commands/report.go
Normal file
22
commands/report.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/report"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func ReportCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "report",
|
||||
Usage: "Generate usage report",
|
||||
Action: func(c *cli.Context) error {
|
||||
cf, err := config.NewConfig(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return report.Handle(cf)
|
||||
},
|
||||
}
|
||||
}
|
||||
124
commands/server.go
Normal file
124
commands/server.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/aws"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/generator"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/printer"
|
||||
"github.com/urfave/cli/v2"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type jsonRequest struct {
|
||||
Image string `json:"image"`
|
||||
Params []string `json:"params"`
|
||||
Format string `json:"format"`
|
||||
}
|
||||
|
||||
func ServerCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "server",
|
||||
Usage: "Start a webserver for s3 file browsing and the web service",
|
||||
Action: func(c *cli.Context) error {
|
||||
p := printer.NewPrinter()
|
||||
return startServer(c, p)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func startServer(c *cli.Context, p *printer.Printer) error {
|
||||
cfg, err := config.NewConfig(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ig, err := generator.NewGenerator(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = os.Stat("./templates")
|
||||
if !os.IsNotExist(err) {
|
||||
awsClient := aws.NewClient(cfg)
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
|
||||
return
|
||||
}
|
||||
contToken := r.URL.Query().Get("next")
|
||||
|
||||
var next *string
|
||||
if contToken == "" {
|
||||
next = nil
|
||||
} else {
|
||||
next = &contToken
|
||||
}
|
||||
|
||||
contents, err := awsClient.ListBucketContents(next)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
for i, content := range contents.Images {
|
||||
contents.Images[i].Url, _ = ig.GenerateUrl("s3://"+cfg.Aws.AwsBucket+"/"+content.Name, []string{"pr:sq"}, "")
|
||||
contents.Images[i].Download, _ = ig.GenerateUrl("s3://"+cfg.Aws.AwsBucket+"/"+content.Name, []string{""}, "")
|
||||
}
|
||||
|
||||
file, _ := os.ReadFile("./templates/index.gohtml")
|
||||
|
||||
tmpl := template.Must(template.New("index").Parse(string(file)))
|
||||
|
||||
err = tmpl.Execute(w, contents)
|
||||
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
http.HandleFunc("/generate", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
w.WriteHeader(404)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
bodyContents := make([]byte, r.ContentLength)
|
||||
_, _ = r.Body.Read(bodyContents)
|
||||
|
||||
jr := jsonRequest{}
|
||||
err = json.Unmarshal(bodyContents, &jr)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
|
||||
url, err := signURL(jr.Image, jr.Params, jr.Format)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("%s - [%s] - (%s)", jr.Image, strings.Join(jr.Params, ","), url))
|
||||
|
||||
_, _ = w.Write([]byte(url))
|
||||
})
|
||||
|
||||
p.LogSuccess("Starting http server on port 8080. http://localhost:8080")
|
||||
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user