mirror of
https://github.com/nikdoof/dotfiles.git
synced 2025-12-13 09:42:27 +00:00
Add Send-0x0
This commit is contained in:
@@ -24,6 +24,76 @@ function Update-Dotfiles {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
function Send-0x0 {
|
||||
[CmdletBinding()]
|
||||
PARAM
|
||||
(
|
||||
[string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$File,
|
||||
[Uri]$Service = 'http://0x0.st/'
|
||||
)
|
||||
BEGIN {
|
||||
if (-not (Test-Path $File)) {
|
||||
$errorMessage = ("File {0} missing or unable to read." -f $File)
|
||||
$exception = New-Object System.Exception $errorMessage
|
||||
$errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, 'MultipartFormDataUpload', ([System.Management.Automation.ErrorCategory]::InvalidArgument), $File
|
||||
$PSCmdlet.ThrowTerminatingError($errorRecord)
|
||||
}
|
||||
}
|
||||
PROCESS {
|
||||
Add-Type -AssemblyName System.Net.Http
|
||||
Add-Type -AssemblyName System.Web
|
||||
|
||||
$httpClientHandler = New-Object System.Net.Http.HttpClientHandler
|
||||
$httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler
|
||||
$packageFileStream = New-Object System.IO.FileStream @($File, [System.IO.FileMode]::Open)
|
||||
|
||||
$contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
|
||||
$contentDispositionHeaderValue.Name = "file"
|
||||
$contentDispositionHeaderValue.FileName = (Split-Path $File -leaf)
|
||||
|
||||
$streamContent = New-Object System.Net.Http.StreamContent $packageFileStream
|
||||
$streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
|
||||
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($File)
|
||||
if ($mimeType) {
|
||||
$ContentType = $mimeType
|
||||
}
|
||||
else {
|
||||
$ContentType = "application/octet-stream"
|
||||
}
|
||||
$streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue $ContentType
|
||||
|
||||
$content = New-Object System.Net.Http.MultipartFormDataContent
|
||||
$content.Add($streamContent)
|
||||
|
||||
try {
|
||||
$response = $httpClient.PostAsync($Service, $content).Result
|
||||
if (!$response.IsSuccessStatusCode) {
|
||||
$responseBody = $response.Content.ReadAsStringAsync().Result
|
||||
$errorMessage = "Status code {0}. Reason {1}. Server reported the following message: {2}." -f $response.StatusCode, $response.ReasonPhrase, $responseBody
|
||||
|
||||
throw [System.Net.Http.HttpRequestException] $errorMessage
|
||||
}
|
||||
$responseBody = $response.Content.ReadAsStringAsync().Result
|
||||
return $responseBody
|
||||
}
|
||||
catch [Exception] {
|
||||
$PSCmdlet.ThrowTerminatingError($_)
|
||||
}
|
||||
finally {
|
||||
if ($null -ne $httpClient) {
|
||||
$httpClient.Dispose()
|
||||
}
|
||||
|
||||
if ($null -ne $response) {
|
||||
$response.Dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
END { }
|
||||
}
|
||||
|
||||
|
||||
|
||||
$DotFilesPath = Join-Path $HOME '.dotfiles'
|
||||
$DotFilesAutodetect = $true
|
||||
$DotFilesAllowNestedSymlinks = $true
|
||||
|
||||
Reference in New Issue
Block a user