コード例 #1
0
func (s *FileBrowserController) GetDir(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	server, payload, err := getServer(r, "FORM")

	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	if server.RecordID() != nil {
		var result []colonycore.FileInfo

		if server.ServerType == SERVER_NODE {
			if payload.Search != "" {
				setting, err := sshConnect(&server)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				// search in app-path
				listApp, err := sshclient.Search(setting, server.AppPath, true, payload.Search)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				resultApp, err := colonycore.ConstructFileInfo(listApp, server.AppPath)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				// search in data-path
				listData, err := sshclient.Search(setting, server.DataPath, true, payload.Search)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				resultData, err := colonycore.ConstructFileInfo(listData, server.DataPath)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				if len(resultApp) > 0 {
					resultApp = colonycore.ConstructSearchResult(resultApp, server.AppPath)
					result = append(result, resultApp...)
				}
				if len(resultData) > 0 {
					resultData = colonycore.ConstructSearchResult(resultData, server.DataPath)
					result = append(result, resultData...)
				}

			} else if payload.Path == "" {
				appFolder := colonycore.FileInfo{
					Name:       "APP",
					Path:       server.AppPath,
					IsDir:      true,
					IsEditable: false,
				}

				dataFolder := colonycore.FileInfo{
					Name:       "DATA",
					Path:       server.DataPath,
					IsDir:      true,
					IsEditable: false,
				}

				result = append(result, appFolder)
				result = append(result, dataFolder)
			} else {
				setting, err := sshConnect(&server)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				list, err := sshclient.List(setting, payload.Path, true)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				result, err = colonycore.ConstructFileInfo(list, payload.Path)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
			}
		} else if server.ServerType == SERVER_HDFS {
			h := setHDFSConnection(server.Host, server.SSHUser)

			//check whether SourcePath type is directory or file
			if payload.Path == "" {
				payload.Path = "/"
			}

			res, err := h.List(payload.Path)
			if err != nil {
				return helper.CreateResult(false, nil, err.Error())
			}

			for _, files := range res.FileStatuses.FileStatus {
				var xNode colonycore.FileInfo

				xNode.Name = files.PathSuffix
				xNode.Size = float64(files.Length)
				xNode.Group = files.Group
				xNode.Permissions = files.Permission
				xNode.User = files.Owner
				xNode.Path = strings.Replace(payload.Path+"/", "//", "/", -1) + files.PathSuffix

				if files.Type == "FILE" {
					xNode.IsDir = false
					xNode.IsEditable = true
				} else {
					xNode.IsDir = true

					if payload.Path == "/" {
						xNode.IsEditable = false
					} else {
						xNode.IsEditable = true
					}
				}

				result = append(result, xNode)
			}
		}

		return helper.CreateResult(true, result, "")
	}

	return helper.CreateResult(false, nil, "")
}
コード例 #2
0
ファイル: filebrowser.go プロジェクト: eaciit/colony-manager
func (s *FileBrowserController) GetDir(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	server, payload, err := getServer(r, "FORM")
	fmt.Printf("server %#v\n", server)

	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	if server.RecordID() != nil {
		var result []colonycore.FileInfo
		var tempResult []ListDir

		if server.ServerType == SERVER_NODE {
			if payload.Search != "" {
				setting, _, err := (&server).Connect()

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				// search in app-path
				listApp, err := sshclient.Search(setting, server.AppPath, true, payload.Search)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				resultApp, err := colonycore.ConstructFileInfo(listApp, server.AppPath)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				// search in data-path
				listData, err := sshclient.Search(setting, server.DataPath, true, payload.Search)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				resultData, err := colonycore.ConstructFileInfo(listData, server.DataPath)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				if len(resultApp) > 0 {
					resultApp = colonycore.ConstructSearchResult(resultApp, server.AppPath)
					result = append(result, resultApp...)
				}
				if len(resultData) > 0 {
					resultData = colonycore.ConstructSearchResult(resultData, server.DataPath)
					result = append(result, resultData...)
				}

			} else if payload.Path == "" {
				appFolder := colonycore.FileInfo{
					Name:       "APP",
					Path:       server.AppPath,
					IsDir:      true,
					IsEditable: false,
				}

				dataFolder := colonycore.FileInfo{
					Name:       "DATA",
					Path:       server.DataPath,
					IsDir:      true,
					IsEditable: false,
				}

				result = append(result, appFolder)
				result = append(result, dataFolder)
			} else {
				setting, _, err := (&server).Connect()

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				list, err := sshclient.List(setting, payload.Path, true)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				result, err = colonycore.ConstructFileInfo(list, payload.Path)

				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
			}
		} else if server.ServerType == SERVER_HDFS {
			fmt.Println("=----", server.ServiceHDFS)
			h := setHDFSConnection(server.ServiceHDFS.Host, server.ServiceHDFS.User)

			//check whether SourcePath type is directory or file
			var depth = 1
			var dir = ""
			var isComplete = false

			if payload.Path == "" {
				payload.Path = "/"
				dir = payload.Path
			}

			if payload.Search != "" {
				for !isComplete {
					if depth == 10 {
						isComplete = true
					}

					DepthList := GetDirbyDepth(tempResult, depth)
					depth++

					if len(DepthList) == 0 {
						if dir != "/" {
							isComplete = true
						} else {
							Dirs, _ := GetDirContent(dir, h)

							for _, singleDir := range Dirs {
								if singleDir.IsDir {
									var res ListDir
									res.Dir = singleDir
									res.DirDepth = depth
									tempResult = append(tempResult, res)
								}

								if strings.Contains(singleDir.Name, payload.Search) {
									result = append(result, singleDir)
								}
							}
						}
					} else {
						for _, singleDepthList := range DepthList {
							dir = singleDepthList.Dir.Path

							Dirs, _ := GetDirContent(dir, h)

							for _, singleDir := range Dirs {
								if singleDir.IsDir {
									var res ListDir
									res.Dir = singleDir
									res.DirDepth = depth
									tempResult = append(tempResult, res)
								}

								if strings.Contains(singleDir.Name, payload.Search) {
									result = append(result, singleDir)
								}
							}
						}
					}
				}
			} else {
				result, err = GetDirContent(payload.Path, h)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
			}
		}

		return helper.CreateResult(true, result, "")
	}

	return helper.CreateResult(false, nil, "")
}