func main() { fmt.Println("Starting PMFS") // Mount a memory root file system // Add some content, read it back var f fs.RootFileSystem var mh memory.MemoryFileSystem f.Init(&mh, "") go func() { for msg := range f.Notification { fmt.Println(msg) } }() f.Format(100, 100) f.WriteFile("/test/alan", []byte("Hello world this is a test")) f.AppendFile("/test/alan", []byte("\nThis is line 2, part of version 2")) f.AppendFile("/test/other", []byte("\nA new file")) f.AppendFile("/other/one", []byte("\nHere we go again")) f.AppendFile("/other/three", []byte("\nHello world")) names, _ := f.ListDirectory("/test") for y := range names { fmt.Println(names[y]) } x, err := f.ReadFile("/test/alan") if err != nil { fmt.Println(err) } else { fmt.Printf("Data is %v\n", string(x)) } fn, e2 := f.StatFile("/test/alan") if e2 != nil { fmt.Println(e2) } else { stats := fn.Stats fmt.Printf("Created : %v\nModified : %v\nAccessed : %v\n", stats.Created, stats.Modified, stats.Accessed) } go web.StartAPIServer(&f) web.StartWebServer() }
func getFunc(w http.ResponseWriter, r *http.Request, filesys *fs.RootFileSystem) { // This can be two things // 1 get of a file, so dump the contents // 2 get of a folder, so construct some nice json fileNode, dirNode, err := filesys.GetFileOrDirectory(r.URL.Path, false) if err != nil { writeError(w, err) } else { w.WriteHeader(http.StatusOK) if fileNode != nil { var x []byte x, err = filesys.ReadFile(r.URL.Path) fmt.Fprintf(w, "%v", string(x)) } else { // Need to get file directory structure as a json object dirStructure := getDirStructure(r.URL.Path, dirNode, filesys) var b []byte b, err = json.MarshalIndent(dirStructure, "", " ") fmt.Fprintf(w, "%v", string(b)) } } }