Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions src/login.ps1

This file was deleted.

286 changes: 144 additions & 142 deletions src/public/Add-GoogleDriveFile.ps1
Original file line number Diff line number Diff line change
@@ -1,135 +1,136 @@
<#
.SYNOPSIS
Uploads a file to Google Drive in a specified folder.
Uploads a file to Google Drive in a specified folder.

.DESCRIPTION
Uploads a file to Google Drive, places it in the "Open Live Writer" subfolder,
preserves the original filename.
Uploads a file to Google Drive, places it in the "Open Live Writer" subfolder,
preserves the original filename.

.PARAMETER FilePath
The local path to the file to upload.
The local path to the file to upload.

.PARAMETER FileName
Optional custom name for the file. If not specified, uses the original filename.
Optional custom name for the file. If not specified, uses the original filename.

.PARAMETER Force
If specified, will overwrite an existing file with the same name in the target folder.
If not specified and the file already exists, it will return the existing file's metadata.
If specified, will overwrite an existing file with the same name in the target folder.
If not specified and the file already exists, it will return the existing file's metadata.

.EXAMPLE
Add-GoogleDriveFile -FilePath "C:\images\photo.jpg"
Add-GoogleDriveFile -FilePath "C:\images\photo.jpg"
#>
function Add-GoogleDriveFile {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]$FilePath,
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]$FilePath,

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

[Parameter()]
[string]$TargetFolderName = "Open Live Writer",
[Parameter()]
[string]$TargetFolderName = "Open Live Writer",

[Parameter()]
[switch]$Force
)
[Parameter()]
[switch]$Force
)

$sourceItem = Get-Item (Resolve-Path $FilePath)
Write-Verbose "Add-GoogleDriveFile: Uploading file: $($sourceItem.FullName) to Google Drive"
$sourceItem = Get-Item (Resolve-Path $FilePath)
Write-Verbose "Add-GoogleDriveFile: Uploading file: $($sourceItem.FullName) to Google Drive"

