crashzero
Goto Top

Nextcloud-API Freigabelinkerstellung mit Tagübergabe

Moin,

ich mal wieder mit einer Frage.
Ich habe via PowerShell eine Freigabe (API) erstellt mit Ablaufdatum usw.
Nun habe ich im Nextcloud Tags erstellt (7Tage nach Erstellung (Name : 7Tage) sowie 30Tage nach Erstellung (Name : 30Tage) Datei automatisch löschen.

Wie kann ich via Api-Freigabe-URL Aufruf den entsprechenden Tag mitgeben ?

API-URL zum Generieren der Freigabe :
$response = Invoke-RestMethod -Uri "$oc/ocs/v2.php/apps/files_sharing/api/v1/shares" -Method Post -Headers $headers -Body @{path="Pfad1/Pfad2/$dateiname";shareType=3;password=$ncpassword;permissions=1;expireDate=$ncablaufdatum}  

Mit den Tags (Retention) erwarte ich mir das die Dateilöschung jeweils entweder nach 7Tagen (Tag : 7Tage) oder eben nach 30 Tagen (Tag : 30Tage) nach Dateierstellung automatisch gelöscht wird.

Content-Key: 1912480085

Url: https://administrator.de/contentid/1912480085

Printed on: April 26, 2024 at 22:04 o'clock

Member: colinardo
Solution colinardo Feb 14, 2022 updated at 16:03:05 (UTC)
Goto Top
Servus wieder einmal @Crashzero!
Wie kann ich via Api-Freigabe-URL Aufruf den entsprechenden Tag mitgeben ?
Über die Share-API, gar nicht!
Erst einmal, das Share-API hat nichts mit dem Tagging von Dateien zu tun. Das macht das WebDAV-API mit dem du auch schon die Dateien hochlädst (Details siehe https://doc.owncloud.com/server/next/developer_manual/webdav_api/tags.ht ... , nicht von owncloud irritieren lassen, das Tagging ist hier gleich wie bei Nextcloud). Wir arbeiten hier also mit dem /remote.php/dav Endpoint der Nextcloud-Instanz.
Das ganze ist etwas komplexer, da wir hier nicht mit Invoke-Restmethod arbeiten können weil das bestimmte HTTP Methoden wie PROPFIND nicht unterstützt. Daher hier mal ein kommentiertes Beispiel.
Der Ablauf ist dabei folgender
  • Holen der fileid für die bestimmte Datei
  • Die id des Tags (7Tage/30Tage) aus der Instanz ermitteln
  • Prüfen ob der Tag der Datei schon zugewiesen wurde
  • und wenn nicht zuweisen des Tags zur Datei

back-to-topNextcloud: Einer Datei einen Tag per API zuweisen

# endpoint url
$endpoint = 'https://my.nextcloud.tld'  
# credentials
$user = 'userxyz'  
$pass = 'Passw0rd'  
# filepath (here no leading backslash)
$filepath = 'Pfad1/Pfad2/test.txt'  
# tag to assign
$tagname = '7Tage'  
# =========================================
# create credential object
$creds = (new-Object PSCredential($user,(ConvertTo-SecureString $pass -AsPlainText -Force)))
# custom restmethod
function Invoke-CustomRestMethod {
    param(
        [string]$url,
        [string]$body,
        [string]$contenttype,
        [string]$method,
        [pscredential]$credentials
    )

    $data = [System.Text.Encoding]::UTF8.GetBytes($body)
    $wr = [System.Net.WebRequest]::Create($url)
    $wr.Credentials = $credentials
    if ($contenttype){
        $wr.ContentType = $contenttype
    }
    $wr.Method = $method
    if ($body){
        $rs = $wr.GetRequestStream()
        $rs.Write($data,0,$data.length)
        $rs.Flush();$rs.Close();$rs.Dispose()
    }
    $reader = [System.IO.StreamReader]::New($wr.GetResponse().GetResponseStream());
    return $reader.ReadToEnd()
}

# property request definitions
$propsfile = @'  
<?xml version="1.0"?>  
<d:propfind  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">  
  <d:prop>
        <oc:fileid />
  </d:prop>
</d:propfind>
'@  
$propstag = @'  
<?xml version="1.0"?>  
<d:propfind  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">  
  <d:prop>
        <oc:display-name />
        <oc:id />
  </d:prop>
</d:propfind>
'@  

# get fileid of file
$fileid = ([xml](Invoke-CustomRestMethod -url "$endpoint/remote.php/dav/files/$user/$filepath" -body $propsfile -method PROPFIND -credentials $creds -contenttype "text/xml")).multistatus.response.propstat.prop.fileid  
if (!$fileid){
    write-error -Message "ID for file '$filpath' not found."  
    return
}
# get tag id of specific tag
$tagid = ([xml](Invoke-CustomRestMethod -url "$endpoint/remote.php/dav/systemtags" -method PROPFIND -credentials $creds -body $propstag)).multistatus.response.propstat.prop | ?{$_.'display-name' -eq $tagname} | select -Expand Id  

if (!$tagid){
    write-error -Message "Tag-ID tag '$tagname' not found."  
    return
}

# if not tag is already assigned add it
if (!(([xml](Invoke-CustomRestMethod -url "$endpoint/remote.php/dav/systemtags-relations/files/$fileid" -method PROPFIND -credentials $creds -body $propstag -contenttype "text/xml")).multistatus.response.propstat.prop | ?{$_.id -eq $tagid})){  
    # assign tag
    write-host "Assigning tag '$tagname' to file '$filepath'" -F Green  
    Invoke-CustomRestMethod -url "$endpoint/remote.php/dav/systemtags-relations/files/$fileid/$tagid" -method PUT -credentials $creds  
}else{
    write-host "Tag '$tagname' is already assigned to '$filepath', no change needed." -F Gray  
}

Grüße Uwe
Member: Crashzero
Crashzero Feb 15, 2022 at 06:15:37 (UTC)
Goto Top
Ok, verstehe.
Das probiere ich gleich mal aus ......
Member: Crashzero
Crashzero Feb 15, 2022 at 11:48:08 (UTC)
Goto Top
Sauber, funktioniert einwandfrei - Ich danke dir ......
Member: colinardo
colinardo Feb 15, 2022 at 12:16:58 (UTC)
Goto Top
👍 Immer gerne.