matrix-screenshot switch to webp

master
lub 3 years ago
parent 02b9a473bc
commit dccd2ad1a4

@ -30,32 +30,42 @@ Set-StrictMode -Version 3.0
$tmp = New-TemporaryFile $tmp = New-TemporaryFile
$tmpPath = $tmp.FullName
$webpPath = $tmpPath -replace 'tmp$','webp'
if ($Fullscreen) { if ($Fullscreen) {
grim $tmp.FullName grim $tmpPath
} else { } 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) { magick $tmpPath $webpPath
Write-Error ('Screenshot file "{0}" is empty' -f $tmp.FullName) -ErrorAction Continue if ((Get-Item $webpPath).Length -eq 0) {
$tmp | Remove-Item Write-Error ('WebP file "{0}" is empty' -f $webpPath) -ErrorAction Continue
Remove-Item $webpPath
exit exit
} else {
Remove-Item $tmpPath
} }
$mediaSplat = @{ $mediaSplat = @{
Authentication = 'Bearer' Authentication = 'Bearer'
Token = $accessToken | ConvertTo-SecureString -AsPlainText Token = $accessToken | ConvertTo-SecureString -AsPlainText
Headers = @{ Headers = @{
'Content-Type' = 'image/jpeg' 'Content-Type' = 'image/webp'
} }
Method = 'Post' Method = 'Post'
Uri = ($homeserverUrl+'/_matrix/media/r0/upload') Uri = ($homeserverUrl+'/_matrix/media/r0/upload')
InFile = $tmp.FullName InFile = $webpPath
} }
$media = Invoke-RestMethod @mediaSplat $media = Invoke-RestMethod @mediaSplat
$tmp | Remove-Item Remove-Item $webpPath
$serverName = $media.content_uri.Split('/')[2] $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 $mediaUrl | wl-copy --trim-newline
if ($roomId) { if ($roomId) {
$fileName = $webpPath | Split-Path -Leaf
$eventSplat = @{ $eventSplat = @{
Authentication = 'Bearer' Authentication = 'Bearer'
Token = $accessToken | ConvertTo-SecureString -AsPlainText Token = $accessToken | ConvertTo-SecureString -AsPlainText
Method = 'Put' 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 = @{
body = $tmp.Name body = $fileName
msgtype = 'm.image' msgtype = 'm.image'
url = $media.content_uri url = $media.content_uri
} | ConvertTo-Json } | 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)
} }

Loading…
Cancel
Save