Beispiel #1
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
}
Beispiel #2
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
}
Beispiel #3
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
}
Beispiel #4
0
func init() {

	// forcing our implementations
	// to comply with our interfaces

	f := DsFile{}
	ifa := fsi.File(&f)
	_ = ifa

	ifi := os.FileInfo(&f)
	_ = ifi

	fs := dsFileSys{}
	ifs := fsi.FileSystem(&fs)
	_ = ifs

}
Beispiel #5
0
func init() {

	// forcing our implementations
	// to comply with our interfaces

	f := InMemoryFile{}
	ifa := fsi.File(&f)
	_ = ifa

	fi := InMemoryFileInfo{}
	ifi := os.FileInfo(&fi)
	_ = ifi

	fs := memMapFs{}
	ifs := fsi.FileSystem(&fs)
	_ = ifs

}
Beispiel #6
0
func init() {

	// forcing our implementations
	// to comply with our interfaces

	f := os.File{}
	ifa := fsi.File(&f)
	_ = ifa

	var fi os.FileInfo
	ifi := os.FileInfo(fi) // of course idiotic, but we keep the pattern
	_ = ifi

	fs := osFileSys{}
	ifs := fsi.FileSystem(&fs)
	_ = ifs

}
Beispiel #7
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")

	}

}
Beispiel #8
0
	"net/http"

	"github.com/pbberlin/tools/net/http/routes"
	"github.com/pbberlin/tools/os/fsi"
	"github.com/pbberlin/tools/os/fsi/httpfs"
	"github.com/pbberlin/tools/os/fsi/memfs"
)

// parallel fetchers routines
const numWorkers = 3

var docRoot = ""  // no relative path, 'cause working dir too flippant
var whichType = 0 // which type of filesystem, default is dsfs

var memMapFileSys = memfs.New(memfs.DirSort("byDateDesc"))             // package variable required as "persistence"
var httpFSys = &httpfs.HttpFs{SourceFs: fsi.FileSystem(memMapFileSys)} // memMap is always ready
var fileserver1 = http.FileServer(httpFSys.Dir(docRoot))

const mountName = "mntftch"

const uriSetType = "/fetch/set-fs-type"
const UriMountNameY = "/" + mountName + "/serve-file/"

const uriFetchCommandReceiver = "/fetch/command-receive"
const uriFetchCommandSender = "/fetch/command-send"

var RepoURL = routes.AppHost() + UriMountNameY

var msg = []byte(`<p>This is an embedded static http server.</p>
<p>
It serves previously downloaded pages<br>