Example #1
0
func (s *FileBrowserController) NewFile(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	server, payload, err := getServer(r, "PAYLOAD")
	fmt.Println("aaaaa")

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

	if server.RecordID() != nil {
		if payload.Path != "" {
			if server.ServerType == SERVER_NODE {
				setting, err := sshConnect(&server)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				err = sshclient.MakeFile(setting, " ", payload.Path, "", false)

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

				//create file on local
				tempPath := strings.Replace(GetHomeDir()+"/", "//", "/", -1)

				if tempPath == "" {
					return helper.CreateResult(false, nil, "No Temporary Directory")
				}
				FileName := strings.Split(payload.Path, "/")[len(strings.Split(payload.Path, "/"))-1]

				file, err := os.Create(tempPath + FileName)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				defer file.Close()

				//put new file to hdfs
				err = h.Put(tempPath+FileName, strings.Replace(payload.Path+"/", "//", "/", -1), "", nil, &server)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				//remove file on local
				// err = os.Remove(tempPath + FileName)
				// if err != nil {
				// 	return helper.CreateResult(false, nil, err.Error())
				// }
				return helper.CreateResult(true, "", "")
			}
		}

		return helper.CreateResult(false, nil, "Please check your param")
	}

	return helper.CreateResult(false, nil, "")
}
Example #2
0
func (s *FileBrowserController) Edit(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	server, payload, err := getServer(r, "PAYLOAD")

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

	if server.RecordID() != nil {
		if payload.Path != "" && payload.Contents != "" && payload.Permission != "" {
			if server.ServerType == SERVER_NODE {
				setting, err := sshConnect(&server)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				err = sshclient.MakeFile(setting, payload.Contents, payload.Path, payload.Permission, false)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				return helper.CreateResult(true, nil, "")
			} else if server.ServerType == SERVER_HDFS {
				DestPath := payload.Path
				SourcePath := strings.Replace(GetHomeDir()+"/", "//", "/", -1) + strings.Split(DestPath, "/")[len(strings.Split(DestPath, "/"))-1]
				err := ioutil.WriteFile(SourcePath, []byte(payload.Contents), 0755)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}

				h := setHDFSConnection(server.Host, server.SSHUser)
				err = h.Put(SourcePath, DestPath, "", map[string]string{"overwrite": "true"}, &server)
				if err != nil {
					return helper.CreateResult(false, nil, err.Error())
				}
				return helper.CreateResult(true, "", "")
			}
		}

		return helper.CreateResult(false, nil, "Please check your param")
	}

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