summaryrefslogtreecommitdiff
path: root/src/edit/util.go
blob: b1950ed6576de8a2fde961e7da3df17a9c80f50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

import (
	"mime"
	"net/http"
	"os/exec"
	"path"
	"strings"
)

type exitError exec.ExitError

func (e *exitError) Error() string {
	ret := e.ProcessState.String()
	if len(e.Stderr) > 0 {
		ret += "\n" + string(e.Stderr)
	}
	return ret
}

func errcheck(err error) {
	if err != nil {
		if ee, ok := err.(*exec.ExitError); ok {
			ee2 := exitError(*ee)
			err = &ee2
		}
		panic(err)
	}
}

func getctype(name string, content []byte) string {
	ctype := mime.TypeByExtension(path.Ext(name))
	if ctype == "" {
		ctype = http.DetectContentType(content)
	}

	i := strings.Index(ctype, ";")
	if i == -1 {
		i = len(ctype)
	}
	ctype = strings.TrimSpace(strings.ToLower(ctype[0:i]))

	return ctype
}

func istext(ctype string) bool {
	return strings.HasPrefix(ctype, "text/") || strings.HasSuffix(ctype, "+xml")
}