BroadleafCommerce/BroadleafCommerce — pull request #37 by bpolster (2013-03-29 05:58:03+00:00)
Title: Provide ability to server Static Assets from the File System

This pull request adds support for serving static assets from the File System. While the current mode of serving images from the Database solves the problem of multiple VMs accessing shared images, a preferred approach for most would be to use a Network file system to serve images across a Broadleaf cluster.

## New Domain
The `StaticAsset.java` class has been extended to support a new field for the StorageType. To remain backward compatible, a null value for this field will return StorageType.DATABASE.

```java
...
public class StaticAssetImpl implements StaticAsset {
...
@Override
public StorageType getStorageType() {
StorageType st = StorageType.getInstance(storageType);
if (st == null) {
return StorageType.DATABASE;
} else {
return st;
}
}

@Override
public void setStorageType(StorageType storageType) {
this.storageType = storageType.getType();
}
...
```

## New Property Settings
Several new property settings were added to `/broadleaf-contentmanagement-module/src/main/resources/config/bc/cms/common.properties`

```xml
# controls whether or not the AssetService stores (and retrieves) the
# binary asset from the filesystem or database
asset.use.filesystem.storage=true

# The base path where file system assets will be stored.
# Should begin but not end with a "/"
# If not set, the system will use System.setProperty("java.io.tmpdir")
asset.server.file.system.path=/broadleaf/assets

# URLs for images and other assets will be stored efficiently on the filesystem
# using a set of generated directories based on the md5 value of the filename.
# The number below will be used to determine the number of directories created.
# 2 supports a distribution of about 10 million files and is suitable for most purposes.
# Upping to 3 would add support for over 2 billion. But for implementations of this
# size, distributed file system options should be considered.
asset.server.max.generated.file.system.directories=2

# The maximum file size that can be uploaded (10 Meg)
# Implementors should tune this according to their needs.
asset.server.max.uploadable.file.size=10000000

# The number of bytes from the input stream that will be read at a time
asset.server.file.buffer.size=8192
```

## Other Notes
- Broadleaf generally recommends the use of a CDN such as Akamai or Amazon CloudFront to serve static content
- This code is being targeted for inclusion in Broadleaf Commerce 3.0 and in its current form would not be compatible with the 2.x BLC admin
- The approach used for writing files is to write to a filename based on a UUID which is then renamed to the target file once writing is complete