if (-not $FileName) {
$FileName = $sourceItem.Name
if (-not $FileName) {
$FileName = $sourceItem.Name
}

# First, find or create the upload folder in Google Drive
Write-Verbose "Add-GoogleDriveFile: Verifying target folder: $TargetFolderName"
$folder = Get-GoogleDriveItems -ResultType "Folders" -Title $TargetFolderName

if (-not $folder) {
# Create the folder if it doesn't exist
Write-Verbose "Add-GoogleDriveFile: Folder '$TargetFolderName' not found. Creating new folder."
$folder = Add-GoogleDriveFolder -Name $TargetFolderName
}
else {
# Get the first folder if multiple exist
Write-Verbose "Add-GoogleDriveFile: Folder '$TargetFolderName' found."
$folder = $folder | Select-Object -First 1
}

# Determine if the file already exists in the target folder
Write-Verbose "Add-GoogleDriveFile: Checking if file '$FileName' already exists in folder '$TargetFolderName'"
$existingFile = Get-GoogleDriveItems -ResultType "Files" -Title $FileName -ParentId $folder.id
if ($existingFile) {
if (-not $Force) {
# use existing file
return New-GoogleDriveMetadata -id $existingFile.id -name $existingFile.name
}
# address multiple file issue
# todo: evaluate additional meta-data of the file to ensure it's not deleted
$existingFile = $existingFile | Select-Object -First 1
}

# First, find or create the upload folder in Google Drive
Write-Verbose "Add-GoogleDriveFile: Verifying target folder: $TargetFolderName"
$folder = Get-GoogleDriveItems -ResultType "Folders" -Title $TargetFolderName

if (-not $folder) {
# Create the folder if it doesn't exist
Write-Verbose "Add-GoogleDriveFile: Folder '$TargetFolderName' not found. Creating new folder."
$folder = Add-GoogleDriveFolder -Name $TargetFolderName
} else {
# Get the first folder if multiple exist
Write-Verbose "Add-GoogleDriveFile: Folder '$TargetFolderName' found."
$folder = $folder | Select-Object -First 1
}

# Determine if the file already exists in the target folder
Write-Verbose "Add-GoogleDriveFile: Checking if file '$FileName' already exists in folder '$TargetFolderName'"
$existingFile = Get-GoogleDriveItems -ResultType "Files" -Title $FileName -ParentId $folder.id
$sourceMime = Get-ImageMimeType -Extension $sourceItem.Extension

# Prepare metadata for the file
$metadata = @{
name = $FileName
parents = @($folder.id)
} | ConvertTo-Json -Compress

$fileContent = [System.IO.File]::ReadAllBytes($sourceItem.FullName)
$fileContentBase64 = [Convert]::ToBase64String($fileContent)

# Create multipart body
$boundary = "boundary_" + [System.Guid]::NewGuid().ToString()

$body = @(

# Metadata part
"--$boundary"
"Content-Type: application/json; charset=UTF-8"
""
$metadata
"--$boundary"

# File content part
"Content-Type: $sourceMime"
"Content-Transfer-Encoding: base64"
""
$fileContentBase64
"--$boundary--"
) -join "`r`n"

$additionalHeaders = @{
"Content-Type" = "multipart/related; boundary=$boundary"
}

try {

if ($existingFile) {
if (-not $Force) {
# use existing file
return New-GoogleDriveMetadata -id $existingFile.id -name $existingFile.name
}
# address multiple file issue
# todo: evaluate additional meta-data of the file to ensure it's not deleted
$existingFile = $existingFile | Select-Object -First 1
}
# If the file exists and Force is specified, update it
$uri = "https://www.googleapis.com/upload/drive/v3/files/$($existingFile.id)?uploadType=media"
$method = "PATCH"

$sourceMime = Get-ImageMimeType -Extension $sourceItem.Extension

# Prepare metadata for the file
$metadata = @{
name = $FileName
parents = @($folder.id)
} | ConvertTo-Json -Compress

$fileContent = [System.IO.File]::ReadAllBytes($sourceItem.FullName)
$fileContentBase64 = [Convert]::ToBase64String($fileContent)

# Create multipart body
$boundary = "boundary_" + [System.Guid]::NewGuid().ToString()

$body = @(

# Metadata part
"--$boundary"
"Content-Type: application/json; charset=UTF-8"
""
$metadata
"--$boundary"

# File content part
"Content-Type: $sourceMime"
"Content-Transfer-Encoding: base64"
""
$fileContentBase64
"--$boundary--"
) -join "`r`n"

$additionalHeaders = @{
"Content-Type" = "multipart/related; boundary=$boundary"
}
"Add-GoogleDriveFile: $Method $uri" | Write-Verbose

try {

if ($existingFile) {
# If the file exists and Force is specified, update it
$uri = "https://www.googleapis.com/upload/drive/v3/files/$($existingFile.id)?uploadType=media"
$method = "PATCH"

"Add-GoogleDriveFile: $Method $uri" | Write-Verbose

$uploadResult = Invoke-GApi -uri $uri -InFile $sourceItem.FullName -method $method -ContentType $sourceMime -Verbose:$false
}
else {
$uri = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
$method = "POST"
"Add-GoogleDriveFile: $Method $uri" | Write-Verbose

$uploadResult = Invoke-GApi -uri $uri -body $body -method $method -ContentType "multipart/related; boundary=$boundary" -AdditionalHeaders $additionalHeaders -Verbose:$false
}

# Return the file information with public URL
return New-GoogleDriveMetadata -id $uploadResult.id -name $uploadResult.name
$uploadResult = Invoke-GApi -uri $uri -InFile $sourceItem.FullName -method $method -ContentType $sourceMime -Verbose:$false
}
catch {
Write-Error "Failed to upload file to Google Drive: $($_.Exception.Message). $($_.ErrorDetails | ConvertTo-Json -Depth 10)" -ErrorAction Stop
else {
$uri = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
$method = "POST"
"Add-GoogleDriveFile: $Method $uri" | Write-Verbose

$uploadResult = Invoke-GApi -uri $uri -body $body -method $method -ContentType "multipart/related; boundary=$boundary" -AdditionalHeaders $additionalHeaders -Verbose:$false
}

# Return the file information with public URL
return New-GoogleDriveMetadata -id $uploadResult.id -name $uploadResult.name
}
catch {
Write-Error "Failed to upload file to Google Drive: $($_.Exception.Message). $($_.ErrorDetails | ConvertTo-Json -Depth 10)" -ErrorAction Stop
}
}

<#
Expand All @@ -146,45 +147,46 @@ The file extension (including the dot).
Get-ImageMimeType -Extension ".jpg"
#>
function Get-ImageMimeType {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Extension
)

$mimeTypes = @{
'.jpg' = 'image/jpeg'
'.jpeg' = 'image/jpeg'
'.png' = 'image/png'
'.gif' = 'image/gif'
'.bmp' = 'image/bmp'
'.webp' = 'image/webp'
'.svg' = 'image/svg+xml'
'.ico' = 'image/x-icon'
'.tiff' = 'image/tiff'
'.tif' = 'image/tiff'
}

$normalizedExtension = $Extension.ToLower()
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Extension
)

$mimeTypes = @{
'.jpg' = 'image/jpeg'
'.jpeg' = 'image/jpeg'
'.png' = 'image/png'
'.gif' = 'image/gif'
'.bmp' = 'image/bmp'
'.webp' = 'image/webp'
'.svg' = 'image/svg+xml'
'.ico' = 'image/x-icon'
'.tiff' = 'image/tiff'
'.tif' = 'image/tiff'
}

$normalizedExtension = $Extension.ToLower()

if ($mimeTypes.ContainsKey($normalizedExtension)) {
return $mimeTypes[$normalizedExtension]
} else {
return 'application/octet-stream'
}
if ($mimeTypes.ContainsKey($normalizedExtension)) {
return $mimeTypes[$normalizedExtension]
}
else {
return 'application/octet-stream'
}
}

function New-GoogleDriveMetadata {
param(
[string]$id,
[string]$name
)
$publicUrl = "https://lh3.googleusercontent.com/d/$id"
param(
[string]$id,
[string]$name
)
$publicUrl = "https://lh3.googleusercontent.com/d/$id"

return [PSCustomObject]@{
Id = $id
Name = $name
PublicUrl = $publicUrl
DriveUrl = "https://drive.google.com/file/d/$id/view"
}
return [PSCustomObject]@{
Id = $id
Name = $name
PublicUrl = $publicUrl
DriveUrl = "https://drive.google.com/file/d/$id/view"
}
}
Loading