| | h3. Overview |
| | |
| | For testing purposes it can be useful to import a folder of images automatically. This Groovy script imports all files of a folder (on the server) as instances of the predefined Image type. |
| | |
| | h3. Executing the code |
| | |
| | Open the backend administration with a user who can execute scripts in the Groovy console, and open {{Scripts/Script Console}}. Check the "Execute at Web layer" box to use one transaction per image import (otherwise timeouts may occur). Copy and paste the code below and click execute. The import messages are written to stdout. |
| | |
| | h3. Code |
| | |
| | {code} |
| | import com.flexive.shared.* |
| | import com.flexive.shared.content.* |
| | import com.flexive.shared.value.* |
| | import com.flexive.shared.exceptions.* |
| | import java.io.FileInputStream |
| | import com.flexive.shared.scripting.groovy.GroovyContentBuilder |
| | |
| | new File("/path/to/images").eachFile { file -> |
| | if (file.directory) { |
| | return; |
| | } |
| | def builder = new GroovyContentBuilder("IMAGE") |
| | try { |
| | builder { |
| | imageBinary(new FxBinary(false, new BinaryDescriptor(file.name, new FileInputStream(file)))) |
| | } |
| | EJBLookup.contentEngine.save(builder.getContent()) |
| | println "Imported " + file.name |
| | } catch (Exception e) { |
| | println "Failed to import file: " + e |
| | } |
| | } |
| | {code} |
| | |
| | h3. Import a folder hierarchy |
| | |
| | | See [Import a folder structure of documents on the server] for an example of how to traverse a folder structure recursively. |
| | | Take a look at [Import a folder structure of documents on the server] for an example of how to traverse a folder structure recursively. |