Log In | Sign Up   View a printable version of the current page.
  Dashboard > [fleXive] > ... > How-Tos > Import a folder of images on the server

Added by Daniel Lichtenberger, last edited by Daniel Lichtenberger on Sep 14, 2009  (view change)
Labels: 
(None)

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.

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.

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
    }
}

Import a folder hierarchy

Take a look at Import a folder structure of documents on the server for an example of how to traverse a folder structure recursively.

Site running on a free Atlassian Confluence Open Source Project License granted to [fleXive] . Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.1 Build:#916 Nov 09, 2007) - Bug/feature request - Contact Administrators