@@ 8,9 8,20 @@ import (
"strings"
gemini "git.tdem.in/tdemin/gmnhg"
+ "github.com/gabriel-vasile/mimetype"
"github.com/go-git/go-git/v5"
)
+func isBinary(detectedMIME *mimetype.MIME) bool {
+ for mime := detectedMIME; mime != nil; mime = mime.Parent() {
+ if mime.Is("text/plain") {
+ return false
+ }
+ }
+
+ return true
+}
+
func createDirectoryIndex(treeRootPath, path string) {
files, err := ioutil.ReadDir(filepath.Join(treeRootPath, path))
check(err)
@@ 28,7 39,17 @@ func createDirectoryIndex(treeRootPath, path string) {
continue
}
- index.WriteString("=> " + file.Name() + "\n")
+ filePath := filepath.Join(treeRootPath, path, file.Name())
+ detectedMIME, err := mimetype.DetectFile(filePath)
+ check(err)
+
+ if isBinary(detectedMIME) {
+ index.WriteString("=> " + file.Name() + "\n")
+ continue
+ }
+
+ check(os.Rename(filePath, filePath+".txt"))
+ index.WriteString("=> " + file.Name() + ".txt " + file.Name() + "\n")
}
}
@@ 64,7 85,7 @@ func createIndexFile(distPath, repositoryPath string) {
indexFile.WriteString("=> " + treeSubPath + "/\n\n")
// convert README.md to gemini and write it to index.gmi in repository root
- text, err := os.ReadFile(filepath.Join(distPath, treeSubPath, "README.md"))
+ text, err := os.ReadFile(filepath.Join(distPath, treeSubPath, "README.md.txt"))
if err != nil {
if os.IsNotExist(err) {
@@ 6,5 6,6 @@ go 1.16
require (
git.tdem.in/tdemin/gmnhg v0.1.1
+ github.com/gabriel-vasile/mimetype v1.2.0
github.com/go-git/go-git/v5 v5.3.1
)
@@ 18,6 18,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
+github.com/gabriel-vasile/mimetype v1.2.0 h1:A6z5J8OhjiWFV91sQ3dMI8apYu/tvP9keDaMM3Xu6p4=
+github.com/gabriel-vasile/mimetype v1.2.0/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=