From dccd2ad1a4d5d8884310eff5ffeab3de564cfb91 Mon Sep 17 00:00:00 2001 From: lub Date: Tue, 11 May 2021 21:15:45 +0200 Subject: [PATCH] matrix-screenshot switch to webp --- dotfiles/bin/.bin/matrix-screenshot.ps1 | 36 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/dotfiles/bin/.bin/matrix-screenshot.ps1 b/dotfiles/bin/.bin/matrix-screenshot.ps1 index 14aa38d..66c08ab 100755 --- a/dotfiles/bin/.bin/matrix-screenshot.ps1 +++ b/dotfiles/bin/.bin/matrix-screenshot.ps1 @@ -30,32 +30,42 @@ Set-StrictMode -Version 3.0 $tmp = New-TemporaryFile +$tmpPath = $tmp.FullName +$webpPath = $tmpPath -replace 'tmp$','webp' if ($Fullscreen) { - grim $tmp.FullName + grim $tmpPath } else { - grim -g $(slurp) $tmp.FullName + grim -g $(slurp) $tmpPath +} +if ((Get-Item $tmpPath).Length -eq 0) { + Write-Error ('Temp file "{0}" is empty' -f $tmpPath) -ErrorAction Continue + Remove-Item $tmpPath + exit } -if (($tmp | Get-Item).Length -eq 0) { - Write-Error ('Screenshot file "{0}" is empty' -f $tmp.FullName) -ErrorAction Continue - $tmp | Remove-Item +magick $tmpPath $webpPath +if ((Get-Item $webpPath).Length -eq 0) { + Write-Error ('WebP file "{0}" is empty' -f $webpPath) -ErrorAction Continue + Remove-Item $webpPath exit +} else { + Remove-Item $tmpPath } $mediaSplat = @{ Authentication = 'Bearer' Token = $accessToken | ConvertTo-SecureString -AsPlainText Headers = @{ - 'Content-Type' = 'image/jpeg' + 'Content-Type' = 'image/webp' } Method = 'Post' Uri = ($homeserverUrl+'/_matrix/media/r0/upload') - InFile = $tmp.FullName + InFile = $webpPath } $media = Invoke-RestMethod @mediaSplat -$tmp | Remove-Item +Remove-Item $webpPath $serverName = $media.content_uri.Split('/')[2] @@ -65,18 +75,20 @@ $mediaUrl = '{0}/_matrix/media/r0/download/{1}/{2}' -f $homeserverUrl,$serverNam $mediaUrl | wl-copy --trim-newline if ($roomId) { + $fileName = $webpPath | Split-Path -Leaf + $eventSplat = @{ Authentication = 'Bearer' Token = $accessToken | ConvertTo-SecureString -AsPlainText Method = 'Put' - Uri = '{0}/_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $homeserverUrl,$roomId,'m.room.message',($tmp.Name) + Uri = '{0}/_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $homeserverUrl,$roomId,'m.room.message',$filename Body = @{ - body = $tmp.Name + body = $fileName msgtype = 'm.image' url = $media.content_uri } | ConvertTo-Json } - $event = Invoke-RestMethod @eventSplat + $event_id = (Invoke-RestMethod @eventSplat).event_id - Write-Output ('{0}' -f $event.event_id) + Write-Output ('{0}' -f $event_id) }