diff --git a/.gitignore b/.gitignore index ba27a75..b935b50 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# Ignore Secrets in .env WICHTIG + +*.env +*.json + # Ignore generated images *.jpg @@ -18,3 +23,5 @@ Thumbs.db .DS_Store + + diff --git a/Get-GDriveToken.ps1 b/Get-GDriveToken.ps1 new file mode 100644 index 0000000..29cada1 --- /dev/null +++ b/Get-GDriveToken.ps1 @@ -0,0 +1,30 @@ +$client = Get-Content ".\client_secret.json" | ConvertFrom-Json + +$clientId = $client.installed.client_id +$clientSecret = $client.installed.client_secret +$redirectUri = "http://localhost" + +$scope = "https://www.googleapis.com/auth/drive.file" + +$authUrl = "https://accounts.google.com/o/oauth2/v2/auth?client_id=$clientId&redirect_uri=$redirectUri&response_type=code&scope=$scope&access_type=offline" + +Write-Host "Öffne folgende URL im Browser:" +Write-Host $authUrl + +Start-Process $authUrl + +$code = Read-Host "Code aus URL eingeben" + +$body = @{ + code = $code + client_id = $clientId + client_secret = $clientSecret + redirect_uri = $redirectUri + grant_type = "authorization_code" +} + +$response = Invoke-RestMethod -Method Post -Uri "https://oauth2.googleapis.com/token" -Body $body + +$response | ConvertTo-Json | Out-File "token.json" + +Write-Host "Token gespeichert!" \ No newline at end of file diff --git a/Upload-A4ToDrive.ps1 b/Upload-A4ToDrive.ps1 new file mode 100644 index 0000000..fdd62c1 --- /dev/null +++ b/Upload-A4ToDrive.ps1 @@ -0,0 +1,54 @@ +# Token laden +$token = Get-Content ".\token.json" | ConvertFrom-Json +$accessToken = $token.access_token + +$folder = "C:\Temp\git\PS-SetEtikettSorte" + +# 👉 HIER deine Google Drive Folder ID eintragen +$DriveFolderId = "1kEmjnVL414XYLRfN597K7YKuiFs-FRBv" + +$files = Get-ChildItem $folder -Filter "*A4*.jpg" + +foreach ($file in $files) { + + Write-Host "Upload: $($file.Name)" + + $boundary = [System.Guid]::NewGuid().ToString() + $LF = "`r`n" + + # 🔥 HIER angepasst + $metadata = @{ + name = $file.Name + parents = @($DriveFolderId) + } | ConvertTo-Json + + $fileBytes = [System.IO.File]::ReadAllBytes($file.FullName) + + $body = ( + "--$boundary$LF" + + "Content-Type: application/json; charset=UTF-8$LF$LF" + + "$metadata$LF" + + "--$boundary$LF" + + "Content-Type: image/jpeg$LF$LF" + ) + + $bodyBytes = [System.Text.Encoding]::UTF8.GetBytes($body) + + $footer = "$LF--$boundary--$LF" + $footerBytes = [System.Text.Encoding]::UTF8.GetBytes($footer) + + $finalBody = New-Object System.IO.MemoryStream + $finalBody.Write($bodyBytes, 0, $bodyBytes.Length) + $finalBody.Write($fileBytes, 0, $fileBytes.Length) + $finalBody.Write($footerBytes, 0, $footerBytes.Length) + $finalBody.Position = 0 + + Invoke-RestMethod ` + -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" ` + -Method Post ` + -Headers @{ Authorization = "Bearer $accessToken" } ` + -ContentType "multipart/related; boundary=$boundary" ` + -Body $finalBody + + Write-Host "Fertig: $($file.Name)" +} \ No newline at end of file diff --git a/UploadToGdrive.ps1 b/UploadToGdrive.ps1 new file mode 100644 index 0000000..e69de29