filedrop-push.ps1
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
# Filedrop Push ================================================================

param(
    [Parameter(Mandatory)][string]$Path,
    [string]$Token = $env:FILEDROP_TOKEN
)

# Config -----------------------------------------------------------------------

$DropsServer = "https://drops.patppuccin.com"

$EXCLUDES = @(
    ".terraform"
    ".terraform.lock.hcl"
    ".git"
    "*.tfstate"
    "*.tfstate.backup"
    "*.binary"
)

# Helpers ----------------------------------------------------------------------

function Write-Log {
    param(
        [Parameter(Mandatory)][string]$Message,
        [ValidateSet("INF", "WRN", "ERR")][string]$Level = "INF"
    )
    $colors = @{ INF = "Blue"; WRN = "Yellow"; ERR = "Red" }
    Write-Host $Level -ForegroundColor $colors[$Level] -NoNewline
    Write-Host " $Message"
}

function Test-Exclusion($item) {
    foreach ($pattern in $EXCLUDES) {
        if ($item.Name -like $pattern) { return $true }
        if ($item.FullName -like "*\$pattern\*") { return $true }
    }
    return $false
}

function Send-File($filePath) {
    Write-Log "Sending $(Split-Path $filePath -Leaf)"
    $form = @{ file = Get-Item $filePath }
    Invoke-WebRequest `
        -Uri "$DropsServer/upload" `
        -Method POST `
        -Headers @{ Authorization = "Bearer $Token" } `
        -Form $form | Out-Null
    Write-Log "$(Split-Path $filePath -Leaf) sent successfully"
}

# Execution entry point --------------------------------------------------------

# Validate ---------------------------------------------------------------------

if (-not $Token) {
    Write-Log "Token not provided (--Token or FILEDROP_TOKEN env var is required)" -Level ERR
    exit 1
}

$resolved = Resolve-Path $Path
$item = Get-Item $resolved

# Push -------------------------------------------------------------------------

if ($item.PSIsContainer) {
    Write-Log "Archiving folder: $($item.Name)"

    $tmpBase = Join-Path $env:TEMP "__filedrop_staging"
    $tmpDir = Join-Path $tmpBase $item.Name
    $tmpZip = Join-Path $env:TEMP "$($item.Name).zip"

    $contents = Get-ChildItem -Path $item.FullName -Recurse | Where-Object { -not (Test-Exclusion $_) }
    foreach ($entry in $contents) {
        $relative = $entry.FullName.Substring($item.FullName.TrimEnd('\').Length + 1)
        $dest = Join-Path $tmpDir $relative
        if ($entry.PSIsContainer) {
            New-Item -ItemType Directory -Force -Path $dest | Out-Null
        }
        else {
            New-Item -ItemType Directory -Force -Path (Split-Path $dest) | Out-Null
            Copy-Item -Path $entry.FullName -Destination $dest
        }
    }

    Push-Location $tmpBase
    Compress-Archive -Path $item.Name -DestinationPath $tmpZip
    Pop-Location

    Send-File $tmpZip

    Remove-Item $tmpZip -Force
    Remove-Item $tmpBase -Recurse -Force
    Write-Log "Temp files removed"
}
else {
    if (Test-Exclusion $item) {
        Write-Log "$($item.Name) is excluded" -Level ERR
        exit 1
    }
    Send-File $item.FullName
}