NextCloud file tagging through WebDAV script

自作多情 提交于 2020-02-02 12:08:08

问题


I am using NextCloud 11 to store my personal files, and I use the simple curl script from the documentation in order to upload files to my NextCloud drive:

curl -u user:pw -T test.pdf "http://localhost/nextcloud/remote.php/dav/files/user/test/test.pdf"

Moreover, I would like to directly add some tags to the uploaded files. However, in the official documentation, they just show how files can be uploaded, deleted and moved through the WebDAV interface.

Does anybody have a hint how I could tag a file remotely?

I have posted the same question in the official NextCloud community forum, but I did not receive a response yet. In case I receive a response, I will post it here.


回答1:


POST https://yournextcloud.com/index.php/api/v1/files/path/to/file

Payload is JSON:

{"tags": ["tag1", "tag2"]}

You will need to authenticate yourself using Basic Auth

Edit: The API can only be called from inside Nextcloud because the CSRF token is required.




回答2:


For the record, after a bit of digging I found https://doc.owncloud.com/server/latest/developer_manual/webdav_api/tags.html which does the job for nextcloud as well. In a nutshell:

Get file id for a given file:

curl --silent -u user:password -X PROPFIND -H "Content-Type: text/xml" \
  --data-binary "@file-propfind.xml" https://nextcloud/remote.php/webdav/file' | xmllint --format -

with a file-propfind.xml in your directory containing something like

<?xml version="1.0" encoding="utf-8" ?>
<a:propfind xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns">
  <a:prop>
    <oc:fileid/>
  </a:prop>
</a:propfind>

Then get list of tags for this file using

curl --silent -u user:password -X PROPFIND -H "Content-Type: text/xml" \
  --data-binary "@tags-propfind.xml" https://nextcloud/remote.php/dav/systemtags-relations/files/<FILEID>" | xmllint --format -

where FILEID is the number you got as oc:fileid in the previous response and tags-propfind.xml a file containing something like

<?xml version="1.0" encoding="utf-8" ?>
<a:propfind xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns">
  <a:prop>
    <oc:display-name/>
    <oc:user-visible/>
    <oc:user-assignable/>
    <oc:id/>
  </a:prop>
</a:propfind>

This is for tag reading, but the API document also explain how to add a tag in the same fashion.



来源:https://stackoverflow.com/questions/42550487/nextcloud-file-tagging-through-webdav-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!