From b725f52366e300ef2baa31a503b4e3255e78596b Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Fri, 21 Feb 2025 11:14:46 -0800 Subject: [PATCH] fix: Correct temp library path construction and remove duplicate upload --- SharePointFunctions.ps1 | 42 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 7b5e5b7..710ee30 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -515,13 +515,25 @@ function Convert-Files { # Upload the converted file to SharePoint try { # Verify file exists before trying to read it - if (-not (Test-Path $xlsxPath)) { + if (-not (Test-Path -LiteralPath $xlsxPath)) { throw "Converted file not found at: $xlsxPath" } - # Get the destination folder - $destinationFolder = $script:txtTempLib.Text - $uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content" + # Get the destination folder from the TempLibrary field + $tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") { + "Documents" + } else { + $script:txtTempLib.Text + } + + # Get the drive for the temp library + $tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary } + if (-not $tempDrive) { + throw "Temp library not found: $tempLibrary" + } + + # Construct the upload URI for the temp library + $uploadUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/$($xlsxFileName):/content" # Read the file content $fileContent = [System.IO.File]::ReadAllBytes($xlsxPath) @@ -529,11 +541,11 @@ function Convert-Files { # Upload the file Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream" - $script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n" + $script:txtStatus.Text += "Successfully uploaded to temp library: $tempLibrary/$xlsxFileName`n" } catch { $script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n" - if (-not (Test-Path $xlsxPath)) { + if (-not (Test-Path -LiteralPath $xlsxPath)) { $script:txtStatus.Text += "File not found at: $xlsxPath`n" } continue # Move to the next file @@ -544,24 +556,6 @@ function Convert-Files { continue # Move to the next file } - # Upload the converted file to SharePoint - try { - # Get the destination folder - $destinationFolder = $script:txtTempLib.Text - $uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content" - - # Read the file content - $fileContent = [System.IO.File]::ReadAllBytes($xlsxPath) - - # Upload the file - Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream" - - $script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n" - } - catch { - $script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n" - continue # Move to the next file - } # Clean up temporary files try {