You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB
PowerShell

#!/usr/bin/env pwsh
<# TODO
.SYNOPSIS
Takes a screenshot and puts the URL into the clipboard
.DESCRIPTION
Uses grim and slurp to create a screenshot and uploads it into a matrix content repository.
A GETable URL is placed into the clipboard via wl-copy and optionally sent to the specified room.
.NOTES
Example config at $HOME/.config/matrix-screenshot.ps1:
```
$roomId = '!XpxHNLnPsnNEbScyeh:imninja.net'
$accessToken = 'NotaReALaCceSSTOk3n'
$homeserverUrl = 'https://imninja.net'
```
#>
[CmdletBinding()]
param (
# Takes a fullscreen screenshot instead of selecting an area.
[switch]
$Fullscreen
)
# import config
$config = Import-Clixml (Join-Path $env:HOME '.config/nextcloud-screenshot.xml')
$tmp = New-TemporaryFile
$tmpPath = $tmp.FullName
$webpPath = $tmpPath -replace 'tmp$','webp'
if ($Fullscreen) {
grim $tmpPath
} else {
grim -g $(slurp) $tmpPath
}
if ((Get-Item $tmpPath).Length -eq 0) {
Remove-Item $tmpPath
throw ('Temp file "{0}" is empty' -f $tmpPath)
}
magick $tmpPath $webpPath
Remove-Item $tmpPath
if ((Get-Item $webpPath).Length -eq 0) {
Remove-Item $webpPath
throw ('WebP file "{0}" is empty' -f $webpPath)
}
$webpName = (Split-Path -Leaf $webpPath)
$nextcloudPath = $config.Path + '/' + $webpName
$uploadSplat = @{
Authentication = 'Basic'
Credential = $config.Credential
Method = 'Put'
Uri = '{0}/remote.php/dav/files/{1}/{2}' -f $config.BaseUrl,$config.Credential.UserName,$nextcloudPath
InFile = $webpPath
}
try {
Invoke-RestMethod @uploadSplat
} catch {
Remove-Item $webpPath
throw $_
}
$shareSplat = @{
Authentication = 'Basic'
Credential = $config.Credential
Header = @{
'OCS-APIRequest' = 'true' # https://docs.nextcloud.com/server/27/admin_manual/configuration_user/user_provisioning_api.html
}
Method = 'Post'
Uri = $config.BaseUrl + '/ocs/v2.php/apps/files_sharing/api/v1/shares'
Body = @{
path = $nextcloudPath
shareType = 3 # public link
permissions = 1 # read
}
}
try {
$share = Invoke-RestMethod @shareSplat
} catch {
Remove-Item $webpPath
throw $_
}
$share.ocs.data.url + '/preview' | wl-copy --trim-newline
notify-send 'Screenshot URL in clipboard' --expire-time=1000
Remove-Item $webpPath