summaryrefslogtreecommitdiff
path: root/contrib/fs/file.go
blob: 7690a62da64e57e949e24421283fe91691e59676 (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
package fs

import (
	"mime"
	"strings"
)

func mediaType(filePath string) string {
	if strings.HasSuffix(filePath, ".gmi") {
		// This may not be present in the listings searched by mime.TypeByExtension,
		// so provide a dedicated fast path for it here.
		return "text/gemini"
	}

	slashIdx := strings.LastIndex(filePath, "/")
	dotIdx := strings.LastIndex(filePath[slashIdx+1:], ".")
	if dotIdx == -1 {
		return "application/octet-stream"
	}
	ext := filePath[slashIdx+1+dotIdx:]

	mtype := mime.TypeByExtension(ext)
	if mtype == "" {
		return "application/octet-stream"
	}
	return mtype
}

func isPrivate(fullpath string) bool {
	for _, segment := range strings.Split(fullpath, "/") {
		if len(segment) > 1 && segment[0] == '.' {
			return true
		}
	}
	return false
}