fix: Update SharePoint file download/upload to use proper drive ID

This commit is contained in:
Bobby Abellana (aider) 2025-02-21 10:52:05 -08:00
parent 728ce14af4
commit 529c0f3982

View File

@ -420,16 +420,31 @@ function Convert-Files {
# Download the file from SharePoint
$downloadPath = "$env:TEMP\$originalFileName"
try {
# Extract DriveId and ItemId from the originalPath
if ($originalPath -match "drives/([^/]+)/items/([^/]+)") {
$driveId = $matches[1]
$itemId = $matches[2]
} else {
throw "Could not extract DriveId and ItemId from OriginalPath"
# Get the site first
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
# Get the drive
$drives = Get-MgSiteDrive -SiteId $site.id
$libraryName = if ($script:txtProdLib.Text -eq "Shared Documents") {
"Documents"
} else {
$script:txtProdLib.Text
}
$drive = $drives | Where-Object { $_.Name -eq $libraryName }
# Get the file path relative to the library root
$relativePath = $originalFileName
if ($script:txtFolder.Text) {
$folderPath = $script:txtFolder.Text.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
$relativePath = "$folderPath/$originalFileName"
}
# Construct the download URI
$encodedPath = [System.Web.HttpUtility]::UrlEncode($relativePath).Replace("+", "%20")
$downloadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$encodedPath:/content"
# Download the file
$fileContent = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$itemId/content" -Method GET -OutFile $downloadPath
Invoke-MgGraphRequest -Uri $downloadUri -Method GET -OutFile $downloadPath
if (-not (Test-Path $downloadPath)) {
throw "Failed to download file from SharePoint"
}
@ -475,9 +490,9 @@ function Convert-Files {
# Upload the converted file to SharePoint
try {
# Get the Drive ID and destination folder
# Get the destination folder
$destinationFolder = $script:txtTempLib.Text
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$driveId/root:/$($destinationFolder)/$($xlsxFileName):/content"
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
# Read the file content
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)