Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mkht committed Oct 31, 2024
1 parent 644a54b commit 97663b7
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 101 deletions.
25 changes: 25 additions & 0 deletions Private/New-ParentFolder.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function New-ParentFolder {
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0)]
[string]$File
)

try {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($File)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}
$AbsoluteOutFile
}
catch {
Write-Error -Exception $_.Exception
}
}
19 changes: 19 additions & 0 deletions Private/Write-ByteContent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Write-ByteContent {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$OutFile,

[Parameter(Mandatory)]
[byte[]]$Bytes
)

$AbsoluteOutFile = New-ParentFolder -File $OutFile

try {
[System.IO.File]::WriteAllBytes($AbsoluteOutFile, $Bytes)
}
catch {
Write-Error -Exception $_.Exception
}
}
20 changes: 1 addition & 19 deletions Public/Files/Get-OpenAIFileContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,7 @@ function Get-OpenAIFileContent {

#region Output
if ($OutFile) {
try {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}

# Output file
[System.IO.File]::WriteAllBytes($AbsoluteOutFile, ([byte[]]$Response))
}
catch {
Write-Error -Exception $_.Exception
}
Write-ByteContent -OutFile $OutFile -Bytes ([byte[]]$Response)
}
else {
Write-Output $Response
Expand Down
20 changes: 1 addition & 19 deletions Public/Request-AudioSpeech.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,7 @@ function Request-AudioSpeech {
#endregion

#region Output
try {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}

# Output file
[System.IO.File]::WriteAllBytes($AbsoluteOutFile, ([byte[]]$Response))
}
catch {
Write-Error -Exception $_.Exception
}
Write-ByteContent -OutFile $OutFile -Bytes ([byte[]]$Response)
#endregion
}

Expand Down
19 changes: 4 additions & 15 deletions Public/Request-ChatCompletion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -678,31 +678,20 @@ function Request-ChatCompletion {

# Save audio to file
if ($PSBoundParameters.ContainsKey('AudioOutFile')) {
foreach ($choice in $InputObject.choices) {
foreach ($choice in $Response.choices) {
if ($null -eq $choice.message.audio.data) {
continue
}

try {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($AudioOutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}

# Output file
[System.IO.File]::WriteAllBytes($AbsoluteOutFile, ([System.Convert]::FromBase64String($choice.message.audio.data)))
$audioData = [System.Convert]::FromBase64String($choice.message.audio.data)
}
catch {
Write-Error -Exception $_.Exception
}

Write-ByteContent -OutFile $AudioOutFile -Bytes $audioData

break
}
}
Expand Down
17 changes: 1 addition & 16 deletions Public/Request-ImageEdit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,7 @@ function Request-ImageEdit {

#region Output
if ($PSCmdlet.ParameterSetName -eq 'OutFile') {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}
# error check
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
Write-Error -Message ('Destination folder "{0}" does not exist.' -f $ParentDirectory)
return
}
$AbsoluteOutFile = New-ParentFolder -File $OutFile

# Download image
$ResponseContent | Select-Object -ExpandProperty 'url' | Select-Object -First 1 | ForEach-Object {
Expand Down
17 changes: 1 addition & 16 deletions Public/Request-ImageGeneration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,7 @@ function Request-ImageGeneration {

#region Output
if ($PSCmdlet.ParameterSetName -eq 'OutFile') {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}
# error check
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
Write-Error -Message ('Destination folder "{0}" does not exist.' -f $ParentDirectory)
return
}
$AbsoluteOutFile = New-ParentFolder -File $OutFile

# Download image
$ResponseContent | Select-Object -ExpandProperty 'url' | Select-Object -First 1 | ForEach-Object {
Expand Down
17 changes: 1 addition & 16 deletions Public/Request-ImageVariation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,7 @@ function Request-ImageVariation {

#region Output
if ($PSCmdlet.ParameterSetName -eq 'OutFile') {
# Convert to absolute path
$AbsoluteOutFile = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutFile)
# create parent directory if it does not exist
$ParentDirectory = Split-Path $AbsoluteOutFile -Parent
if (-not $ParentDirectory) {
$ParentDirectory = [string](Get-Location -PSProvider FileSystem).ProviderPath
$AbsoluteOutFile = Join-Path $ParentDirectory $AbsoluteOutFile
}
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
$null = New-Item -Path $ParentDirectory -ItemType Directory -Force
}
# error check
if (-not (Test-Path -LiteralPath $ParentDirectory -PathType Container)) {
Write-Error -Message ('Destination folder "{0}" does not exist.' -f $ParentDirectory)
return
}
$AbsoluteOutFile = New-ParentFolder -File $OutFile

# Download image
$ResponseContent | Select-Object -ExpandProperty 'url' | Select-Object -First 1 | ForEach-Object {
Expand Down

0 comments on commit 97663b7

Please sign in to comment.