implement nextcloud-screenshot.ps1

master
lub 1 year ago
parent 9f93d04ff9
commit 9b863196db

@ -0,0 +1,94 @@
#!/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
if ((Get-Item $webpPath).Length -eq 0) {
Remove-Item $webpPath
throw ('WebP file "{0}" is empty' -f $webpPath)
} else {
Remove-Item $tmpPath
}
$webpName = (Split-Path -Leaf $webpPath)
$nextcloudPath = $config.Path + '/' + $webpName
$uploadSplat = @{
Authentication = 'Basic'
#Credential = New-Object System.Management.Automation.PSCredential (($config.Password | ConvertFrom-SecureString -AsPlainText), (New-Object System.Security.SecureString))
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 {
throw $_
Remove-Item $webpPath
}
$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 {
throw $_
Remove-Item $webpPath
}
$share.ocs.data.url | wl-copy --trim-newline
notify-send 'Screenshot URL in clipboard' --expire-time=1000
Remove-Item $webpPath
Loading…
Cancel
Save