feat: attempt to use input filename as output filename

This commit is contained in:
Marcus Noble 2021-07-25 09:47:49 +01:00
parent c1d955dc3a
commit 4b5e20b460
1 changed files with 11 additions and 1 deletions

12
main.go
View File

@ -9,6 +9,8 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strings"
) )
//go:embed index.html //go:embed index.html
@ -36,14 +38,22 @@ func main() {
filename := "/app/" + randomString(7) + ".svg" filename := "/app/" + randomString(7) + ".svg"
fmt.Println("Filename: " + filename) fmt.Println("Filename: " + filename)
outFilename := "out"
if svgURL != "" { if svgURL != "" {
fmt.Println("Fetching " + svgURL) fmt.Println("Fetching " + svgURL)
re := regexp.MustCompile(`(?m)/([\w|-]+)\.svg$`)
matches := re.FindStringSubmatch(svgURL)
if len(matches) == 2 {
outFilename = matches[1]
}
if err := downloadFile(svgURL, filename); err != nil { if err := downloadFile(svgURL, filename); err != nil {
fmt.Fprintf(w, err.Error()) fmt.Fprintf(w, err.Error())
return return
} }
fmt.Println("Downloaded SVG") fmt.Println("Downloaded SVG")
} else { } else {
outFilename = strings.ReplaceAll(fileHeader.Filename, ".svg", "")
file, err := os.Create(filename) file, err := os.Create(filename)
if err != nil { if err != nil {
return return
@ -65,7 +75,7 @@ func main() {
w.Header().Add("Content-Type", "application/octet-stream") w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("Content-Length", fmt.Sprint(len(dxf))) w.Header().Add("Content-Length", fmt.Sprint(len(dxf)))
w.Header().Add("Content-Disposition", "inline; filename=out.dxf") w.Header().Add("Content-Disposition", fmt.Sprintf("inline; filename=%s.dxf", outFilename))
fmt.Fprintf(w, string(dxf)) fmt.Fprintf(w, string(dxf))
os.Remove(filename) os.Remove(filename)