Esempio n. 1
0
func deleteAll(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	lg, _ := loghttp.BuffLoggerUniversal(w, r)

	err := r.ParseForm()
	lg(err)

	wpf(w, tplx.ExecTplHelper(tplx.Head, map[string]interface{}{"HtmlTitle": "Delete all filesystem data"}))
	defer wpf(w, tplx.Foot)

	confirm := r.FormValue("confirm")
	if confirm != "yes" {
		wpf(w, "All dsfs contents are deletes. All memfs contents are deleted<br>\n")
		wpf(w, "Put a get param into the URL ?confirm - and set it to 'yes'<br>\n")
		wpf(w, "Put a get param 'mountname' into url; i.e. mountname=mntftch<br>\n")
		return
	}

	wpf(w, "<pre>\n")
	defer wpf(w, "\n</pre>")

	//
	//
	fs := dsfs.New(
		dsfs.AeContext(appengine.NewContext(r)),
	)

	mountName := r.FormValue("mountname")
	if mountName != "" {
		wpf(w, "mountame = "+mountName+"\n")
		fs = dsfs.New(
			dsfs.AeContext(appengine.NewContext(r)),
			dsfs.MountName(mountName),
		)
	}

	wpf(w, "dsfs:\n")
	msg, err := fs.DeleteAll()
	if err != nil {
		wpf(w, "err during delete %v\n", err)
	}
	wpf(w, msg)

	memMapFileSys = memfs.New()
	wpf(w, "\n")
	wpf(w, "memMapFs new")

	// cleanup must be manual
	osFileSys = osfs.New()

}
Esempio n. 2
0
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
}
Esempio n. 3
0
// We cannot use http.FileServer(http.Dir("./css/") to dispatch our dsfs files.
// We need the appengine context to initialize dsfs.
//
// Thus we re-implement a serveFile method:
func ServeDsFsFile(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {

	urlPath := m["dir"].(string)
	if len(urlPath) > 0 {
		urlPath = urlPath[1:]
	}

	prefix := "/mnt00"

	pos := strings.Index(urlPath, "/")
	if pos > 0 {
		prefix = "/" + urlPath[:pos]
	}
	if pos == -1 {
		prefix = "/" + urlPath
	}

	fs2 := dsfs.New(
		dsfs.MountName(prefix[1:]),
		dsfs.AeContext(appengine.NewContext(r)),
	)

	fileserver.FsiFileServer(w, r, fileserver.Options{FS: fs2, Prefix: prefix + "/"})

}
Esempio n. 4
0
//
// 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)

}
Esempio n. 5
0
func getFS(c context.Context, mnt string) fsi.FileSystem {

	var fs fsi.FileSystem

	switch whichType {
	case 0:
		// must be re-instantiated for each request
		dsFileSys := dsfs.New(dsfs.MountName(mnt), dsfs.AeContext(c))
		fs = fsi.FileSystem(dsFileSys)
	case 1:
		fs = fsi.FileSystem(osFileSys)
	case 2:
		// re-instantiation would delete everything
		fs = fsi.FileSystem(memMapFileSys)
	default:
		panic("invalid whichType ")
	}

	return fs
}
Esempio n. 6
0
func GetFS(c context.Context, whichType int) (fs fsi.FileSystem) {

	switch whichType {
	case 0:
		// re-instantiation would delete contents
		fs = fsi.FileSystem(memMapFileSys)
	case 1:
		// must be re-instantiated for each request
		dsFileSys := dsfs.New(dsfs.DirSort("byDateDesc"), dsfs.MountName("mntTest"), dsfs.AeContext(c))
		fs = fsi.FileSystem(dsFileSys)
	case 2:

		osFileSys := osfs.New(osfs.DirSort("byDateDesc"))
		fs = fsi.FileSystem(osFileSys)
		os.Chdir(logDir)
	default:
		panic("invalid whichType ")
	}

	return
}
Esempio n. 7
0
func TemplateFromHugoPage(w http.ResponseWriter, r *http.Request) string {

	mp.Lock()
	if tpl, ok := mp.mp[pathToTmpl]; ok {
		mp.Unlock()
		return tpl
	}
	mp.Unlock()

	lg, _ := loghttp.BuffLoggerUniversal(w, r)

	//
	fs2 := dsfs.New(
		dsfs.MountName(TplPrefix[1:]),
		dsfs.AeContext(appengine.NewContext(r)),
	)
	fs1.SetOption(
		memfs.ShadowFS(fs2),
	)

	bts, err := fs1.ReadFile(pathToTmpl)
	if err != nil {
		lg(err)
		bts = hugoTplFallback
	}

	bts = bytes.Replace(bts, []byte("[REPLACE_TITLE]"), []byte("{{ .HtmlTitle }}"), -1)
	bts = bytes.Replace(bts, []byte("[REPLACE_DESC]"), []byte("{{ .HtmlDescription }}"), -1)
	bts = bytes.Replace(bts, []byte("</head>"), []byte("{{ .HtmlHeaders }}\n</head>"), -1)
	bts = bytes.Replace(bts, []byte("<p>[REPLACE_CONTENT]</p>"), []byte("{{ .HtmlContent }}"), -1)
	bts = bytes.Replace(bts, []byte("[REPLACE_CONTENT]"), []byte("{{ .HtmlContent }}"), -1)
	bts = bytes.Replace(bts, []byte("<span id='REPLACE_FOOTER'></span>"), []byte("{{ .HtmlFooter }}"), -1)

	mp.Lock()
	mp.mp[pathToTmpl] = string(bts)
	mp.Unlock()

	return mp.mp[pathToTmpl]

}
Esempio n. 8
0
// GetFS instantiates a filesystem, depending on whichtype
func GetFS(c context.Context) (fs fsi.FileSystem) {
	switch whichType {
	case 0:
		// must be re-instantiated for each request
		docRoot = ""
		dsFileSys := dsfs.New(dsfs.DirSort("byDateDesc"), dsfs.MountName(mountName), dsfs.AeContext(c))
		fs = fsi.FileSystem(dsFileSys)
	case 1:
		docRoot = "c:/docroot/"
		os.Chdir(docRoot)
		osFileSys := osfs.New(osfs.DirSort("byDateDesc"))
		fs = fsi.FileSystem(osFileSys)
	case 2:
		// re-instantiation would delete contents
		docRoot = ""
		fs = fsi.FileSystem(memMapFileSys)
	default:
		panic("invalid whichType ")
	}

	return
}
Esempio n. 9
0
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")

	}

}