Retrieve assets by folders

I have a folder, let’s say ‘gallery’

I can currently get all assets with the https://getcockpit.com/documentation/api/cockpit

/api/cockpit/assets?token=xxtokenxx'

If I want to filter assets only within the gallery folder, how will I go about that?

Is it possible to use a custom query passed along in the body of the request to do that?

Currently what I’m thinking of a workaround is simply getting all the assets, then filtering locally in the browser, based on a tag, which I’ll have to set on all the images within the gallery folder.

Yes, you need to pass the folder id in the filter like:

{
	"filter": {
		"folder": "5e3c2582132d6b00584168f2"
	}
}

Issue is that you may need to do a query first to get the folder ids and if not wrong the core API doesn’t provide that, but you can add it like:

Create a new file in config/api/, e.g. config/api/folder.php

<?php

return $this->storage->findOne('cockpit/assets_folders', ['name' => $this->param('name')]);

So if you invoke the endpoint /api/folder?token=xxxx&name=FolderX you’ll receive the folder data:

{
  "_id": "5e3c2ceb132d6b00584168f3",
  "name": "FolderX",
  "_p": "",
  "_by": "5d8e7300d54909004c78b6c2"
}
2 Likes