Download

Get Loom

Persistent files

Uploading files with Loom

Every time a file gets uploaded its file name, size and client-side mime type are included with its contents. Loom provides a common interface to access any request parameter, be it a file or not, using LoomServletRequest.getParam(String paramName). This method can return an instance of StringParam or FileParam. These classes are the low-API to access uploaded file data, and can be used by users who wish to handle uploaded files by themselves.

Supported binding types

Loom includes automatic binding of uploaded files to:

  • InputStream: when binding to an InputStream property every other metadata will be lost and the developer should keep them by hand.
  • PersistentFile: this is a persistent class that holds the uploaded file metadata and its contents.

PersistentFile

PersistentFile is an useful abstraction that may be used for uploaded files, download files, or injected as a spring Resource (using PersistentFileResource). While persisting, the file contents get separated 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, since hibernate ignores FetchType.LAZY in blob properties.

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

}