Skip to content

Commit

Permalink
Refactor file upload handling to support non-ASCII filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
mkht committed Sep 15, 2024
1 parent f1dcdd0 commit efcdd81
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 153 deletions.
6 changes: 5 additions & 1 deletion Private/New-MultipartFormContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ function GetMultipartBytesContent {
[string]$fileName
)
$contentType = 'application/octet-stream'
$ContentDispositionHeader = 'Content-Disposition: form-data; name="{0}"' -f $fieldName
if (-not [string]::IsNullOrWhiteSpace($fileName)) {
$ContentDispositionHeader += "; filename*=utf-8''{0}" -f [Uri]::EscapeDataString($fileName)
}
$header = @(
('Content-Disposition: form-data; name="{0}"; filename="{1}"' -f $fieldName, $fileName),
$ContentDispositionHeader,
('Content-Type: {0}' -f $contentType),
'',
''
Expand Down
49 changes: 15 additions & 34 deletions Public/Request-AudioTranscription.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ function Request-AudioTranscription {

process {
$FileInfo = Resolve-FileInfo $File
# (Only PS6+)
# If the filename contains non-ASCII characters,
# the OpenAI API cannot recognize the file format correctly and returns an error.
# As a workaround, copy the file to a temporary file and send it.
# We need to find a better way.
$IsTempFileCreated = $false
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($FileInfo.Name -match '[^\u0000-\u007F]') {
Write-Warning 'File name contains non-ASCII characters. It is strongly recommended that file name only contains ASCII characters.'
$FileInfo = Copy-TempFile -SourceFile $FileInfo -ErrorAction Stop
$IsTempFileCreated = $true
}
}

#region Construct parameters for API request
$PostBody = [System.Collections.Specialized.OrderedDictionary]::new()
Expand All @@ -125,28 +112,22 @@ function Request-AudioTranscription {
}

#region Send API Request
try {
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
AuthType = $OpenAIParameter.AuthType
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat
}
finally {
if ($IsTempFileCreated -and (Test-Path $FileInfo -PathType Leaf)) {
Remove-Item $FileInfo -Force -ErrorAction SilentlyContinue
}
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
AuthType = $OpenAIParameter.AuthType
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat

# error check
if ($null -eq $Response) {
return
Expand Down
49 changes: 15 additions & 34 deletions Public/Request-AudioTranslation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,6 @@ function Request-AudioTranslation {

process {
$FileInfo = Resolve-FileInfo $File
# (Only PS6+)
# If the filename contains non-ASCII characters,
# the OpenAI API cannot recognize the file format correctly and returns an error.
# As a workaround, copy the file to a temporary file and send it.
# We need to find a better way.
$IsTempFileCreated = $false
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($FileInfo.Name -match '[^\u0000-\u007F]') {
Write-Warning 'File name contains non-ASCII characters. It is strongly recommended that file name only contains ASCII characters.'
$FileInfo = Copy-TempFile -SourceFile $FileInfo -ErrorAction Stop
$IsTempFileCreated = $true
}
}

#region Construct parameters for API request
$PostBody = [System.Collections.Specialized.OrderedDictionary]::new()
Expand All @@ -95,28 +82,22 @@ function Request-AudioTranslation {
}

#region Send API Request
try {
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
AuthType = $OpenAIParameter.AuthType
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat
}
finally {
if ($IsTempFileCreated -and (Test-Path $FileInfo -PathType Leaf)) {
Remove-Item $FileInfo -Force -ErrorAction SilentlyContinue
}
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
AuthType = $OpenAIParameter.AuthType
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat

# error check
if ($null -eq $Response) {
return
Expand Down
65 changes: 15 additions & 50 deletions Public/Request-ImageEdit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,9 @@ function Request-ImageEdit {

process {
$ImageFileInfo = Resolve-FileInfo $Image
# (Only PS6+)
# If the filename contains non-ASCII characters,
# the OpenAI API cannot recognize the file format correctly and returns an error.
# As a workaround, copy the file to a temporary file and send it.
# We need to find a better way.
$IsTempImageFileCreated = $false
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($ImageFileInfo.Name -match '[^\u0000-\u007F]') {
Write-Warning 'File name contains non-ASCII characters. It is strongly recommended that file name only contains ASCII characters.'
$ImageFileInfo = Copy-TempFile -SourceFile $ImageFileInfo -ErrorAction Stop
$IsTempImageFileCreated = $true
}
}

if ($PSBoundParameters.ContainsKey('Mask')) {
$MaskFileInfo = Resolve-FileInfo $Mask
# (Only PS6+)
# If the filename contains non-ASCII characters,
# the OpenAI API cannot recognize the file format correctly and returns an error.
# As a workaround, copy the file to a temporary file and send it.
# We need to find a better way.
$IsTempMaskFileCreated = $false
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($MaskFileInfo.Name -match '[^\u0000-\u007F]') {
Write-Warning 'File name contains non-ASCII characters. It is strongly recommended that file name only contains ASCII characters.'
$MaskFileInfo = Copy-TempFile -SourceFile $MaskFileInfo -ErrorAction Stop
$IsTempMaskFileCreated = $true
}
}
}

if ($NumberOfImages -gt 1) {
Expand Down Expand Up @@ -162,30 +136,21 @@ function Request-ImageEdit {
#endregion

#region Send API Request
try {
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat
}
finally {
if ($IsTempImageFileCreated -and (Test-Path $ImageFileInfo -PathType Leaf)) {
Remove-Item $ImageFileInfo -Force -ErrorAction SilentlyContinue
}
if ($IsTempMaskFileCreated -and (Test-Path $MaskFileInfo -PathType Leaf)) {
Remove-Item $MaskFileInfo -Force -ErrorAction SilentlyContinue
}
}
$splat = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @splat

# error check
if ($null -eq $Response) {
return
Expand Down
49 changes: 15 additions & 34 deletions Public/Request-ImageVariation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ function Request-ImageVariation {

process {
$FileInfo = Resolve-FileInfo $Image
# (Only PS6+)
# If the filename contains non-ASCII characters,
# the OpenAI API cannot recognize the file format correctly and returns an error.
# As a workaround, copy the file to a temporary file and send it.
# We need to find a better way.
$IsTempFileCreated = $false
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($FileInfo.Name -match '[^\u0000-\u007F]') {
Write-Warning 'File name contains non-ASCII characters. It is strongly recommended that file name only contains ASCII characters.'
$FileInfo = Copy-TempFile -SourceFile $FileInfo -ErrorAction Stop
$IsTempFileCreated = $true
}
}

if ($NumberOfImages -gt 1) {
if ($PSCmdlet.ParameterSetName -eq 'OutFile') {
Expand Down Expand Up @@ -132,27 +119,21 @@ function Request-ImageVariation {
#endregion

#region Send API Request
try {
$params = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @params
}
finally {
if ($IsTempFileCreated -and (Test-Path $FileInfo -PathType Leaf)) {
Remove-Item $FileInfo -Force -ErrorAction SilentlyContinue
}
}
$params = @{
Method = $OpenAIParameter.Method
Uri = $OpenAIParameter.Uri
ContentType = $OpenAIParameter.ContentType
TimeoutSec = $OpenAIParameter.TimeoutSec
MaxRetryCount = $OpenAIParameter.MaxRetryCount
ApiKey = $OpenAIParameter.ApiKey
Organization = $OpenAIParameter.Organization
Body = $PostBody
AdditionalQuery = $AdditionalQuery
AdditionalHeaders = $AdditionalHeaders
AdditionalBody = $AdditionalBody
}
$Response = Invoke-OpenAIAPIRequest @params

# error check
if ($null -eq $Response) {
return
Expand Down
14 changes: 14 additions & 0 deletions Tests/Request-AudioTranscription.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,19 @@ Describe 'Request-AudioTranscription' {
$ret.text.Length | Should -BeGreaterThan 1
$ret.task | Should -Be 'transcribe'
}

It 'Non-ASCII filename' {
Copy-Item ($script:TestData + '/voice_japanese.mp3') 'TestDrive:/日本語音声ファイル.mp3'
{ $params = @{
File = 'TestDrive:/日本語音声ファイル.mp3'
TimeoutSec = 30
ErrorAction = 'Stop'
}

$script:Text = Request-AudioTranscription @params
} | Should -Not -Throw
$Text | Should -BeOfType [string]
$Text.Length | Should -BeGreaterThan 1
}
}
}

0 comments on commit efcdd81

Please sign in to comment.