Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SaveAttachmentsTo will fail if the name contains invalid path characters (Windows) #169

Closed
craigminihan opened this issue Mar 29, 2019 · 3 comments
Assignees
Labels

Comments

@craigminihan
Copy link

Describe the bug
Passing SaveAttachmentsTo to Get-GSGmailMessage fails when the attachment file name contains characters treated as invalid by Windows.

An exception is thrown in the call to [System.IO.File]::Create($fileName) .

To Reproduce
Steps to reproduce the behavior:

  1. Send an email with an attachment named as: ENDEP1_2019-03-29 08:30:00.docx
  2. Call Get-GSGmailMessage -SaveAttachmentsTo

Expected behavior
The file should appear in the folder with invalid characters substituted out.

Environment (please complete the following information):

  • OS: Windows 10 1803
  • PowerShell Version: 5.1.17134.590
  • PSGSuite Version: 2.25.3
@scrthq
Copy link
Member

scrthq commented Mar 29, 2019

@craigminihan - Thanks for opening this up! My initial thought to resolve this is replace any unsafe characters with underscores; would this be an alright approach? If not, what would the preferred behavior be? I should be able to get this resolved pretty quickly tonight and will proceed with underscores as the replacement character unless otherwise noted 🙂

@scrthq scrthq self-assigned this Mar 29, 2019
@scrthq scrthq added the bug label Mar 29, 2019
@ghost ghost added the work in progress label Mar 30, 2019
@scrthq scrthq closed this as completed in f561cfa Mar 30, 2019
scrthq added a commit that referenced this issue Mar 30, 2019
…support

## 2.26.0

* [Issue #169](#169)
  * Fixed: `Get-GSGmailMessage` fails to download attachments containing invalid characters (e.g. `:`)
* [Issue #168](#168)
  * Added: `Add-GSUserLocation`
  * Updated: `New-GSUser` and `Update-GSUser` to add in Location support
* Miscellaneous
  * Improved pipeline support for the `User` parameter across all pertinent functions, i.e. Drive, Calendar, Gmail, Sheets & Tasks APIs.
@ghost ghost removed the work in progress label Mar 30, 2019
@scrthq
Copy link
Member

scrthq commented Mar 30, 2019

@craigminihan - Fix is being rolled out with v2.26.0, should be on the Gallery shortly!

Here's what I've added, for reference:

foreach ($att in $attachments) {
    $cleanedName = $att.FileName -replace "[$(([System.IO.Path]::GetInvalidFileNameChars() + [System.IO.Path]::GetInvalidPathChars()) -join '')]","_" # <<<<< FileName cleanup via RegEx replace
    $fileName = Join-Path $resPath $cleanedName
    Write-Verbose "Saving attachment to path '$fileName'"
    $stream = [System.IO.File]::Create($fileName)
    $att.ContentObject.DecodeTo($stream)
    $stream.Close()
}

@craigminihan
Copy link
Author

nice - thanks for resolving this so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants