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

Issue #1096 - Add Save-PodeRequestFile functionality - disable overwrite and return filepaths #1097

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/Public/Responses.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,13 @@ If the Request has multiple files in, and you specify a file path, then all file
.PARAMETER FileName
An optional FileName to save a specific files if multiple files were supplied in the Request. By default, every file is saved.

.PARAMETER NoOverwrite
If supplied, disables overwriting already existing files, and adds a numerical suffix to the filename if necessary.
F.x. if C:\pode\test.txt already exists and test.txt is uploaded again, it will be saved as C:\pode\test (1).txt

.PARAMETER Return
If supplied, filepaths of all saved files will be returned as a list.

.EXAMPLE
Save-PodeRequestFile -Key 'avatar'

Expand All @@ -1215,6 +1222,9 @@ Save-PodeRequestFile -Key 'avatar' -Path 'F:/Images'

.EXAMPLE
Save-PodeRequestFile -Key 'avatar' -Path 'F:/Images' -FileName 'icon.png'

.EXAMPLE
$filePaths = Save-PodeRequestFile -Key 'avatar' -Path 'F:/Images' -NoOverwrite -Return
#>
function Save-PodeRequestFile
{
Expand All @@ -1230,7 +1240,15 @@ function Save-PodeRequestFile

[Parameter()]
[string[]]
$FileName
$FileName,

[Parameter()]
[switch]
$NoOverwrite,

[Parameter()]
[switch]
$Return
)

# if path is '.', replace with server root
Expand Down Expand Up @@ -1258,17 +1276,49 @@ function Save-PodeRequestFile
}
}

# set up filepath list
if ($Return) {
$filePathsList = New-Object System.Collections.Generic.List[string]
}

# save the files
foreach ($file in $files) {
# if the path is a directory, add the filename
$filePath = $Path
if (Test-PodePathIsDirectory -Path $filePath) {
$filePath = [System.IO.Path]::Combine($filePath, $file)
}

# add numeric suffix to file name if overwrites are not permitted
if ($NoOverwrite -and [System.IO.File]::Exists($filePath)) {
# set up necessary variables for looping
$splitPathArray = $filePath -split '\.(?=[^\\\/]+$)'
$i = 0

# loop until suggested filepath doesn't already exist
while ([System.IO.File]::Exists($filePath)) {
$i++
$tempSplitPathArray = $splitPathArray.Clone()
$tempSplitPathArray[0] = -join ($splitPathArray[0], " ($i)")
$filePath = $tempSplitPathArray -join '.'
}
}

# add filepath to filepath list
if ($Return) {
$filePathsList.Add($filePath)
}

# save the file
$WebEvent.Files[$file].Save($filePath)

}

# return filepath list
if ($Return) {
return ,$filePathsList
}

}

<#
Expand Down Expand Up @@ -1600,4 +1650,4 @@ function Add-PodeViewFolder
# add the route(s)
Write-Verbose "Adding View Folder: [$($Name)] $($Source)"
$PodeContext.Server.Views[$Name] = $Source
}
}