Download

Get Loom

Persistent files

Uploading files with Loom

Starting with 2.0, uploaded files are not automatically bound to java attributes. It is expected that you get any uploaded file with request.getFileParameter() and save it manually. Loom provides several persistence implementations for uploaded files that can be used out-of-the-box.

PersistentFile

PersistentFile is an useful abstraction that may be used for persisted files. While persisting, the file contents get separated from the file object depending on your persistence strategy:

  • BlobPersistentFileContents: Persist the file contents in a separate database table, as a blob.
  • ByteArrayPersistentFileContents: Same as BlobPersistentFileContents, but the contents may be accessed as an array.
  • SystemFileContents: The file is stored outside the database, as a system file.
  • InputStreamFileContents: The file is stored somewhere else, and can only be accessed as an InputStream.

Keeping the file metadata and its contents separated allows for flexible file management, keeping a list of files in the database and their content in some system folder, for example. It also is an effective workaround for handling large persistent files.

Storing and Retrieving PersistentFile instances

You can use one of the default FileManager implementation classes or roll your own:

  • DatabaseFileManager: Store both PersistentFile and its contents in the database.
  • ExternalFileManager: Store PersistentFile in the database and its contents as an external file.

Note that there is no JPA relationship between a PersistentFile and its contents. To get the contents, you should invoke fileManager.find() for each file.

Downloading persistent files

Delivering persistent files from the server is quite easy:

public class FilesAction extends AbstractAction {

	@Injected
	private FileManager fileManager;
	
	/** the id of the file to be retrieved */
	private Integer id;
	
	@Path("/{id}")
	public Resolution getFile() {
		return send(fileManager.find(id));
	}

}