Example #1
0
func (s *Server) LocalRequest(w http.ResponseWriter, r *http.Request, req *types.ListPathRequest) {

	voy, err := s.VoyFile.Load(req)
	if err != nil {
		WriteError(w, r, "load voyfile %s: %s", req, err)
		return
	}

	paths, err := s.VoyFile.ResolvePath(voy, req)
	if err != nil {
		WriteError(w, r, "voyfile resolve path %s: %s", req, err)
		return
	}

	log.Tracef("voyfile allow:%+v alias:%+v servers:%+v - paths %s", voy.Allow, voy.Alias, voy.Servers, paths)

	// dispatch handler to appropriate handler based on content
	hndlr := &handler.Handler{
		Username:  req.User,
		RootPath:  paths.RootPath,
		Path:      paths.RelPath,
		RelPath:   paths.RelPath,
		UrlPrefix: paths.UrlPrefix,
		LocalPath: paths.LocalPath,
		Template:  s.Template,
	}

	// call the path loader
	files, err := s.PathLoader.GetFiles(paths.LocalPath)
	if err != nil {
		WriteError(w, r, "path loader GetFiles: %s", err)
		return
	}

	// determine the layout
	layout, err := s.Layout.Resolve(voy, paths.LocalPath, files)
	if err != nil {
		WriteError(w, r, "resolve layout %s: %s", paths.LocalPath, err)
		return
	}

	hndlr.Layout = layout
	hndlr.Files = files

	log.Debugf("dispatch %s user:%s files:%d %s",
		hndlr.Layout, req.User, len(files), paths)

	reqHandler := s.Factory.MakeHandler(hndlr)
	if reqHandler == nil {
		handler.NewListHandler(hndlr).ServeHTTP(w, r)
	} else {
		reqHandler.ServeHTTP(w, r)
	}

}
Example #2
0
func (s *Server) RemoteRequest(w http.ResponseWriter, r *http.Request, req *types.ListPathRequest) {

	// dispatch handler to appropriate handler based on content

	log.Tracef("remote request %s", req)

	dopts := vapi.DefaultDialOptions()

	c, err := vapi.Connect(req.ServerName, dopts)
	if err != nil {
		WriteError(w, r, "remote:%s connect %s: %s", req.ServerName, req.Path, err)
		return
	}

	list_req := &vapi.ListRequest{
		User: req.User,
		Path: req.Path,
	}

	res, err := c.Client.ListFiles(s.Ctx, list_req)
	if err != nil {
		WriteError(w, r, "remote:%s list files %s: %s", req.ServerName, req.Path, err)
		return
	}
	log.Tracef("response layout:%s files:%d",
		res.Layout, len(res.Files))

	hndlr := &handler.Handler{
		Username:     req.User,
		UrlPrefix:    res.UrlPrefix,
		RemoteServer: res.RemoteServer,
		RelPath:      res.RelPath,
		LocalPath:    res.LocalPath,
		Template:     s.Template,
	}

	if res.IsDir == false {
		// deliver the object normally
		handler.NewListHandler(hndlr).ServeHTTP(w, r)
		return
	}

	hndlr.Layout = filetype.TypeFromString(res.Layout)
	files := make([]*types.File, 0)
	for _, file := range res.Files {
		f := &types.File{
			IsDir: file.IsDir,
			Name:  file.Name,
			Size:  file.Size,
		}
		files = append(files, f)
	}
	hndlr.Files = files

	log.Debugf("dispatch %s user:%s files:%d",
		hndlr.Layout, req.User, len(files))

	reqHandler := s.Factory.MakeHandler(hndlr)
	if reqHandler == nil {
		handler.NewListHandler(hndlr).ServeHTTP(w, r)
	} else {
		reqHandler.ServeHTTP(w, r)
	}

}