Compare commits
3 Commits
fffd276238
...
1031ad7e8c
| Author | SHA1 | Date | |
|---|---|---|---|
|
1031ad7e8c
|
|||
|
741b196497
|
|||
|
7c6d8f4206
|
@@ -5,9 +5,13 @@ import (
|
||||
|
||||
"tomatentum.net/svg-templater/internal/command"
|
||||
"tomatentum.net/svg-templater/internal/database"
|
||||
"tomatentum.net/svg-templater/pkg/format"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if !format.CheckInkscape() {
|
||||
panic("Inkscape not found")
|
||||
}
|
||||
if err := database.OpenSQLite(); err != nil {
|
||||
log.Fatal("Failed opening DB:\n", err)
|
||||
return
|
||||
|
||||
@@ -30,7 +30,7 @@ func HandleCommandline() {
|
||||
} else if help {
|
||||
flag.PrintDefaults()
|
||||
} else {
|
||||
svg.Storage = svg.NewFileStorage(datapath)
|
||||
svg.Storage = svg.NewFileStorage(datapath, "public")
|
||||
server.PrepareHTTP()
|
||||
server.Start()
|
||||
}
|
||||
|
||||
@@ -4,22 +4,26 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/routes"
|
||||
"tomatentum.net/svg-templater/pkg/auth"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
func PrepareHTTP() {
|
||||
registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "You are authorized!")
|
||||
})
|
||||
registerAuthorized("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||
registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
routes.CreateSVG(w, *r)
|
||||
})
|
||||
|
||||
registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||
}
|
||||
|
||||
func Start() {
|
||||
@@ -29,7 +33,15 @@ func Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||
http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f)))
|
||||
func registerAuthorized(path string, handler http.Handler) {
|
||||
http.HandleFunc(path, auth.AuthMiddleware(handler))
|
||||
log.Println("Registered authorized handler for", path)
|
||||
}
|
||||
|
||||
func registerAuthorizedFunc(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||
registerAuthorized(path, http.HandlerFunc(f))
|
||||
}
|
||||
|
||||
func GetPublicPath(path string) string {
|
||||
return filepath.Join("public", path)
|
||||
}
|
||||
|
||||
97
pkg/format/formatconverter.go
Normal file
97
pkg/format/formatconverter.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
type ConversionParameters struct {
|
||||
Format string
|
||||
Width, Height int
|
||||
}
|
||||
|
||||
func ConvertByte(svgblob []byte, param ConversionParameters) ([]byte, error) {
|
||||
reader, err := ConvertReader(svgblob, param)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
result, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ConvertReader(svgblob []byte, param ConversionParameters) (io.ReadCloser, error) {
|
||||
if param.Format == "svg" {
|
||||
return io.NopCloser(bytes.NewReader(svgblob)), nil
|
||||
}
|
||||
file, err := svg.CreateTemp(bytes.NewReader(svgblob), "svg")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.Remove(file)
|
||||
|
||||
return runCommand(file, param)
|
||||
}
|
||||
|
||||
func runCommand(input string, param ConversionParameters) (io.ReadCloser, error) {
|
||||
var args []string
|
||||
|
||||
var outExt string
|
||||
switch param.Format {
|
||||
case "png":
|
||||
args = append(args, "--export-type=png")
|
||||
outExt = ".png"
|
||||
case "jpg":
|
||||
args = append(args, "--export-type=png") // inkscape doesn't export jpg directly
|
||||
outExt = ".png"
|
||||
case "pdf":
|
||||
args = append(args, "--export-type=pdf")
|
||||
outExt = ".pdf"
|
||||
default:
|
||||
return nil, fmt.Errorf("format not supported: %s", param.Format)
|
||||
}
|
||||
|
||||
if param.Width > 0 {
|
||||
args = append(args, "-w", strconv.Itoa(param.Width))
|
||||
}
|
||||
if param.Height > 0 {
|
||||
args = append(args, "-h", strconv.Itoa(param.Height))
|
||||
}
|
||||
|
||||
outFile := strings.TrimSuffix(input, filepath.Ext(input)) + outExt
|
||||
args = append(args, "-o", outFile)
|
||||
args = append(args, input)
|
||||
|
||||
cmd := exec.Command("/opt/homebrew/bin/inkscape", args...)
|
||||
cmd.Dir = svg.TempDir
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("inkscape failed: %w: %s", err, string(out))
|
||||
}
|
||||
|
||||
file, err := os.Open(outFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func CheckInkscape() bool {
|
||||
_, err := exec.LookPath("inkscape")
|
||||
return err == nil
|
||||
}
|
||||
@@ -3,26 +3,33 @@ package svg
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type SvgStorage interface {
|
||||
Create(id string, svg io.Reader) (string, error)
|
||||
Get(id string) (io.Reader, error)
|
||||
Get(id string) (io.ReadCloser, error)
|
||||
CreatePublic(data io.Reader, filetype string) (string, error)
|
||||
GetPublic(path string) (io.ReadCloser, error)
|
||||
GetPublicDir() http.Dir
|
||||
}
|
||||
|
||||
var _ SvgStorage = FileSvgStorage{}
|
||||
var TempDir string = ""
|
||||
|
||||
type FileSvgStorage struct {
|
||||
basepath string
|
||||
basepath, publicSubPath string
|
||||
}
|
||||
|
||||
func NewFileStorage(path string) *FileSvgStorage {
|
||||
func NewFileStorage(path, publicSubPath string) *FileSvgStorage {
|
||||
err := os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Initialized file storage handler")
|
||||
return &FileSvgStorage{path}
|
||||
return &FileSvgStorage{path, publicSubPath}
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) {
|
||||
@@ -42,10 +49,51 @@ func (f FileSvgStorage) Create(id string, svg io.Reader) (string, error) {
|
||||
return file.Name(), nil
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) Get(id string) (io.Reader, error) {
|
||||
file, err := os.Open(filepath.Join(f.basepath, "svg", id+".svg"))
|
||||
func (f FileSvgStorage) Get(id string) (io.ReadCloser, error) {
|
||||
file, err := os.Open(filepath.Join(f.basepath, id+".svg"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) {
|
||||
path := filepath.Join(f.basepath, f.publicSubPath)
|
||||
if err := os.Mkdir(path, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
file, err := os.CreateTemp(path, "*."+filetype)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return file.Name(), nil
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) GetPublic(path string) (io.ReadCloser, error) {
|
||||
file, err := os.Open(filepath.Join(f.basepath, f.publicSubPath, path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (f FileSvgStorage) GetPublicDir() http.Dir {
|
||||
return http.Dir(filepath.Join(f.basepath, f.publicSubPath))
|
||||
}
|
||||
|
||||
func CreateTemp(data io.Reader, filetype string) (string, error) {
|
||||
file, err := os.CreateTemp(TempDir, "*."+filetype)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.Copy(file, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return file.Name(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user