func initFileSystems() (fss []fsi.FileSystem, c aetest.Context) { var err error c, err = aetest.NewContext(nil) if err != nil { log.Fatal(err) } // defer c.Close() // Not here, but instead at the start of the test-funcs // We cant make variadic options generic, // since they need the concrete filesystem type. fs1 := dsfs.New( dsfs.MountName(dsfs.MountPointLast()), dsfs.AeContext(c), ) fs3 := osfs.New() fs4 := memfs.New( memfs.Ident("m"), ) fss = []fsi.FileSystem{fs1, fs3, fs4} return fss, c }
// // A static fileserver is NOT working // Since we need an appengine.context // // UNUSED, NOT WORKING func serveDsFs(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { // Examples http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./img/")))) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/")))) http.Handle("/tmp/", http.StripPrefix("/tmp/", http.FileServer(http.Dir("c:\\temp")))) c := appengine.NewContext(r) cx = c mountPoint = dsfs.MountPointLast() fs1 := dsfs.New( dsfs.MountName(mountPoint), dsfs.AeContext(c), ) httpFSys := &httpfs.HttpFs{SourceFs: fs1} // HERE is the trouble! http.Handle("/tmp1/", http.StripPrefix("/tmp1/", http.FileServer(httpFSys.Dir("./")))) wpf(w, "serving %v", mountPoint) // time.Sleep(5 * time.Second) }
func lastMountPoint(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "last Mountpoint"})) defer wpf(w, tplx.Foot) wpf(w, "<pre>\n") defer wpf(w, "\n</pre>") wpf(w, "reset %v\n", dsfs.MountPointLast()) }
func DeleteSubtree(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { lg, lge := loghttp.Logger(w, r) err := r.ParseForm() lge(err) wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Delete Subtree for curr FS"})) defer wpf(w, tplx.Foot) if r.Method == "POST" { wpf(w, "<pre>\n") defer wpf(w, "\n</pre>") mountPoint := dsfs.MountPointLast() if len(r.FormValue("mountname")) > 0 { mountPoint = r.FormValue("mountname") } lg("mount point is %v", mountPoint) pathPrefix := "impossible-value" if len(r.FormValue("pathprefix")) > 0 { pathPrefix = r.FormValue("pathprefix") } lg("pathprefix is %v", pathPrefix) fs := getFS(appengine.NewContext(r), mountPoint) lg("created fs %v-%v ", fs.Name(), fs.String()) lg("removing %q - and its subtree ...", pathPrefix) err := fs.RemoveAll(pathPrefix) lge(err) errMc := memcache.Flush(appengine.NewContext(r)) lge(errMc) if err == nil && errMc == nil { lg("success") } } else { tData := map[string]string{"Url": UriDeleteSubtree} err := tplBase.ExecuteTemplate(w, "tplName01", tData) lge(err) } }
func receiveUpload(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { lg, _ := loghttp.Logger(w, r) c := appengine.NewContext(r) // parsing multipart before anything else err := r.ParseMultipartForm(1024 * 1024 * 2) if err != nil { lg("Multipart parsing failed: %v", err) return } wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Receive an Upload"})) defer wpf(w, tplx.Foot) wpf(w, "<pre>") defer wpf(w, "</pre>") fields := []string{"getparam1", "mountname", "description"} for _, v := range fields { lg("%12v => %q", v, r.FormValue(v)) } mountPoint := dsfs.MountPointLast() if len(r.FormValue("mountname")) > 0 { mountPoint = r.FormValue("mountname") } lg("mount point is %v", mountPoint) fs1 := dsfs.New( dsfs.MountName(mountPoint), dsfs.AeContext(c), ) // As closure, since we cannot define dsfs.dsFileSys as parameter funcSave := func(argName string, data []byte) (error, *bytes.Buffer) { b1 := new(bytes.Buffer) fs1 := dsfs.New( dsfs.MountName(mountPoint), dsfs.AeContext(c), ) dir, bname := fs1.SplitX(argName) err := fs1.MkdirAll(dir, 0777) wpf(b1, "mkdir %v - %v\n", dir, err) if err != nil { return err, b1 } err = fs1.WriteFile(path.Join(dir, bname), data, 0777) wpf(b1, "saved file content to %v - %v\n", argName, err) return err, b1 } ff := "filefield" file, handler, err := r.FormFile(ff) if err != nil { lg("error calling FormFile from %q => %v", ff, err) return } if handler == nil { lg("no multipart file %q", ff) } else { lg("extracted file %v", handler.Filename) data, err := ioutil.ReadAll(file) if err != nil { lg("ReadAll on uploaded file failed: %v", err) return } defer file.Close() lg("extracted file content; %v bytes", len(data)) newFilename := docRootDataStore + handler.Filename ext := path.Ext(newFilename) if ext == ".zip" { lg("found zip - treat as dir-tree %q", newFilename) r, err := zip.NewReader(file, int64(len(data))) if err != nil { lg("open as zip failed: %v", err) return } for _, f := range r.File { newFilename = docRootDataStore + f.Name dir, bname := fs1.SplitX(newFilename) if f.FileInfo().IsDir() { lg("\t dir %s", newFilename) err := fs1.MkdirAll(path.Join(dir, bname), 0777) if err != nil { lg("MkdirAll %v failed: %v", newFilename, err) return } } else { lg("\t file %s", newFilename) rc, err := f.Open() if err != nil { return } defer func(rc io.ReadCloser) { if err := rc.Close(); err != nil { panic(err) } }(rc) bts := new(bytes.Buffer) size, err := io.Copy(bts, rc) if err != nil { lg("Could not copy from zipped file %v: %v", newFilename, err) return } err = common.WriteFile(fsi.FileSystem(fs1), path.Join(dir, bname), bts.Bytes()) // err = fs1.WriteFile(path.Join(dir, bname), bts.Bytes(), 0777) if err != nil { lg("WriteFile of zipped file %v failed: %v", newFilename, err) return } lg("\t saved %v - %v Bytes", newFilename, size) } } } else { err, b2 := funcSave(newFilename, data) lg("%s", b2) if err != nil { return } } errMc := memcache.Flush(appengine.NewContext(r)) if errMc != nil { lg("Error flushing memache: %v", errMc) return } lg("--------------------\n") } }