Upload asset folder structure

Hi Cockpit team and community!

I have a library of photos organized in folders/subfolders which I would like to upload to Cockpit Assets.

Is there a way to upload a complete folder with all the files and subfolders? Otherwise, is there an API method I could use to do it programmatically ?

Thank you!

Hang

You can do that using the REST API, or by creating a CLI command, in both cases is a straightforward process.

If building a CLI command and assuming your cockpit installation has access to the photos folder:

  1. Iterate over the folder
  2. Create folder with same name in Cockpit
  3. Iterate of the images in the folder, create them on Cockpit using the API

to create a folder you can do something like:

$folder = [
  'name' => $folder_name,
  '_p' => $parent_folder_name // if any
];

$app->storage->save('cockpit/assets_folders', $folder);

keep in mind folders in Cockpit are virtual, they are tagged to the images as an attribute

for the images you need to create them on cockpit (e.g. $app->storage->insert('cockpit/assets', $asset); and copy to the storage, there is CLI import command in the core, maybe you can check the code there: modules/Cockpit/cli/import/assets.php

1 Like

@pauloamgomes Thanks for the concept. :slight_smile:

@gnagnu I wrote that cli command in the last hours. It is a feature, that I’m missing for a long time. I tested it only on my localhost on a Windows machine, but it seems to work well.

Usage:

$ ./cp assets-folder-import --dir "/e/cpimageimport" --list
cpimageimport
  0005_test.jpg
  ...
  test.txt
$ ./cp assets-folder-import --dir "/e/cpimageimport" --ext jpg,png --r --list
cpimageimport
  0005_test.jpg
  ...
cpimageimport > nature
  0017_test.jpg
  ...
cpimageimport > nature > flowers
  0037_test.jpg
  ...
cpimageimport > urban
  0001_test.jpg
  ...
$ ./cp assets-folder-import --dir "/e/cpimageimport" --ext jpg,png --r
Created new virtual folder nature - 5e90d8a39cb38e1b7c006d82
Created new virtual folder flowers - 5e90d8a39cb38e1b7c006d83
Created new virtual folder urban - 5e90d8a39cb38e1b7c006d84
Start to import assets. This may take a while...
Imported 1 of 16 files | mem: 4 MB | peak: 4 MB
...
Imported 16 of 16 files | mem: 30 MB | peak: 106 MB
Imported 16 assets in 52 seconds

The memory and the peak output is because the image import might break your memory limit (ColorThief and SimpleImage are two memory heavy libraries, that are called for each asset).

2 Likes

Thanks @pauloamgomes @raffaelj for your answers. I was able to get it done :slight_smile: