added support for povray

This commit is contained in:
Özgür Kesim 2018-11-25 13:55:47 +01:00
parent 62f1f72f40
commit ab9109b016
4 changed files with 62 additions and 11 deletions

View File

@ -161,7 +161,11 @@ function run() {
req.onreadystatechange = function() { req.onreadystatechange = function() {
switch (req.status) { switch (req.status) {
case 200: 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; break;
case 400: case 400:
output.innerHTML = '<pre class="error">'+req.responseText+'</pre>'; output.innerHTML = '<pre class="error">'+req.responseText+'</pre>';

52
main.go
View File

@ -29,6 +29,8 @@ type Tool struct {
Name string Name string
Cmd string Cmd string
Args []string Args []string
NeedsFile bool
EncodeBase64 bool
Suffix string Suffix string
Description string Description string
Documentation map[string]string Documentation map[string]string
@ -36,31 +38,61 @@ type Tool struct {
BgColor string 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 ( var (
cmd = exec.Command(t.Cmd, t.Args...) cmd = exec.Command(t.Cmd, args...)
err, buf = &bytes.Buffer{}, &bytes.Buffer{} err, buf = &bytes.Buffer{}, &bytes.Buffer{}
) )
cmd.Stdin = in if !t.NeedsFile {
cmd.Stdin = in
}
cmd.Stderr = err cmd.Stderr = err
cmd.Stdout = buf cmd.Stdout = buf
if e := cmd.Run(); e == nil { if e := cmd.Run(); e == nil {
io.Copy(w, buf) if t.EncodeBase64 {
w.Header().Add("Content-Type", "img/png")
io.Copy(base64.NewEncoder(base64.StdEncoding, w), buf)
} else {
io.Copy(w, buf)
}
} else { } else {
log.Printf("%s returned error\n", t.Name) log.Printf("%s returned error\n", t.Name)
http.Error(w, err.String(), http.StatusBadRequest) 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) { return func(w http.ResponseWriter, r *http.Request) {
t.execute(r.Body, w) 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) { return func(w http.ResponseWriter, r *http.Request) {
name := filepath.Base(r.URL.Path) name := filepath.Base(r.URL.Path)
if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil { 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 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) { return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, maxSnippetSize)) body, err := ioutil.ReadAll(io.LimitReader(r.Body, maxSnippetSize))
if err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
name := filepath.Base(r.URL.Path) name := filepath.Base(r.URL.Path)
if file, err := os.Open(filepath.Join(*savedir, name+t.Suffix)); err != nil { 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) { return func(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "index.html", map[string]interface{}{"Tools": data, "Cur": t}) err := tmpl.ExecuteTemplate(w, "index.html", map[string]interface{}{"Tools": data, "Cur": t})
if err != nil { if err != nil {
@ -135,7 +167,7 @@ func (t Tool) index(tmpl *template.Template, data interface{}) func(http.Respons
} }
func main() { func main() {
var tools = []Tool{} var tools = []*Tool{}
var tmpl *template.Template var tmpl *template.Template
flag.Parse() flag.Parse()

1
static/povray.js Symbolic link
View File

@ -0,0 +1 @@
dot.js

View File

@ -25,5 +25,19 @@
"https://ece.uwaterloo.ca/~aplevich/dpic": "Get DPIC from here" "https://ece.uwaterloo.ca/~aplevich/dpic": "Get DPIC from here"
}, },
"Example": ".PS\n\nbox \"foo\"; arrow ->; box \"bar\"\n\n.PE" "Example": ".PS\n\nbox \"foo\"; arrow ->; box \"bar\"\n\n.PE"
},
{
"Name": "povray",
"Cmd": "povray",
"Args": ["+O-", "-D"],
"Suffix": ".pov",
"NeedsFile": true,
"EncodeBase64": true,
"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"
} }
] ]