feat(template): add Templating functionality and endpoint
This commit is contained in:
105
internal/routes/download.go
Normal file
105
internal/routes/download.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"tomatentum.net/svg-templater/pkg/format"
|
||||
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||
)
|
||||
|
||||
type downloadRequest struct {
|
||||
TemplateKeys map[string]string
|
||||
}
|
||||
|
||||
type downloadResponse struct {
|
||||
Url string
|
||||
}
|
||||
|
||||
func DownloadSVG(w http.ResponseWriter, r *http.Request) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType != "application/json" {
|
||||
http.Error(w, "Incorrect Content-Type. Needs application/json.", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
var request downloadRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
http.Error(w, "No ID provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
widthString := r.URL.Query().Get("w")
|
||||
width := 0
|
||||
if widthString != "" {
|
||||
width, err = strconv.Atoi(widthString)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid w parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
heightString := r.URL.Query().Get("h")
|
||||
height := 0
|
||||
if heightString != "" {
|
||||
height, err = strconv.Atoi(heightString)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid h parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
convParam := format.ConversionParameters{
|
||||
Format: r.URL.Query().Get("format"),
|
||||
Width: width,
|
||||
Height: height,
|
||||
}
|
||||
if convParam.Format == "" {
|
||||
convParam.Format = "svg"
|
||||
}
|
||||
|
||||
templateParam := actions.TemplateParameters{
|
||||
Id: id,
|
||||
Keys: request.TemplateKeys,
|
||||
}
|
||||
|
||||
filename, err := actions.ProvideFile(&templateParam, &convParam)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
urlparsed := getPublicUrl(r, filename)
|
||||
log.Printf("Made %s available as %s\n", id, urlparsed)
|
||||
response := downloadResponse{
|
||||
Url: urlparsed,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(json)
|
||||
}
|
||||
|
||||
func getPublicUrl(r *http.Request, path string) string {
|
||||
newURL := url.URL{
|
||||
Host: r.Host,
|
||||
Path: filepath.Join("public", path),
|
||||
}
|
||||
newURL.Scheme = "http"
|
||||
if r.TLS != nil {
|
||||
newURL.Scheme = "https"
|
||||
}
|
||||
return newURL.String()
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||
)
|
||||
|
||||
func CreateSVG(writer http.ResponseWriter, r http.Request) {
|
||||
func CreateSVG(writer http.ResponseWriter, r *http.Request) {
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType != "image/svg+xml" {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/routes"
|
||||
"tomatentum.net/svg-templater/pkg/auth"
|
||||
@@ -20,10 +19,17 @@ func PrepareHTTP() {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
routes.CreateSVG(w, *r)
|
||||
routes.CreateSVG(w, r)
|
||||
})
|
||||
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
routes.DownloadSVG(w, r)
|
||||
})
|
||||
|
||||
registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||
}
|
||||
|
||||
func Start() {
|
||||
@@ -41,7 +47,3 @@ func registerAuthorized(path string, handler http.Handler) {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user