You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
posters-shared-functions/Gitea-AttachAssetToRelease.ps1

47 lines
1.7 KiB
PowerShell

$apiurl=$args[0]
$apitoken=$args[1]
$filename=$args[2]
$filepath=$args[3]
Write-Host "apiurl: $apiurl"
Write-Host "apitoken: $apitoken"
Write-Host "filename: $filename"
Write-Host "filepath: $filepath"
# Load the required .NET assemblies
Add-Type -AssemblyName System.Net.Http
# Create Object and Load the required .NET assemblies
$httpClient = New-Object System.Net.Http.HttpClient
# $url = 'https://git.zinchuk.xyz/api/v1/repos/yuriy/TestRepo/releases/22/assets'
# $fileName = 'TestFile58.exe'
# $token = '2cf4a54d941'
$headers = @{
'accept' = 'application/json'
'Content-Type' = 'multipart/form-data'
}
# $filePath = "C:\Users\yuriy\Syncthing\Git\TestREpo\test.exe"
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$fileStream = [System.IO.MemoryStream]::new($fileBytes)
$httpClient = [System.Net.Http.HttpClient]::new()
$httpClient.DefaultRequestHeaders.Add('Authorization', "Bearer $apitoken")
$multipartFormData = New-Object System.Net.Http.MultipartFormDataContent
$fileContent = New-Object System.Net.Http.StreamContent -ArgumentList $fileStream
$fileContent.Headers.ContentDisposition = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue -ArgumentList "form-data"
$fileContent.Headers.ContentDisposition.Name = "attachment"
$fileContent.Headers.ContentDisposition.FileName = $fileName
# $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("text/plain")
$fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/x-msdownload")
$multipartFormData.Add($fileContent)
$response = $httpClient.PostAsync($apiurl, $multipartFormData).Result
$responseContent = $response.Content.ReadAsStringAsync().Result
Write-Output $responseContent