Powershell-Public/teedy.ps1

257 lines
9.5 KiB
PowerShell
Raw Permalink Normal View History

2020-12-17 02:25:33 -06:00
2020-12-17 12:33:03 -06:00
function get-sitelogin(){
$username = read-host "Teedy Username"
$password = read-host "Teedy Password"
$tologin=@{username="$username";password="$password";}
2020-12-15 02:00:27 -06:00
try{
2020-12-17 12:33:03 -06:00
$loginresponse = Invoke-webrequest -Uri "$siteurl/api/user/login" -Method POST -Body $tologin -SessionVariable Session
2020-12-15 02:00:27 -06:00
} catch {
if(($error[0].ErrorDetails.Message|convertfrom-json|select-object -ExpandProperty Type) -eq 'ValidationCodeRequired'){
$mfacode = read-host "MFA Code Required for user. Please enter MFA Code:"
if($mfacode -match '\d{6}'){
$tologin.add('code',$mfacode)
2020-12-17 12:33:03 -06:00
$loginresponse = Invoke-webrequest -Uri "$siteurl/api/user/login" -Method POST -Body $tologin -SessionVariable Session
2020-12-15 02:00:27 -06:00
}
}
}
2020-12-17 12:33:03 -06:00
$global:session=$session
2020-12-15 02:00:27 -06:00
if($loginresponse.baseresponse.StatusCode -eq 200){
write-host "Logged in successfully"
}
2020-12-17 12:33:03 -06:00
$headercookie = $loginresponse.baseresponse.headers.getvalues('Set-Cookie')
2020-12-15 02:00:27 -06:00
$token,$null = $headercookie -split ";"
2020-12-17 12:33:03 -06:00
$global:headers=@{
2020-12-15 02:00:27 -06:00
Cookie = "$token"
}
2020-12-17 12:33:03 -06:00
return $global:headers
2020-12-15 02:00:27 -06:00
}
2020-12-17 12:33:03 -06:00
$siteurl = read-host "Teedy URL (i.e. https://demo.teedy.io)"
$global:headers = get-sitelogin
$global:taghash=@{}
2020-12-16 16:30:56 -06:00
function New-Tag(){
param(
$TagName,
$ParentTagName="",
$color="3a87ad"
)
2020-12-17 02:25:33 -06:00
if($tagname.length -gt 36){
$tagname = $tagname.substring(0,36)
}
2020-12-16 16:30:56 -06:00
try{
2020-12-17 02:25:33 -06:00
if($color -eq "3a87ad"){
$colorcode="$color"
} else {
$colorcode = ("{0:X}" -f [drawing.Color]::FromName($color).toargb()).Substring(2)
}
2020-12-16 16:30:56 -06:00
}catch{
$error[0]
write-host "Unable to determine color code. Using default blue."
$colorcode = '3a87ad'
}
Update-TagHash
2020-12-17 02:25:33 -06:00
try{
2020-12-16 16:30:56 -06:00
if($global:taghash[$TagName]){
2020-12-17 02:25:33 -06:00
return "TAG $tagname already exists."
2020-12-16 16:30:56 -06:00
}
if((-not($global:taghash[$ParentTagName])) -and ($ParentTagName -ne '') ){
$parentTagID = (New-Tag -TagName $ParentTagName -ParentTagName '').id
} else{
if($ParentTagName -eq ''){
$parentTagID=''
} else {
$parentTagID=$global:taghash[$ParentTagName].id
}
}
$mytagtocreate = @{
2020-12-17 02:25:33 -06:00
name=$TagName -replace ' ','_' -replace ':','_';
2020-12-16 16:30:56 -06:00
parent=$parentTagID;
color="#$colorcode";
}
2020-12-17 02:25:33 -06:00
#$mytagtocreate
2020-12-17 12:33:03 -06:00
$newtagid = Invoke-RestMethod -uri "$siteurl/api/tag" -Headers $global:headers -Method PUT -body $mytagtocreate -ContentType 'application/x-www-form-urlencoded' -WebSession $Session
2020-12-16 16:30:56 -06:00
Update-TagHash
2020-12-17 02:25:33 -06:00
} catch {
$error[0]
}
$newtagid.id
2020-12-16 16:30:56 -06:00
}
function Remove-Tag(){
param(
[parameter(mandatory)][string]$TagName
)
$tagid = $taghash[$tagname].id
if($tagid){
2020-12-17 12:33:03 -06:00
$result = Invoke-RestMethod -uri "$siteurl/api/tag/$tagid" -Headers $global:headers -Method DELETE -WebSession $Session
2020-12-16 16:30:56 -06:00
Update-TagHash
} else {
$result = "$tagname not found"
#continue
}
$result
}
function update-tag(){
param(
[parameter(Mandatory)][string]$TagName,
[parameter()][string]$ParentTagName,
[parameter()][string]$Color
)
update-taghash
if($taghash[$TagName]){
$mytag = $taghash[$tagname]
if($color){
try{
$colorcode = ("{0:X}" -f [drawing.Color]::FromName($color).toargb() ).Substring(2)
$mytag.color = "#$colorcode"
} catch{
$error[0]
write-host "Color $color not found, not changing"
}
}
if($taghash[$ParentTagName]){
$mytag.parent = $taghash[$ParentTagName].id
}
$tagid=$mytag.id
$mytag
$topost=@{
name=$mytag.name;
id=$mytag.id;
parent=$mytag.parent;
color=$mytag.Color
}
2020-12-17 12:33:03 -06:00
Invoke-RestMethod -uri "$siteurl/api/tag/$tagid" -Headers $global:headers -Method POST -Body $topost -ContentType 'application/x-www-form-urlencoded' -WebSession $Session
2020-12-16 16:30:56 -06:00
} else {
write-host "$tagname not found"
}
}
function Update-TagHash(){
2020-12-17 12:33:03 -06:00
$uri = [System.UriBuilder]"$siteurl/api/tag/list"
$taglist = Invoke-RestMethod -uri $uri.uri -Headers $global:headers -Method GET -WebSession $global:session | select-object -ExpandProperty tags
2020-12-16 16:30:56 -06:00
#if($taglist){write-host "Got tags"}
$global:taghash=@{}
foreach($tag in $taglist){
$global:taghash.add($tag.name, @{ID=$tag.id;Name=$tag.name;Parent=$tag.parent;Color=$tag.color})
$global:taghash.add($tag.id, @{ID=$tag.id;Name=$tag.name;Parent=$tag.parent;Color=$tag.color})
}
}
function Attach-File(){
param(
$documentID,
$fileID
)
foreach($file in @($fileID)){
foreach($document in @($documentID)){
$toattach=@{
fileID=$file;
id=$document
}
2020-12-17 12:33:03 -06:00
Invoke-RestMethod -uri "$siteurl/api/file/$file/attach" -Headers $global:headers -Method POST -Body $toattach -ContentType 'application/x-www-form-urlencoded' -WebSession $Session
2020-12-16 16:30:56 -06:00
}
}
}
function New-Document(){
param(
$title,
$language='eng',
2020-12-17 04:02:47 -06:00
$tags,
2020-12-16 16:30:56 -06:00
$file
)
if($file){
$fileids= Add-File -Files $file
}
2020-12-17 04:02:47 -06:00
update-taghash
$mytags=@()
foreach($mytag in $tags){
$mytags += $taghash[$mytag].id
2020-12-16 16:30:56 -06:00
}
2020-12-17 04:02:47 -06:00
$title=[System.Web.HttpUtility]::UrlEncode($title)
$basequery = "title=$title&language=$language"
if ($tags) { $tagsquery = '&tags={0}' -f ($mytags -join '&tags=') }
2020-12-17 12:33:03 -06:00
$newdocid = (Invoke-RestMethod -uri "$siteurl/api/document" -Headers $global:headers -Method PUT -body "$($basequery)$($tagsquery)" -ContentType 'application/x-www-form-urlencoded' -WebSession $Session).id
2020-12-17 02:25:33 -06:00
if($file){
2020-12-17 04:02:47 -06:00
attach-file -documentid $newdocid -fileid $fileids
2020-12-17 02:25:33 -06:00
}
2020-12-17 04:02:47 -06:00
$newdocid
2020-12-17 02:25:33 -06:00
}
2020-12-16 16:30:56 -06:00
Function Add-File(){
param(
$Files
)
$fileids = @()
foreach($file in $files){
if(test-path $file){
$toupload = get-item $file
2020-12-17 12:33:03 -06:00
c:\windows\system32\curl.exe -H "Cookie: $($global:headers['Cookie'])" --url "$siteurl/api/file" --upload-file $toupload.FullName
$response =curl.exe --location --request PUT 'https://docs.hamik.net/api/file' --header "Cookie: $($global:headers['Cookie'])" --form "file=@`"$($toupload.fullname)`""
$fileid=($response|convertfrom-json).id
$fileids += $fileid
#$fileids += (Invoke-RestMethod -uri "$siteurl/api/file" -Headers $global:headers -Method PUT -form @{$toupload} -ContentType "multipart/form-data").id
2020-12-16 16:30:56 -06:00
}
}
$fileids
}
2020-12-17 02:25:33 -06:00
function Add-Directory(){
param(
$AnchorTag='DirUploadTest',
$Directory='C:\Users\dan\teedytest',
[switch]$DontUseExistingTags,
[switch]$OnlyCreateTags,
$Tags
2020-12-17 02:25:33 -06:00
)
Update-TagHash
if(-not($taghash[$AnchorTag])){
new-tag -TagName $AnchorTag
}
$directories = get-childitem -Path $directory -Directory -Recurse
2020-12-17 12:33:03 -06:00
$directories+= Get-item -path $directory
2020-12-17 02:25:33 -06:00
foreach($mydirectory in $directories){
2020-12-17 12:33:03 -06:00
if($mydirectory.FullName -eq $directory){
$newtagname=$AnchorTag
}else{
$myparts = @(($mydirectory.fullname -replace [regex]::escape($directory),'').substring(1) -split '\\')
#$mydirectory.FullName
#$myparts.count
for($i=0;$i -lt $myparts.count;$i++){
$myparts[$i]=$myparts[$i] -replace ' ','_' -replace ':',''
if(-not($taghash[$myparts[$i]])){
if($i -eq 0){
write-host "Creating Tag $($myparts[$i])"
new-tag -TagName $myparts[$i] -ParentTagName $AnchorTag
} else{
write-host "Creating Tag $($myparts[$i])"
new-tag -TagName $myparts[$i] -ParentTagName $myparts[$i-1]
}
2020-12-17 02:25:33 -06:00
}
}
2020-12-17 12:33:03 -06:00
$newtagname = $myparts[-1]
2020-12-17 02:25:33 -06:00
}
if(-not $OnlyCreateTags){
$files = get-childitem -Path $mydirectory.FullName -File | select-object -ExpandProperty FullName
if($files.count -gt 0){
if((split-path $files[0] -parent) -eq $Directory){
New-Document -title $mydirectory.FullName -tags @($AnchorTag,$tags) -file $files
2020-12-17 02:25:33 -06:00
} else {
New-Document -title $mydirectory.FullName -tags @($newtagname,$tags) -file $files
2020-12-17 02:25:33 -06:00
}
}
}
}
}
2020-12-17 12:33:03 -06:00
$importdir = read-host "Please specify the path to import into Teedy"
$anchortag = read-host "What is the anchor tag to import items under?"
$additionalTags = read-host "Any additional tags (comma separated)?"
$tagstoadd = $additionalTags -split ","
Add-Directory -AnchorTag $anchortag -Directory $importdir -tags $tagstoadd
<#
$documentlist = Invoke-RestMethod -uri "$siteurl/api/document/list" -Headers $global:headers -Method GET | select-object -ExpandProperty documents
2020-12-15 02:00:27 -06:00
if($documentlist){write-host "Got docs"}
2020-12-17 12:33:03 -06:00
$filelist = Invoke-RestMethod -uri "$siteurl/api/file/list" -Headers $global:headers -Method GET |Select-Object -ExpandProperty Files
2020-12-15 02:00:27 -06:00
if($filelist){write-host "Got files"}
2020-12-17 12:33:03 -06:00
#>
$logoutresponse = Invoke-webrequest -Uri "$siteurl/api/user/logout" -Headers $global:headers -Method POST -WebSession $global:session
2020-12-15 02:00:27 -06:00
if($logoutresponse.BaseResponse.StatusCode -eq 200){
write-host "logged out successfully"
}