package handlers

import (
	
	

	
	
	
	
	
	
	
	
)

// PutFile puts file to remote end from local path
func ( *fiber.Ctx) error {
	 := []string{"server_id", "remote_path", "local_path"}

	for ,  := range  {
		if len(.FormValue()) < 1 {
			return logger.FiberError(fiber.StatusBadRequest, +" parameter is missing")
		}
	}

	,  := file.PutFileHandler(
		.Locals("user_id").(string),
		.FormValue("server_id"),
		.FormValue("remote_path"),
		.FormValue("local_path"),
	)
	if  != nil {
		return 
	}

	return .Type("json").SendString(`{
		"status":  200,
		"message": "file transfer completed successfully"
	}`)
}

// GetFile downloads file from remote path to local path
func ( *fiber.Ctx) error {
	 := []string{"server_id", "remote_path", "local_path"}

	for ,  := range  {
		if len(.FormValue()) < 1 {
			return logger.FiberError(fiber.StatusBadRequest, +" parameter is missing")
		}
	}

	,  := liman.GetServer(&models.Server{ID: .FormValue("server_id")})
	if  != nil {
		return 
	}

	,  := bridge.GetSession(
		.Locals("user_id").(string),
		.ID,
		.IPAddress,
	)
	if  != nil {
		return 
	}

	 := .CreateFileConnection(
		.Locals("user_id").(string),
		.ID,
		.IPAddress,
	)
	if ! {
		return logger.FiberError(fiber.StatusServiceUnavailable, "cannot establish file connection")
	}

	 := ""
	if .Os == "linux" {
		 = "/tmp/" + filepath.Base(.FormValue("remote_path"))
	} else {
		 = .WindowsPath + .FormValue("remote_path")
	}

	 = .Get(.FormValue("local_path"), )
	if  != nil {
		return 
	}

	return .Type("json").SendString(`{
		"status":  200,
		"message": "file transfer completed successfully"
	}`)
}

// DownloadFile returns file pointer
func ( *fiber.Ctx) error {
	 := []string{"server_id", "path", "extension_id"}

	for ,  := range  {
		if len(.FormValue()) < 1 {
			return logger.FiberError(fiber.StatusBadRequest, +" parameter is missing")
		}
	}
	if len(.FormValue("extension_id")) < 1 {
		return logger.FiberError(fiber.StatusBadRequest, "extension not found")
	}

	,  := liman.GetExtension(&models.Extension{
		ID: .FormValue("extension_id"),
	})

	if  != nil {
		return 
	}

	if .Status == "0" {
		return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable")
	}

	 := &models.Credentials{}
	if .RequireKey == "true" {
		,  = liman.GetCredentials(
			&models.User{
				ID: .Locals("user_id").(string),
			},
			&models.Server{
				ID: .FormValue("server_id"),
			},
		)

		if  != nil || len(.Username) < 1 {
			return logger.FiberError(fiber.StatusForbidden, "you need a key to use this extension")
		}
	}

	 := helpers.GetFormData()

	 := .FormValue("token")
	if len(.FormValue("liman-token")) > 0 {
		 = .FormValue("liman-token")
	}

	_,  = sandbox.GenerateCommand(
		,
		,
		&models.CommandParams{
			TargetFunction: "",
			User:           .Locals("user_id").(string),
			Extension:      .FormValue("extension_id"),
			Server:         .FormValue("server_id"),
			RequestData:    ,
			Token:          ,
			BaseURL:        .FormValue("lmnbaseurl", .Get("origin")),
			Locale:         .FormValue("locale", helpers.Env("APP_LANG", "tr")),
			LogID:          .Locals("log_id").(string),
		},
	)
	if  != nil {
		return 
	}

	 := filepath.Clean(.FormValue("path"))
	 = "/liman/extensions/" + strings.ToLower(.Name) + 
	 = strings.ReplaceAll(, "../", "/")
	 = strings.ReplaceAll(, "./", "/")
	 = strings.ReplaceAll(, "..", "")

	return .Download()
}