Use-Uptobox.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
Function Get-UptoboxFileAsync { <# .SYNOPSIS main function used to download a file from UpToBox online file hosting service using multi threading and asynchronous capabilities .DESCRIPTION main function used to download a file from UpToBox online file hosting service using multi threading and asynchronous capabilities this function requires the Microsoft PowerShell Module 'threadjob' To use this function you must have an account and a valid API key .PARAMETER filecode -filecode string use an uptobox filecode to target a file to download .PARAMETER url -url string use an uptobox file url to download a file .PARAMETER outputfolder -outputfolder string path to a target folder where the file will be downloaded. if no target folder is set, a default value is used ($home\downloads) .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .OUTPUTS TypeName: PSuptobox .EXAMPLE Get-UptoboxFileAsync -url https://uptobox.com/xxxxx11yyyy2 start a multi thread / asynchronous download for filecode xxxxx11yyyy2 and download it in default folder .EXAMPLE Get-UptoboxFileAsync -filecode xxxxx11yyyy2 -outputfolder c:\MyFolder start a multi thread / asynchronous download for filecode xxxxx11yyyy2 and download it in c:\MyFolder #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [URI]$url, [parameter(Mandatory=$false)] [ValidateLength(12,12)] [string]$filecode, [parameter(Mandatory=$false)] [ValidateScript({(test-path $_)})] [string]$outputfolder ) process { if ($Host.Version.Major -lt 6) { throw "please use PowerShell in last version to use the multithreading feature" } if (!(Get-Module -Name threadjob)) { try { import-module -Name threadjob -Force } catch { throw "please install PowerShell module threadjob first - 'Install-Module -Name threadjob'" } } if (!($global:uptoboxAPIKey)) { throw "please set an api key using 'Set-UptoboxAPIKey'" } $inputobject = [pscustomobject]@{ APIKey = $global:uptoboxAPIKey.clone() url = $PSBoundParameters["url"] filecode = $PSBoundParameters["filecode"] outputfolder = $PSBoundParameters["outputfolder"] proxyparams = if ($global:uptoboxProxyParams) {$global:uptoboxProxyParams.clone()} } $actionblock = { $settings = $input | ConvertTo-Json | ConvertFrom-Json Set-UptoboxAPIKey -APIKey $settings.APIKey if ($settings.proxyparams) { $global:uptoboxProxyParams = $settings.proxyparams } if ($settings.url) { if ($settings.outputfolder) { Get-UptoboxFile -url $settings.url -outputfolder $settings.outputfolder } else { Get-UptoboxFile -url $settings.url } } elseif ($settings.filecode) { if ($settings.outputfolder) { Get-UptoboxFile -filecode $settings.filecode -outputfolder $settings.outputfolder } else { Get-UptoboxFile -filecode $settings.filecode } } } $intializationblock = { Import-Module Use-Uptobox import-module Microsoft.PowerShell.Management import-module Microsoft.PowerShell.Utility } Start-ThreadJob -ScriptBlock $actionblock -InputObject $inputobject -StreamingHost $Host -InitializationScript $intializationblock -Name "UptoBox $(new-guid)" } } Function Get-UptoboxFile { <# .SYNOPSIS main function used to download a file from UpToBox online file hosting service. .DESCRIPTION main function used to download a file from UpToBox online file hosting service. To use this function you must have an account and a valid API key .PARAMETER filecode -filecode string use an uptobox filecode to target a file to download .PARAMETER url -url string use an uptobox file url to download a file .PARAMETER outputfolder -outputfolder string path to a target folder where the file will be downloaded. if no target folder is set, a default value is used ($home\downloads) .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .OUTPUTS TypeName: PSuptobox .EXAMPLE Get-UptoboxFile -url https://uptobox.com/xxxxx11yyyy2 start a single thread / synchronous download for filecode xxxxx11yyyy2 and download it in default folder .EXAMPLE Get-UptoboxFile -filecode xxxxx11yyyy2 -outputfolder c:\MyFolder start a single thread / synchronous download for filecode xxxxx11yyyy2 and download it in c:\MyFolder #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [URI]$url, [parameter(Mandatory=$false)] [ValidateLength(12,12)] [string]$filecode, [parameter(Mandatory=$false)] [ValidateScript({(test-path $_)})] [string]$outputfolder, [parameter(Mandatory=$false)] [ValidateLength(37,37)] [string]$APIKey ) process { if ($APIKey) {Set-uptoboxAPIKey -APIKey $APIKey | out-null} if (!($outputfolder)) { if (!$home) { $global:home = $env:userprofile } $outputfolder = join-path $home "Downloads" if (!(test-path $outputfolder)) { new-item -Path $outputfolder -ItemType Directory -Force | Out-Null } } if ($url) { $filecode = $url.AbsolutePath.Substring(1,$url.AbsolutePath.Length-1) } $fileinfo = Invoke-APIuptoboxLinkInfo -filecodes $filecode if ($fileinfo.list.'file_name') { $filename = $fileinfo.list.'file_name' write-verbose "Filename is : $($filename)" $fullfilename = join-path $outputfolder $filename write-verbose "Output file generated : $($fullfilename)" $downloadlink = Invoke-APIuptoboxLink -filecode $filecode write-verbose "Download link generated : $($downloadlink.dlLink)" try { $tmpfile = join-path $outputfolder "$((new-guid).guid).tmp" Invoke-WebRequest -uri $downloadlink.dlLink -OutFile $tmpfile -UseBasicParsing | Out-Null } catch { write-verbose -message "Error when downloading filecode $($filecode) - file $($filename)" write-verbose -message "Error Type: $($_.Exception.GetType().FullName)" write-verbose -message "Error Message: $($_.Exception.Message)" write-verbose -message "HTTP error code:$($_.Exception.Response.StatusCode.Value__)" write-verbose -message "HTTP error message:$($_.Exception.Response.StatusDescription)" write-error "Error when downloading filecode $($filecode) - file $($filename)" } if (test-path $fullfilename) { write-error "Not able to save temporary file $($tmpfile) to $($filename). The file was alredy existing" } else { rename-item -Path $tmpfile -NewName $fileinfo.list.'file_name' } } else { write-error "Filecode $($filecode) not existing or with file status in error" } } } Function Invoke-APIuptoboxLink { <# .SYNOPSIS create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from link API .DESCRIPTION create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from link API .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .OUTPUTS TypeName: PSuptobox #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [ValidateLength(37,37)] [string]$APIKey, [parameter(Mandatory=$true)] [ValidateLength(12,12)] [string]$filecode ) Process { if ($APIKey) {Set-uptoboxAPIKey -APIKey $APIKey | out-null} $params = @{ api = "link" apiparam = "file_code=$($filecode)" } Write-Verbose -message "URL Info : $($params.api)" Invoke-uptoboxAPIV2 @params } } Function Invoke-APIuptoboxLinkInfo { <# .SYNOPSIS create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from link info API .DESCRIPTION create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from link info API .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .OUTPUTS TypeName: PSuptobox #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [ValidateLength(37,37)] [string]$APIKey, [parameter(Mandatory=$true)] [ValidateLength(12,12)] [string[]]$filecodes ) Process { if ($APIKey) {Set-uptoboxAPIKey -APIKey $APIKey | out-null} if ($filecodes.count -gt 1) { $filecodes = $filecodes -join "," } $params = @{ api = "link/info" apiparam = "fileCodes=$($filecodes)" } Write-Verbose -message "URL Info : $($params.api)" Invoke-uptoboxAPIV2 @params } } Function Invoke-APIuptoboxUser { <# .SYNOPSIS create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from user API .DESCRIPTION create several input for Invoke-uptoboxAPIV2 function and then call it to get the user account info from user API .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .OUTPUTS TypeName: PSuptobox .EXAMPLE Invoke-APIuptoboxUser -APIKey "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" get user account info for api key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and set the api key .EXAMPLE Invoke-APIuptoboxUser get your user account info #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [ValidateLength(37,37)] [string]$APIKey ) Process { if ($APIKey) {Set-uptoboxAPIKey -APIKey $APIKey | out-null} $params = @{ api = "user/me" } Write-Verbose -message "URL Info : $($params.api)" Invoke-uptoboxAPIV2 @params } } Function Set-UptoboxAPIKey { <# .SYNOPSIS set and remove uptobox API key as global variable uptoboxAPIKey .DESCRIPTION set and remove uptobox API key as global variable uptoboxAPIKey .PARAMETER APIKEY -APIKey string{APIKEY} Set APIKEY as global variable. .PARAMETER MasterPassword -MasterPassword SecureString{Password} Use a passphrase for encryption purpose. .PARAMETER EncryptKeyInLocalFile -EncryptKeyInLocalFile Store APIKey in encrypted value on local drive .PARAMETER Remove -Remove Remove your current APIKEY from global variable. .OUTPUTS none .EXAMPLE Set-uptoboxAPIKey -apikey "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" Set your API key as global variable so it will be used automatically by all use-uptobox functions .EXAMPLE Set-uptoboxAPIKey -remove Remove your API key set as global variable .EXAMPLE Set-uptoboxAPIKey -apikey "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -MasterPassword (ConvertTo-SecureString -String "YourP@ssw0rd" -AsPlainText -Force) -EncryptKeyInLocalFile Store your API key on hard drive #> [cmdletbinding()] Param ( [parameter(Mandatory=$false)] [ValidateLength(37,37)] [string]$APIKey, [parameter(Mandatory=$false)] [switch]$Remove, [parameter(Mandatory=$false)] [switch]$EncryptKeyInLocalFile, [parameter(Mandatory=$false)] [securestring]$MasterPassword ) process { if ($Remove.IsPresent) { $global:uptoboxAPIKey = $Null } Else { $global:uptoboxAPIKey = $APIKey If ($EncryptKeyInLocalFile.IsPresent) { If (!$MasterPassword -or !$APIKey) { Write-warning "Please provide a valid Master Password to protect the API Key storage on disk and a valid API Key" throw 'no api key or master password' } Else { [Security.SecureString]$SecureKeyString = ConvertTo-SecureString -String $APIKey -AsPlainText -Force $SaltBytes = New-Object byte[] 32 $RNG = New-Object System.Security.Cryptography.RNGCryptoServiceProvider $RNG.GetBytes($SaltBytes) $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList 'user', $MasterPassword $Rfc2898Deriver = New-Object System.Security.Cryptography.Rfc2898DeriveBytes -ArgumentList $Credentials.GetNetworkCredential().Password, $SaltBytes $KeyBytes = $Rfc2898Deriver.GetBytes(32) $EncryptedString = $SecureKeyString | ConvertFrom-SecureString -key $KeyBytes $ObjConfiguptobox = @{ Salt = $SaltBytes EncryptedAPIKey = $EncryptedString } $FolderName = 'Use-Uptobox' $ConfigName = 'Use-Uptobox-Config.xml' if (!$home) { $global:home = $env:userprofile } if (!(Test-Path -Path "$($home)\$FolderName")) { New-Item -ItemType directory -Path "$($home)\$FolderName" | Out-Null } if (test-path "$($home)\$FolderName\$ConfigName") { Remove-item -Path "$($home)\$FolderName\$ConfigName" -Force | out-null } $ObjConfiguptobox | Export-Clixml "$($home)\$FolderName\$ConfigName" } } } } } Function Set-uptoboxProxy { <# .SYNOPSIS Set an internet proxy to use uptobox web api .DESCRIPTION Set an internet proxy to use uptobox web api .PARAMETER DirectNoProxy -DirectNoProxy Remove proxy and configure uptobox powershell functions to use a direct connection .PARAMETER Proxy -Proxy{Proxy} Set the proxy URL .PARAMETER ProxyCredential -ProxyCredential{ProxyCredential} Set the proxy credential to be authenticated with the internet proxy set .PARAMETER ProxyUseDefaultCredentials -ProxyUseDefaultCredentials Use current security context to be authenticated with the internet proxy set .PARAMETER AnonymousProxy -AnonymousProxy No authentication (open proxy) with the internet proxy set .OUTPUTS none .EXAMPLE Set-uptoboxProxy -DirectNoProxy Remove Internet Proxy and set a direct connection .EXAMPLE Set-uptoboxProxy -Proxy "http://myproxy:8080" -ProxyCredential (get-credential) Set Internet Proxy and with manual authentication .EXAMPLE Set-uptoboxProxy -Proxy "http://myproxy:8080" -ProxyUseDefaultCredentials Set Internet Proxy and with automatic authentication based on current security context .EXAMPLE Set-uptoboxProxy -Proxy "http://myproxy:8080" -AnonymousProxy Set Internet Proxy and with no authentication #> [cmdletbinding()] Param ( [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)] [switch]$DirectNoProxy, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)] [string]$Proxy, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)] [Management.Automation.PSCredential]$ProxyCredential, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)] [Switch]$ProxyUseDefaultCredentials, [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$false)] [Switch]$AnonymousProxy ) if ($DirectNoProxy.IsPresent){ $global:uptoboxProxyParams = $null } ElseIf ($Proxy) { $global:uptoboxProxyParams = @{} $uptoboxProxyParams.Add('Proxy', $Proxy) if ($ProxyCredential){ $uptoboxProxyParams.Add('ProxyCredential', $ProxyCredential) If ($uptoboxProxyParams.ProxyUseDefaultCredentials) {$uptoboxProxyParams.Remove('ProxyUseDefaultCredentials')} } Elseif ($ProxyUseDefaultCredentials.IsPresent){ $uptoboxProxyParams.Add('ProxyUseDefaultCredentials', $ProxyUseDefaultCredentials) If ($uptoboxProxyParams.ProxyCredential) {$uptoboxProxyParams.Remove('ProxyCredential')} } ElseIf ($AnonymousProxy.IsPresent) { If ($uptoboxProxyParams.ProxyUseDefaultCredentials) {$uptoboxProxyParams.Remove('ProxyUseDefaultCredentials')} If ($uptoboxProxyParams.ProxyCredential) {$uptoboxProxyParams.Remove('ProxyCredential')} } } } Function Import-uptoboxEncryptedIKey { <# .SYNOPSIS import uptobox API key as global variable from encrypted local config file .DESCRIPTION import uptobox API key as global variable from encrypted local config file .PARAMETER MasterPassword -MasterPassword SecureString{Password} Use a passphrase for encryption purpose. .OUTPUTS none .EXAMPLE Import-uptoboxEncryptedIKey -MasterPassword (ConvertTo-SecureString -String "YourP@ssw0rd" -AsPlainText -Force) set API Key as global variable using encrypted key hosted in local xml file previously generated with Set-uptoboxAPIKey #> [CmdletBinding()] Param( [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [securestring]$MasterPassword ) process { $FolderName = 'Use-uptobox' $ConfigName = 'Use-uptobox-Config.xml' if (!$home) { $global:home = $env:userprofile } if (!(Test-Path "$($home)\$($FolderName)\$($ConfigName)")){ throw 'Configuration file has not been set, Set-uptoboxAPIKey to configure the API Keys.' } $ObjConfiguptobox = Import-Clixml "$($home)\$($FolderName)\$($ConfigName)" $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList 'user', $MasterPassword try { $Rfc2898Deriver = New-Object System.Security.Cryptography.Rfc2898DeriveBytes -ArgumentList $Credentials.GetNetworkCredential().Password, $ObjConfiguptobox.Salt $KeyBytes = $Rfc2898Deriver.GetBytes(32) $SecString = ConvertTo-SecureString -Key $KeyBytes $ObjConfiguptobox.EncryptedAPIKey $SecureStringToBSTR = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecString) $APIKey = [Runtime.InteropServices.Marshal]::PtrToStringAuto($SecureStringToBSTR) $global:uptoboxAPIKey = $APIKey } catch { throw "Not able to set correctly your API Key, your passphrase my be incorrect" write-error -message "Error Type: $($_.Exception.GetType().FullName)" write-error -message "Error Message: $($_.Exception.Message)" } } } Function Invoke-uptoboxAPIV2 { [cmdletbinding()] Param ( [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$api, [parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$apiparam, [parameter(Mandatory=$false)] [Validateset("GET","PATCH")] [string]$Method = "GET", [parameter(Mandatory=$false)] [switch]$Stream ) Process { $script:uptoboxurl = "https://uptobox.com/api/" write-verbose -message "using production uptobox service - https://uptobox.com/" if ((!$global:uptoboxAPIKey)) { write-verbose -message "please set an api key using 'Set-UptoboxAPIKey'" throw "please set an api key using 'Set-UptoboxAPIKey'" } try { $fulluptoboxurl = "$($uptoboxurl)$($api)?token=$($global:uptoboxAPIKey)" if ($apiparam) { $fulluptoboxurl = "$($fulluptoboxurl)&$($apiparam)" } if ($global:uptoboxProxyParams) { $params = $global:uptoboxProxyParams.clone() If (!$params.UseBasicParsing){ $params.add('UseBasicParsing', $true) } If (!$params.URI) { $params.add('URI', "$($fulluptoboxurl)") } Else { $params['URI'] = "$($fulluptoboxurl)" } } Else { $params = @{} $params.add('UseBasicParsing', $true) $params.add('URI', "$($fulluptoboxurl)") } if (($Method -eq "PATCH") -and !$params.Method) { $params.add('Method','PATCH') } $uptoboxresult = invoke-webrequest @params } catch { write-verbose -message "Not able to use uptobox online service - KO" write-verbose -message "Error Type: $($_.Exception.GetType().FullName)" write-verbose -message "Error Message: $($_.Exception.Message)" write-verbose -message "HTTP error code:$($_.Exception.Response.StatusCode.Value__)" write-verbose -message "HTTP error message:$($_.Exception.Response.StatusDescription)" throw "error with uptobox online service" } write-verbose -message "Response Headers : $($uptoboxresult.Headers | out-string)" write-verbose -message "Web Content : $($uptoboxresult.Content)" $temp = $uptoboxresult.Content if ($temp) { if ($stream) { $temp } else { $tempobj = $temp | Convertfrom-Json $tempobj.PSObject.TypeNames.Insert(0,"PSuptobox") if ($tempobj.data) { $tempobj.data } else { $tempobj } } } } } New-Alias -name Get-UptoboxUserInfo -Value Invoke-APIuptoboxUser Export-ModuleMember -Function Invoke-uptoboxAPIV2, Import-uptoboxEncryptedIKey, Set-uptoboxProxy, Set-UptoboxAPIKey, Invoke-APIuptoboxUser, Invoke-APIuptoboxLink, Invoke-APIuptoboxLinkInfo, Get-UptoboxFile, Get-UptoboxFileAsync Export-ModuleMember -Alias Get-UptoboxUserInfo |