Saving Converted Files in Temp Library with same folder structure

This commit is contained in:
Bobby Abellana 2025-02-24 14:13:17 -08:00
parent d729b88606
commit e29391639f

View File

@ -341,7 +341,17 @@ function Get-XlsFilesRecursive {
$allFiles += [PSCustomObject]@{
OriginalPath = $item.WebUrl
OriginalFileName = $item.Name
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
TempPath = if ($script:txtFolder.Text) {
# Extract the relative path from the original path
$relativePath = $item.ParentReference.Path -replace ".*root:/", ""
if ($relativePath) {
"$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx")
} else {
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
}
} else {
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
}
}
}
}
@ -386,7 +396,17 @@ function Get-XlsFilesCurrentFolder {
$allFiles += [PSCustomObject]@{
OriginalPath = $item.WebUrl
OriginalFileName = $item.Name
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
TempPath = if ($script:txtFolder.Text) {
# Extract the relative path from the original path
$relativePath = $item.ParentReference.Path -replace ".*root:/", ""
if ($relativePath) {
"$($script:txtTempLib.Text)/$relativePath/$($item.Name)".Replace(".xls", ".xlsx")
} else {
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
}
} else {
"$($script:txtTempLib.Text)/$($item.Name)".Replace(".xls", ".xlsx")
}
}
}
}
@ -648,24 +668,31 @@ function Convert-Files {
throw "Converted file not found at: $xlsxPath"
}
# Get the destination folder from the TempLibrary field
# Get the drive for the temp library
$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"
}
# Use TempPath from the JSON configuration for the upload path
# Use the correct drive ID
$driveId = $tempDrive.Id
# Extract folder path from TempPath, but remove the library name prefix
$relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath
$relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix
$uploadFileName = $xlsxFileName # Use the converted file name
# Construct the upload URI for the temp library, properly encoding the path
$uploadPath = $uploadFileName # Just use the filename to save in root
$uploadPath = if ($relativePath) {
"$relativePath/$uploadFileName" # Include folder structure
} else {
$uploadFileName # Just use the filename to save in root
}
# URL encode the path but preserve forward slashes and encode spaces as %20
$encodedPath = $uploadPath.Split('/') |
@ -676,7 +703,6 @@ function Convert-Files {
# Fix the URI construction to avoid the colon issue
$baseUri = "https://graph.microsoft.com/v1.0/drives"
$driveId = $tempDrive.Id
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
@ -687,7 +713,9 @@ function Convert-Files {
# Upload the file
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadFileName`n"
if ($script:txtStatus) {
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n"
}
}
catch {
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
@ -780,7 +808,9 @@ function Move-Files {
$originalPath = $file.OriginalPath
$originalFileName = $file.OriginalFileName
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
$tempPath = $xlsxFileName # Remove the ConvertedFiles folder prefix
# Use the full TempPath to maintain folder structure
$tempPath = $file.TempPath.Replace(".xls", ".xlsx")
$script:txtStatus.Text += "Moving file: $xlsxFileName`n"
@ -789,8 +819,16 @@ function Move-Files {
$sourceUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/" +
[System.Web.HttpUtility]::UrlEncode($tempPath).Replace("+", "%20")
# For the destination, we need to extract the folder path
$folderPath = $script:txtFolder.Text
$destinationPath = if ($folderPath) {
"$folderPath/$xlsxFileName"
} else {
$xlsxFileName
}
$destinationUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/" +
[System.Web.HttpUtility]::UrlEncode($originalFileName).Replace("+", "%20")
[System.Web.HttpUtility]::UrlEncode($destinationPath).Replace("+", "%20")
$script:txtStatus.Text += "Source URI: $sourceUri`n"
$script:txtStatus.Text += "Destination URI: $destinationUri`n"
@ -798,8 +836,9 @@ function Move-Files {
# Move the file
$moveResponse = Invoke-MgGraphRequest -Uri $sourceUri -Method PATCH -Body @{
parentReference = @{
path = "/drives/$($drive.Id)/root:/$($script:txtFolder.Text)"
path = "/drives/$($drive.Id)/root:/$folderPath"
}
name = $xlsxFileName
} -ContentType "application/json"
$script:txtStatus.Text += "Successfully moved file: $xlsxFileName`n"
@ -916,8 +955,20 @@ function Upload-ConvertedFile {
throw "Temp library not found: $tempLibrary"
}
# Use the correct drive ID
$driveId = $tempDrive.Id
# Extract folder path from TempPath, but remove the library name prefix
$relativePath = $file.TempPath -replace "/[^/]+$", "" # Extract folder path from TempPath
$relativePath = $relativePath -replace "^$($script:txtTempLib.Text)/", "" # Remove library name prefix
$uploadFileName = $xlsxFileName # Use the converted file name
# Construct the upload URI for the temp library, properly encoding the path
$uploadPath = $uploadFileName # Just use the filename to save in root
$uploadPath = if ($relativePath) {
"$relativePath/$uploadFileName" # Include folder structure
} else {
$uploadFileName # Just use the filename to save in root
}
# URL encode the path but preserve forward slashes and encode spaces as %20
$encodedPath = $uploadPath.Split('/') |
@ -928,7 +979,6 @@ function Upload-ConvertedFile {
# Fix the URI construction to avoid the colon issue
$baseUri = "https://graph.microsoft.com/v1.0/drives"
$driveId = $tempDrive.Id
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
if ($script:txtStatus) {
@ -942,7 +992,7 @@ function Upload-ConvertedFile {
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
if ($script:txtStatus) {
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadFolder/$uploadFileName`n"
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadPath`n"
}
}
catch {