2

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.

Matze
  • 5,100
  • 6
  • 46
  • 69
Julian
  • 1,694
  • 22
  • 29

2 Answers2

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.

user1703761
  • 1,086
  • 3
  • 11
  • 23
  • Hello Bernhard, I just tried to upload a file with this command `curl -H "Content-Type: application/json" -X POST -d "{ \"tags\" : [ \"xxx\"] }" http://user:pass@localhost/nextcloud/index.php/apps/files/api/v1/user/test/test.pdf` but unfortunately nothing happens. I do not get any negative feedback, neither from the nextcloud Server, nor from the curl command. Do you have an idea what I did wrong? Thanks a lot. – Julian Mar 02 '17 at 23:04
  • I have tried something differently `curl -H "Content-Type: application/json" -X POST -d "{ \"tags\" : [ \"xxx\" , \"k\" ] }" -u user:pw http://localhost:4080/nextcloud/index.php/apps/files/api/v1/files/user/test`. Now I am getting the error message: `{"message":"CSRF check failed"}%` – Julian Mar 03 '17 at 10:07
  • 1
    You are correct https://github.com/nextcloud/server/blob/master/apps/files/lib/Controller/ApiController.php#L141 does not add @NoCSRFRequired so it's only callable from the web interface. So it's not possible to do after all :( – user1703761 Mar 06 '17 at 13:23
  • Thank you very much for your help. I have developed a simple python tagging deamon that runs permanently on my owncloud server. Actually it is pretty straightforward to put the tags right inside the db. – Julian Mar 06 '17 at 21:29
  • I just made the code of the small tagging daemon available - http://stackoverflow.com/a/43081850/5068458 – Julian Mar 28 '17 at 23:59
0

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.

sthenault
  • 14,397
  • 5
  • 38
  • 32