Issue #1096 - Add Save-PodeRequestFile functionality - disable overwrite and return filepaths#1097
Open
acommonusername wants to merge 5 commits intoBadgerati:developfrom
Open
Issue #1096 - Add Save-PodeRequestFile functionality - disable overwrite and return filepaths#1097acommonusername wants to merge 5 commits intoBadgerati:developfrom
acommonusername wants to merge 5 commits intoBadgerati:developfrom
Conversation
Owner
|
Hey @acommonusername, Thanks for the PR! Looking at this there's quite a few use cases this would have to cover when
I have built something similar once before, and it would be worth moving the "file ID suffix builder" into it's own function in Using the logic I've built before, and combing your logic produces the following (which should cover all use-cases above): param(
$FilePath = 'C:\temp\tests.txt'
)
# check the initial file path, and use that if it doesn't exist
if (!(Test-Path $filepath)) {
return $filepath
}
# vars for some regex
$fileRegex = '(.+)(\.(?=[^\\\/\.]+$))'
$idRegex = '^.+? \((?<id>\d+)\).*$'
# split the file path into base path and name
$filepath, $filename = $filepath -split '[\\/](?=[^\\\/]+$)'
# build a wildcard file name pattern for filtering existing files
# the regex check is for ".name" use cases where there is no extension
if ($filename -imatch $fileRegex) {
$wildFilename = $filename -replace $fileRegex, '$1 (*)$2'
} else {
$wildFilename = "$($filename) (*)"
}
# calculate the next "id" based on existing files
$nextId = 1
$files = @(Get-ChildItem -Name -File -Force -Path $filepath -Filter $wildFilename |
Sort-Object -Property { $_.Length },{ $_ })
$lastId = $files[-1] -replace $idRegex, '$1'
if (($file.Length -eq $lastId)) {
$nextId = $lastId + 1
} else {
foreach ($file in $files) {
if (($file -replace $idRegex, '$1') -ne $nextId) {
break
}
$nextId++
}
}
# generate the new file name
if ($filename -imatch $fileRegex) {
$filename = $filename -replace $fileRegex, "`$1 ($($nextId))`$2"
} else {
$filename = "$($filename) ($($nextId))"
}
# combine the new file name with the original base path, and return
$filepath = [System.IO.Path]::Combine($filepath, $filename)
return $filepathFor the # save the files
$filePathsList = @(foreach ($file in $files) {
# logic
if ($Return) {
$filePath
}
})
if ($Return) {
return $filePathsList
}Saves building list objects and appending to them 😉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the Change
Added functionality to make it possible to disable overwriting files, and instead add a numerical suffix to the filename if it already exists in the directory. Enabled via Switch parameter.
Added functionality to make it possible to return a list of filepaths of the uploaded files. Enabled via Switch parameter.
Help comment updated to include parameters and an example.
Related Issue
Resolves #1096
Examples
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.
Usage
$filePaths = Save-PodeRequestFile -Key 'avatar' -Path 'F:/Images' -NoOverwrite -Return
Result
In the above example, if F:/Images already contains test.jpg and you're uploading the files test.jpg and asdf.jpg. test.jpg will be saved as test (1).jpg and asdf.jpg as asdf.jpg, the function will then return a list containing the following filepaths:
F:/Images\test (1).jpg
F:/Images\asdf.jpg