diff --git a/index.html b/index.html index f4cd42d..37ab4aa 100644 --- a/index.html +++ b/index.html @@ -161,7 +161,11 @@ function run() { req.onreadystatechange = function() { switch (req.status) { case 200: - output.innerHTML = req.responseText; + if (req.getResponseHeader("Content-Type") === "img/png") { + output.innerHTML = ''; + } else { + output.innerHTML = req.responseText; + } break; case 400: output.innerHTML = '
'+req.responseText+'
'; diff --git a/main.go b/main.go index c3afed3..07e15b1 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,8 @@ type Tool struct { Name string Cmd string Args []string + NeedsFile bool + ContentType string Suffix string Description string Documentation map[string]string @@ -36,31 +38,61 @@ type Tool struct { BgColor string } -func (t Tool) execute(in io.Reader, w http.ResponseWriter) { +func (t *Tool) execute(in io.Reader, w http.ResponseWriter) { + var args []string + + if t.NeedsFile { + tmpf, e := ioutil.TempFile(".", "*"+t.Suffix) + if e != nil { + log.Printf("couldn't create tmp-file: %v\n", e) + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } else if _, e = io.Copy(tmpf, in); e != nil { + log.Printf("couldn't write to tmp-file: %v\n", e) + http.Error(w, e.Error(), http.StatusInternalServerError) + return + } + defer os.Remove(tmpf.Name()) + // log.Printf("using tempfile: %q\n", tmpf.Name()) + + args = []string{} + args = append(args, t.Args...) + args = append(args, tmpf.Name()) + } else { + args = t.Args + } + var ( - cmd = exec.Command(t.Cmd, t.Args...) + cmd = exec.Command(t.Cmd, args...) err, buf = &bytes.Buffer{}, &bytes.Buffer{} ) - cmd.Stdin = in + if !t.NeedsFile { + cmd.Stdin = in + } cmd.Stderr = err cmd.Stdout = buf if e := cmd.Run(); e == nil { - io.Copy(w, buf) + if t.ContentType != "" { + w.Header().Add("Content-Type", t.ContentType) + io.Copy(base64.NewEncoder(base64.StdEncoding, w), buf) + } else { + io.Copy(w, buf) + } } else { log.Printf("%s returned error\n", t.Name) http.Error(w, err.String(), http.StatusBadRequest) } } -func (t Tool) compile() func(http.ResponseWriter, *http.Request) { +func (t *Tool) compile() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { t.execute(r.Body, w) } } -func (t Tool) svg() func(http.ResponseWriter, *http.Request) { +func (t *Tool) svg() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { name := filepath.Base(r.URL.Path) if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil { @@ -75,7 +107,7 @@ func (t Tool) svg() func(http.ResponseWriter, *http.Request) { const maxSnippetSize = 64 * 1024 -func (t Tool) save() func(http.ResponseWriter, *http.Request) { +func (t *Tool) save() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(io.LimitReader(r.Body, maxSnippetSize)) if err != nil { @@ -112,7 +144,7 @@ func (t Tool) save() func(http.ResponseWriter, *http.Request) { } } -func (t Tool) load() func(http.ResponseWriter, *http.Request) { +func (t *Tool) load() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { name := filepath.Base(r.URL.Path) if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil { @@ -125,7 +157,7 @@ func (t Tool) load() func(http.ResponseWriter, *http.Request) { } } -func (t Tool) index(tmpl *template.Template, data interface{}) func(http.ResponseWriter, *http.Request) { +func (t *Tool) index(tmpl *template.Template, data interface{}) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { err := tmpl.ExecuteTemplate(w, "index.html", map[string]interface{}{"Tools": data, "Cur": t}) if err != nil { @@ -135,7 +167,7 @@ func (t Tool) index(tmpl *template.Template, data interface{}) func(http.Respons } func main() { - var tools = []Tool{} + var tools = []*Tool{} var tmpl *template.Template flag.Parse() diff --git a/static/povray.js b/static/povray.js new file mode 120000 index 0000000..e382383 --- /dev/null +++ b/static/povray.js @@ -0,0 +1 @@ +dot.js \ No newline at end of file diff --git a/tools.json b/tools.json index 9f3b3fe..f19fa5d 100644 --- a/tools.json +++ b/tools.json @@ -25,5 +25,19 @@ "https://ece.uwaterloo.ca/~aplevich/dpic": "Get DPIC from here" }, "Example": ".PS\n\nbox \"foo\"; arrow ->; box \"bar\"\n\n.PE" + }, + { + "Name": "povray", + "Cmd": "povray", + "Args": ["+O-", "-D", "+A0.9", "+R3"], + "Suffix": ".pov", + "NeedsFile": true, + "ContentType": "img/png", + "Description": "Povray, raytracer", + "BgColor": "Orange", + "Documentation": { + "http://povray.org/documentation/3.7.0/index.html": "POV-Ray for Unix" + }, + "Example": "#include \"colors.inc\"\n#include \"woods.inc\"\n#include \"glass.inc\"\ncylinder {\n\t<-2,-2,0.5>, <-2,1,0.5>, 1\n\tpigment {color Red}\n}\nsphere {\n\t<0,1,1>, 1\n\tpigment {color Blue }\n}\nbox {\n\t<-3,-1,-1>, <1,-0.5, 2>\n\tpigment {\n\t\tCol_Glass_Winebottle\n\t}\n}\nbackground { color Gray60 }\nlight_source {<0, 5, -3> color White }\nlight_source {<-2, 2, 0.5> color Yellow }\ncamera {\n\tlocation <0, 2, -6>\n\tangle 50 right x\n\tlook_at\n\t<-1, 0, 0>\n}\n" } ]