How can I get all the information about a asset?

I have a collection that has a field of type image.

For example:

[
	{
		"image" : {
			"path":"\/cockpit\/storage\/uploads\/2020\/12\/08\/image.png"
		},
		"_mby":"5fa40497363238bd460000fe",
		"_by":"5fa40497363238bd460000fe",
		"_modified":1607497476,
		"_created":1607497476,
		"_id":"5fd07704393966912a0001f2"
	}
]

How can I get all the information about a asset? The problem is that I don’t have a asset id.

$app->storage->findOne('cockpit/assets', ['_id' => /* asset id */]);

You have to remove /cockpit/storage/uploads from the path and than you can query the database with:

$asset = $app->storage->findOne('cockpit/assets', ['path' => '/2020/12/08/image.png']);

I had the same problem before and solved it here.

This should work (not tested):

// img src: /cockpit/storage/uploads/2020/12/08/image.png

$src = $entry['image']['path'];

// remove `/storage/uploads` from image urls
$uploads = \str_replace(COCKPIT_ENV_ROOT, '', $app->path('#uploads:'));
if (\strpos($src, $uploads) === 0) {
    $src = \substr($src, \strlen($uploads));
}

// eventually same result as above, not tested
//$src = $app->pathToUrl($app->path('#uploads:'));


// img src is now: /2020/12/08/image.png

$asset = $app->storage->findOne('cockpit/assets', ['path' => $src]);

print_r($asset);
1 Like

Thank you! :slightly_smiling_face: