"io/ioutil"
"log"
"net"
+ "net/http"
"os"
"path/filepath"
"strconv"
return path, err
}
+func guessFiletype(path string) (string, error) {
+ // Open File
+ f, err := os.Open(path)
+ if check(err, "Open "+path) != nil {
+ return "i", err
+ }
+ defer f.Close()
+ // Get the content
+ buffer := make([]byte, 512)
+ _, err = f.Read(buffer)
+ if check(err, "Read "+path) != nil {
+ return "i", err
+ }
+ contentType := http.DetectContentType(buffer)
+ //tracer.Print("Content Type " + path + " == "+ contentType)
+ shortType := strings.Split(contentType, ";")[0]
+ genType := strings.Split(shortType, "/")[0]
+ gopherType := "9"
+ if shortType == "text/html" {
+ gopherType = "h"
+ } else if genType == "text" {
+ gopherType = "0"
+ } else if shortType == "image/gif" {
+ gopherType = "g"
+ } else if genType == "image" {
+ gopherType = "I"
+ } else if genType == "audio" {
+ gopherType = "s"
+ }
+ return gopherType, nil
+}
+
func createIndex(selector string) (string, error) {
dirname := cfg.docRoot + selector
d, err := os.Open(dirname)
list += "1" + fi.Name() + "\t" + selector + pathSep + fi.Name() + loc
} else if fmode.IsRegular() {
// create a file reference
- // TODO: determine file type
- list += "0" + fi.Name() + " (" + strconv.FormatInt(fi.Size(), 10) + ")\t" + selector + pathSep + fi.Name() + loc
+ ftype, _ := guessFiletype(dirname + pathSep + fi.Name())
+ list += ftype + fi.Name() + " (" + strconv.FormatInt(fi.Size(), 10) + ")\t" + selector + pathSep + fi.Name() + loc
} else if fmode&os.ModeSymlink != 0 {
// create a reference according to link target
linktarget, _ := os.Readlink(dirname + pathSep + fi.Name())
list += "1" + fi.Name() + " -> " + linktarget + "\t" + selector + pathSep + fi.Name() + loc
} else if lfi.Mode().IsRegular() {
// link points to a regular file
- // TODO: determine file type
- list += "0" + fi.Name() + " -> " + linktarget + "\t" + selector + pathSep + fi.Name() + loc
+ ftype, _ := guessFiletype(dirname + pathSep + fi.Name())
+ list += ftype + fi.Name() + " -> " + linktarget + "\t" + selector + pathSep + fi.Name() + loc
}
}
}