The assets components is a component used for stores files. It provides an abstraction on how the file is stored so that it is possible to use the component on different platforms. More specifically, it allows for the file to be stored on the local machine, in the database as a blob or in a remote system.
3 types are defined for assets: internal, local and remote.
Internal assets are stored in chunks in the database. The size of these chunks is defined by the paramTargetValue ‘assetChunkSize’. Note that the size of the chunks cannot be larger than the size defined in the database.
Increasing this size will increase the speed of storing and retrieving the files, but will also store the files less efficiently.
Local assets are stored in the local file system. It uses the StringFile valuetype to store the file. Removing the assets will also remove the local files.
Remote assets are defined with a URI. The asset can then be downloaded and used in the application. Deleting a remote asset will not affect the file described by the URI.
Assets in backend
Assets can also be created and retrieved programmatically through the AssetAgent.
Creating a new Asset
The AssetAgent has a method called
uploadAsset that can be used to create new assets. It takes an AssetStream object as parameter.
Say we have a byte-array that we want to store as an asset. The AssetStream object expects an InputStream object so we create the following:
ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
We also need to define a number of details on our asset, so we create an AssetDetails object:
AssetDetails assetDetails = new AssetDetails(); assetDetails.setName("doc.pdf"); assetDetails.setType("FILE"); assetDetails.setContentType("application/pdf"); assetDetails.setByteSize(1024L);
We then create an AssetStream object and pass it to the uploadAsset method:
AssetStream assetStream = new AssetStream(); assetStream.setInputStream(inputStream); assetStream.setAssetDetails(assetDetails); CrudsResult<DataRef> result = assetAgent.uploadAsset(assetStream);
Retrieving an Asset
The AssetAgent also has a method getAssetStream to retrieve an asset. It returns an inputStream that can be used to retrieve the actual file.
CrudsResult<AssetStream> result = assetAgent.getAssetStream(assetRef); AssetStream assetStream = result.getValue(); InputStream inputStream = assetStream.getInputStream();