FexTools.psm1



function New-Connection
{
    [cmdletbinding()]
    param
    (
        [string]$apiHost,
        [string]$login,
        [string]$password
    )

    $useragent = "FEX Sync (WINDOWS)"

    Try
    {
        $useragent = [System.String]::Format("FEX Sync ({0})", [Net.Fex.Api.Connection]::GetOSName());
    }
    Catch
    {
    }

    [Net.Fex.Api.IHttpClient] $httpClient = new-object Net.Fex.Api.HttpClientWrapper
    [Net.Fex.Api.IConnection] $connection = new-object Net.Fex.Api.Connection $httpClient, $apiHost, $useragent

    $userInfo = $connection.SignIn($login, $password, $false)

    return $connection
}

function Send-Folder {
    [cmdletbinding()]
    param
    (
        [Net.Fex.Api.IConnection]$connection,
        [string]$token,
        [string]$uploadfolder
    )
        $serverIds = New-Object 'System.Collections.Generic.Dictionary[String,String]'
        $alldirs = [System.IO.Directory]::GetDirectories($uploadfolder, "*.*", [System.IO.SearchOption]::AllDirectories)
        $allfiles = [System.IO.Directory]::GetFiles($uploadfolder, "*.*", [System.IO.SearchOption]::AllDirectories)

        Foreach ($dir IN $alldirs)
        {
            $parentPath = [System.IO.Path]::GetDirectoryName($dir)
            $relativePath = $dir.Replace($uploadfolder, "").Trim([System.IO.Path]::DirectorySeparatorChar)
            $relativeParentPath = [System.IO.Path]::GetDirectoryName($relativePath)
            $parentId = $null;
            
            $parentNotEmpty = ![string]::IsNullOrWhitespace($relativeParentPath)

            if ($parentNotEmpty)
            {
                if($serverIds.ContainsKey($relativeParentPath))
                {
                    $parentId = $serverIds[$relativeParentPath];
                }                
                else
                {
                    $parentId = $connection.GetItem($token, $relativeParentPath).UploadId
                    $serverIds.Add($relativeParentPath, $parentId);
                }
            }

            $uploadfolderName = [System.IO.Path]::GetFileName($dir);
            $item = $connection.GetItem($token, $relativePath)

            if ([object]::Equals($item, $null))
            {
                $uploadId = $connection.CreateFolder($token, $parentId, $uploadfolderName);
                Write-Host "Created remote folder $relativePath : $uploadId"
                $serverIds.Add($relativePath, $uploadId);
            }
            else
            {
                $serverIds.Add($relativePath, $item.UploadId);
            }
        }

        Foreach ($dir IN $allfiles)
        {
            $file = $dir
            #Write-Host "file $file"
            
            $parentPath = [System.IO.Path]::GetDirectoryName($file)
            $relativePath = $dir.Replace($uploadfolder, "").Trim([System.IO.Path]::DirectorySeparatorChar)
            $relativeParentPath = [System.IO.Path]::GetDirectoryName($relativePath)
            $parentId = $null;
            
            $parentNotEmpty = ![string]::IsNullOrWhitespace($relativeParentPath)

            if ($parentNotEmpty)
            {
                if($serverIds.ContainsKey($relativeParentPath))
                {
                    $parentId = $serverIds[$relativeParentPath];
                }                
                else
                {
                    $parentId = $connection.GetItem($token, $relativeParentPath).UploadId
                    $serverIds.Add($relativeParentPath, $parentId);
                }
            }

            $item = $connection.GetItem($token, $relativePath)

            if ([object]::Equals($item, $null))
            {
                Write-Host "Uploading file (token=$token, parentId=$parentId, path=$file"
                $z = $connection.Upload($token, $parentId, $file, $null)
                Write-Host "Uploaded file (token=$token, parentId=$parentId, path=$file"
            }
            else
            {
                $serverIds.Add($relativePath, $item.UploadId);
            }
        }
}


function CreateFoldersRecursive
{
    param( $item)

            $fullPath = [System.IO.Path]::Combine($downloadfolder, $item.Path)
            if ($item.Object.IsFolder)
            {
                if (![System.IO.Directory]::Exists($fullPath))
                {
                    $di = [System.IO.Directory]::CreateDirectory($fullPath)
                }

                Foreach ($child IN $item.Childern)
                {
                    CreateFoldersRecursive($child)
                }
            }
}


function CreateFilesRecursive
{
    param( $item)

            $fullPath = [System.IO.Path]::Combine($downloadfolder, $item.Path)
            if ($item.Object.IsFolder)
            {
                if (![System.IO.Directory]::Exists($fullPath))
                {
                    $di = [System.IO.Directory]::CreateDirectory($fullPath)
                }

                Foreach ($child IN $item.Childern)
                {
                    CreateFilesRecursive($child)
                }
            }
            else
            {
                if (![System.IO.File]::Exists($fullPath))
                {
                    $connection.Get($token, $item.UploadId, $fullPath);
                }
            }
}


function Receive-Folder {
    [cmdletbinding()]
    param
    (
        [Net.Fex.Api.IConnection]$connection,
        [string]$token,
        [string]$downloadfolder
    )

        $cmd = New-Object Net.Fex.Api.CommandBuildRemoteTree ($token)
        $cmd.Execute($connection)

        Foreach ($item IN $cmd.Result.List[0].Childern)
        {
            CreateFoldersRecursive($item)
        }

        Foreach ($item IN $cmd.Result.List[0].Childern)
        {
            CreateFilesRecursive($item)
        }

}

export-modulemember -function New-Connection
export-modulemember -function Send-Folder
export-modulemember -function Receive-Folder