Beispiel #1
0
// Implementation of http.Handler, to pass to http.ListenAndServe,
// which is used in ServerInfo.StartServer.
// /               - Pass the JSON of the parsed directory
// /quit           - kill the server
// /file?p=file    - read the file after ? with the ServerInfo.RelativeFilePath directory
// You can add q=func to / and /file to return the values as jsonp
func (serverInfo *ServerInfo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Println("REQUESTED on dirserver: " + r.URL.RequestURI())
	jsonutils.SetJsonHeader(&w)

	switch val := r.URL.Path; {
	case val == "/":
		if jsonp := r.URL.Query().Get("q"); len(jsonp) > 0 {
			fmt.Fprintf(w, "%s(%s)", jsonp, serverInfo.DirectoryTree.ToJson())
		} else {
			fmt.Fprintf(w, "%s", serverInfo.DirectoryTree.ToJson())
		}

	case val == "/quit":
		os.Exit(0)

	case val == "/file":
		path := r.URL.Query().Get("p")
		if of := fileutils.Create(serverInfo.RelativeFilePath + path); of != nil && of.IsInDirectory(serverInfo.RelativeFilePath) {
			if jsonp := r.URL.Query().Get("q"); len(jsonp) > 0 {
				o := of.ContentOfFile()
				fmt.Fprintf(w, "%s(%s)", jsonp, jsonutils.ConvertMultilineToJSONString(o))
			} else {
				fmt.Fprintf(w, of.ContentOfFile())
			}
		}
	}
}
Beispiel #2
0
// Takes a dir, and gives back a DirTree with all its files, recursively
// through the sub directories.
// The output will have paths for the files. relativePath says from where they should start.
func ParseDir(relativePath string, path string) DirTree {
	node := DirTree{make(map[string]string), []DirTree{}}

	files, _ := filepath.Glob(path + "*")
	if files == nil {
		log.Print("ERROR: No files found at " + path + "*")
		return node
	}

	node.ValueMap["DirName"] = fileutils.Create(path).Basename()
	node = parseAllFiles(relativePath, files, node)

	return node
}
Beispiel #3
0
// Adds all the files, and directories, onto the DirTree passed
// relativePath is used to say from where the file path should start.
func parseAllFiles(relativePath string, files []string, currentNode DirTree) DirTree {
	for _, file := range files {
		of := fileutils.Create(file)
		if of == nil {
			continue
		}
		log.Println("PARSING: " + file)
		if of.IsDir() {
			child := ParseDir(relativePath, file+"/")
			currentNode.Children = append(currentNode.Children, child)
		} else {
			file = strings.Replace(file, relativePath, "", -1)
			currentNode.ValueMap[of.Basename()] = file //of.GetContentOfFile()
		}
	}
	return currentNode
}