diff --git a/Dockerfile b/Dockerfile index 506bfdf..3f3d1de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,11 @@ FROM jess/inkscape -RUN apt-get update && apt-get install -y python-lxml python-numpy +RUN apt-get update && apt-get install -y python-lxml python-numpy wget + +RUN wget https://golang.org/dl/go1.16.2.linux-amd64.tar.gz && rm -rf /usr/local/go && tar -C /usr/local -xzf go1.16.2.linux-amd64.tar.gz && ln -s /usr/local/go/bin/go /usr/local/bin/go && go version WORKDIR /app -RUN apt-get update && apt-get install -y golang -ADD main.go . +ADD main.go index.html ./ RUN go build -o main main.go ENTRYPOINT ["/app/main"] diff --git a/Makefile b/Makefile index 2f8b07f..d2c3302 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ build: lint check-format fetch-deps .PHONY: docker-build # Build the docker image docker-build: - @docker build build --tag $(IMAGE) . + @docker build --tag $(IMAGE) . .PHONY: docker-publish # Push the docker image to the remote registry docker-publish: @@ -39,7 +39,7 @@ docker-publish: .PHONY: run # Run the application run: docker-build - @docker run -it -p 8000:8080 $(IMAGE) + @docker run -it -p 8080:8080 $(IMAGE) .PHONY: ci # Perform CI specific tasks to perform on a pull request ci: diff --git a/README.md b/README.md index 7b16edb..e9d3f36 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ -# svg-to-dxf +![svg-to-dxf](logo.png) -Convert .svg files to .dxf +> Convert .svg files to .dxf + +Available at https://svg-to-dxf.cluster.fun/ ## Features -Runs a webserver that takes in an `?url=` query string, fetches the SVG from that URL and then returns it as a .dxf +Runs a webserver that takes in an `?url=` query string and fetches the SVG from that URL or an svg file uploaded and then returns it as a .dxf ## Usage diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..760a436 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module svg-to-dxf + +go 1.16 diff --git a/index.html b/index.html new file mode 100644 index 0000000..1ac5fe9 --- /dev/null +++ b/index.html @@ -0,0 +1,93 @@ + + + + + + + svg-to-dxf + + + + + + + + + + + + + + + + + +
+

+ svg-to-dxf + +

+ +
+ Convert .svg files to .dxf +
+ +

+ Enter the URL of an SVG file and press convert to download the file as a .dxf. +

+ +
+
+
+ + +
+
+
+ +

+ Upload an SVG file and press convert to download the file as a .dxf. +

+ +
+
+
+ + +
+
+
+ +
+ +
+ Source code available on GitHub, GitLab, Bitbucket & my own Gitea server. +
+
+ +
+
+
+ +
+
+
+ + diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..08450f0 Binary files /dev/null and b/logo.png differ diff --git a/main.go b/main.go index 2ac8abf..29f9e40 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "errors" "fmt" "io" @@ -10,6 +11,9 @@ import ( "os/exec" ) +//go:embed index.html +var content embed.FS + var port = os.Getenv("PORT") func main() { @@ -19,19 +23,38 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { svgURL := r.URL.Query().Get("url") - if svgURL == "" { + r.ParseMultipartForm(1000000) + + uploadedFile, fileHeader, err := r.FormFile("svg") + + if svgURL == "" && (fileHeader == nil || fileHeader.Size == 0) { + body, _ := content.ReadFile("index.html") + w.Write(body) return } - fmt.Println("Fetching " + svgURL) filename := "/app/" + randomString(7) + ".svg" - fmt.Println("Filename: " + filename) - if err := downloadFile(svgURL, filename); err != nil { - fmt.Fprintf(w, err.Error()) - return + + if svgURL != "" { + fmt.Println("Fetching " + svgURL) + if err := downloadFile(svgURL, filename); err != nil { + fmt.Fprintf(w, err.Error()) + return + } + fmt.Println("Downloaded SVG") + } else { + file, err := os.Create(filename) + if err != nil { + return + } + defer file.Close() + + _, err = io.Copy(file, uploadedFile) + if err != nil { + return + } } - fmt.Println("Downloaded SVG") dxf, err := convertToDXF(filename) if err != nil {