diff options
author | Özgür Kesim <oec@codeblau.de> | 2019-06-14 11:02:54 +0200 |
---|---|---|
committer | Özgür Kesim <oec@codeblau.de> | 2019-06-14 11:02:54 +0200 |
commit | 4d20598a28a3139cea545736eca90df88a608e1b (patch) | |
tree | 1381b5924bc0d4603f288ed8f58829ea6d998c2d | |
parent | 6c7d5f258fc754e6623a9a0b56d4420c068f6931 (diff) | |
parent | 1e3b329150d6c259ba058f8d8f2e1b3fd4bb525d (diff) |
Merge branch 'master' of github.com:/oec/playdot
-rw-r--r-- | index.html | 6 | ||||
-rw-r--r-- | main.go | 52 | ||||
l--------- | static/povray.js | 1 | ||||
-rw-r--r-- | tools.json | 14 |
4 files changed, 62 insertions, 11 deletions
@@ -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 = '<img src="data:image/png;base64,' + req.responseText + '"/>'; + } else { + output.innerHTML = req.responseText; + } break; case 400: output.innerHTML = '<pre class="error">'+req.responseText+'</pre>'; @@ -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 @@ -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" } ] |