Manages file content stored on a per-tenant basis in the local file system, keyed by directory and content name. A single instance is shared across the server and reached from Java via the C(FileStorageManager.class) context lookup, and its exported methods are bound into content-type and other scripts as the "fsm" variable, so a script calls fsm.storeContent(...), fsm.readContent(...) and so on. Storage is always scoped to the current tenant, resolved from CurrentTenantService, so these methods can only be used within a request or other context that has a current tenant.
Group: Managers
Implements: AssetContentStorageService
Properties
| Property | Returns | Description |
|---|---|---|
| contentStorageContentPrefix | String | The prefix used to identify content stored in per-tenant storage. Only content ids carrying this prefix can be parsed by DefaultHashContentService. |
Methods
listDirs() · listDirs(String dir) · listDirectoryInfo(String dir) · listFiles(String filePrefix) · fileStats(String dir) · fileInfo(String dir) · fileInfo(String dir, Integer maxFiles) · listFiles(String dir, String filePrefix) · deleteFiles(List<String> fileNames) · deleteFiles(String dir, List<String> fileNames) · deleteDirectory(String toRemoveDirectory) · toInputStream(Object ob) · streamToString(InputStream in) · storeContent(String contentName, InputStream bin) · storeContentToPath(Path path, String contentName, InputStream bin) · storeContent(String dir, String contentName, InputStream bin) · storeContent(String dir, String contentName, FileItem file, List<String> allowedMimeTypes) · parseFileInputStream(FileItem file, List<String> allowedMimeTypes) · saveWithOutput(String dir, String fileName, boolean overwrite, Consumer<OutputStream> writer) · readContent(String contentName, Consumer<ContentInfo> consumer) · readContent(String dir, String contentName, Consumer<ContentInfo> consumer) · directoryExists(String directoryName) · contentExists(String contentName) · contentExists(String dir, String contentName) · storeFile(InputStream bin) · storeZipFile(InputStream in) · processZipInputStream(InputStream inputStream, Consumer<Map<String,Object>> consumer) · processGzipInputStream(InputStream inputStream, Consumer<InputStream> consumer) · getAsByteArray(Object o) · readFile(String hash) · saveAssetContent(String fileName, InputStream in) · lookupAssetContent(String fileName, Consumer<ContentInfo> c) · detectContentTypeForInputStream(InputStream in) · findFileExtension(FileItem fileItem) · findFileExtension(String fileName) · cropImage(String dir, String contentName, String newContentName, Map<String,Integer> boundary, String formatName) · rotateImage(InputStream in, int angle, String formatName) · rotateImage(InputStream in, int angle, String formatName, Float compressionQuality) · getExifRotation(InputStream imageInputStream) · getExifData(InputStream imageInputStream) · getExifDataByHash(String hash) · getExifDataByPath(String dir, String contentName) · getContentStorageContentPrefix() · generateContentStorageContentId(String directory, String contentName) · extractContentStorageFileName(String contentName) · exportToZip(String outputDir, String outputName, Consumer<ZipBuilder> c)
listDirs()
Returns: List<String>
List the first-level subdirectories of the current tenant's root storage directory.
listDirs(String dir)
Returns: List<String>
List the first-level subdirectories within the directory.
| Parameter | Description |
|---|---|
dir | {@code String} |
listDirectoryInfo(String dir)
Returns: List<Map<String,Object>>
List the first-level subdirectories of the given directory, with details about each one.
| Parameter | Description |
|---|---|
dir | the parent directory to list subdirectories of, or null for the tenant's root storage directory |
listFiles(String filePrefix)
Returns: List<String>
List the file names in the tenant's root storage directory that start with the given prefix, sorted alphabetically.
| Parameter | Description |
|---|---|
filePrefix | the file name prefix to match |
fileStats(String dir)
Returns: Map<String,Object>
Summary statistics for the files in the given directory, such as file count and total size.
| Parameter | Description |
|---|---|
dir | the directory to summarise, or null for the tenant's root storage directory |
fileInfo(String dir)
Returns: List<Map<String,Object>>
List the files in the given directory, with details such as name and size for each one.
| Parameter | Description |
|---|---|
dir | the directory to list, or null for the tenant's root storage directory |
fileInfo(String dir, Integer maxFiles)
Returns: List<Map<String,Object>>
List the files in the given directory, with details such as name and size for each one, up to a maximum number of files.
| Parameter | Description |
|---|---|
dir | the directory to list, or null for the tenant's root storage directory |
maxFiles | the maximum number of files to return |
listFiles(String dir, String filePrefix)
Returns: List<String>
List the file names in the given directory that start with the given prefix, sorted alphabetically.
| Parameter | Description |
|---|---|
dir | the directory to list, or null for the tenant's root storage directory |
filePrefix | the file name prefix to match |
deleteFiles(List<String> fileNames)
Returns: void
Deletes the named files from the tenant's root storage directory.
| Parameter | Description |
|---|---|
fileNames | the names of the files to delete |
deleteFiles(String dir, List<String> fileNames)
Returns: void
Deletes the named files from the given directory.
| Parameter | Description |
|---|---|
dir | the directory the files are stored in, or null for the tenant's root storage directory |
fileNames | the names of the files to delete |
deleteDirectory(String toRemoveDirectory)
Returns: void
Deletes the named directory and its contents from per-tenant storage.
| Parameter | Description |
|---|---|
toRemoveDirectory | the path of the directory to delete |
toInputStream(Object ob)
Returns: InputStream
Wraps the given data as an InputStream, converting it if needed. Accepts a String, StringBuilder, byte array or an existing InputStream (returned unchanged); any other type throws.
| Parameter | Description |
|---|---|
ob | the data to wrap, as a String, StringBuilder, byte array or InputStream |
streamToString(InputStream in)
Returns: String
Reads the entire stream and returns its contents as a string, using the platform default character encoding.
| Parameter | Description |
|---|---|
in | the stream to read |
storeContent(String contentName, InputStream bin)
Returns: long
Saves a content item to the tenant's root storage directory, overwriting any existing content with the same name.
| Parameter | Description |
|---|---|
contentName | the name to store the content under |
bin | the content to store |
storeContentToPath(Path path, String contentName, InputStream bin)
Returns: long
Saves a content item to per-tenant storage rooted at the given directory path, overwriting any existing content with the same name.
| Parameter | Description |
|---|---|
path | the directory path, within the tenant's storage, to store the content under |
contentName | the name to store the content under |
bin | the content to store |
storeContent(String dir, String contentName, InputStream bin)
Returns: long
Saves a content item to the given directory in per-tenant storage, overwriting any existing content with the same name.
| Parameter | Description |
|---|---|
dir | the directory to store the content in, or null for the tenant's root storage directory |
contentName | the name to store the content under |
bin | the content to store |
storeContent(String dir, String contentName, FileItem file, List<String> allowedMimeTypes)
Returns: long
If the given uploaded file's detected content type matches one of the allowed MIME types, saves it to per-tenant content storage and returns its size.
| Parameter | Description |
|---|---|
dir | the directory to store the content in, or null for the tenant's root storage directory |
contentName | the name to store the content under |
file | the uploaded file to save |
allowedMimeTypes | the MIME type prefixes the file's detected content type must match against, or null to skip the check |
parseFileInputStream(FileItem file, List<String> allowedMimeTypes)
Returns: BufferedInputStream
Checks the given uploaded file's detected content type against the allowed MIME types and returns a buffered, mark-supporting stream over its content if it matches.
| Parameter | Description |
|---|---|
file | the uploaded file to check |
allowedMimeTypes | the MIME type prefixes the file's detected content type must match against, or null to skip the check |
saveWithOutput(String dir, String fileName, boolean overwrite, Consumer<OutputStream> writer)
Returns: void
Saves content to the given directory by handing the caller an OutputStream to write to, rather than requiring the content up-front as an InputStream. Content is always saved with overwrite enabled, regardless of the overwrite argument.
| Parameter | Description |
|---|---|
dir | the directory to store the content in, or null for the tenant's root storage directory |
fileName | the name to store the content under |
overwrite | currently has no effect; content is always saved with overwrite enabled |
writer | callback that writes the content to the given OutputStream |
readContent(String contentName, Consumer<ContentInfo> consumer)
Returns: void
Looks up a content item in the tenant's root storage directory and passes it to the given callback.
| Parameter | Description |
|---|---|
contentName | the name the content was stored under |
consumer | callback invoked with the content's ContentInfo, including its data as an InputStream |
readContent(String dir, String contentName, Consumer<ContentInfo> consumer)
Returns: void
Looks up a content item in the given directory and passes it to the given callback.
| Parameter | Description |
|---|---|
dir | the directory the content was stored in, or null for the tenant's root storage directory |
contentName | the name the content was stored under |
consumer | callback invoked with the content's ContentInfo, including its data as an InputStream |
directoryExists(String directoryName)
Returns: boolean
Checks whether the given directory exists in per-tenant storage.
| Parameter | Description |
|---|---|
directoryName | the path of the directory to check |
contentExists(String contentName)
Returns: boolean
Checks whether content with the given name exists in the tenant's root storage directory.
| Parameter | Description |
|---|---|
contentName | the name to check |
contentExists(String dir, String contentName)
Returns: boolean
Checks whether content with the given name exists in the given directory.
| Parameter | Description |
|---|---|
dir | the directory to check, or null for the tenant's root storage directory |
contentName | the name to check |
storeFile(InputStream bin)
Returns: String
Saves the given content to the per-tenant blob store and returns its hash.
| Parameter | Description |
|---|---|
bin | the content to save |
storeZipFile(InputStream in)
Returns: List<Map<String,Object>>
Extracts a zip file and stores each entry in the "zip" directory of per-tenant storage under a timestamp-prefixed name, returning the details of each stored file (its original name, stored name, size and directory). Use readContent with the returned dir and storedName to retrieve a stored entry.
| Parameter | Description |
|---|---|
in | the zip file content to extract |
processZipInputStream(InputStream inputStream, Consumer<Map<String,Object>> consumer)
Returns: void
Iterates over the entries of a zip stream, invoking the callback once per file entry with its name, size, an open InputStream over its content, the entry's file name and its parent path. Directory entries are skipped. Unlike storeZipFile, entries are not saved to per-tenant storage.
| Parameter | Description |
|---|---|
inputStream | the zip stream to iterate over |
consumer | callback invoked once per file entry, with a map describing it |
processGzipInputStream(InputStream inputStream, Consumer<InputStream> consumer)
Returns: void
Processes the contents of a GZIP-compressed InputStream by providing an uncompressed InputStream. This method decompresses the data from the provided InputStream using GZIP.
| Parameter | Description |
|---|---|
inputStream | the InputStream containing GZIP-compressed data |
consumer | the Consumer that accepts the uncompressed InputStream |
getAsByteArray(Object o)
Returns: byte[]
Converts the given data to a byte array. Accepts a byte array (returned unchanged), a String, a FileItem or an InputStream; any other type returns null.
| Parameter | Description |
|---|---|
o | the data to convert, as a byte array, String, FileItem or InputStream |
readFile(String hash)
Returns: InputStream
Reads back the content previously stored in the hash-split blob store under the given hash.
| Parameter | Description |
|---|---|
hash | the content hash, as returned by storeFile |
saveAssetContent(String fileName, InputStream in)
Returns: long
Saves content to the tenant's "assets" directory, implementing the AssetContentStorageService contract used to persist asset binary content.
| Parameter | Description |
|---|---|
fileName | the name to store the content under |
in | the content to store |
lookupAssetContent(String fileName, Consumer<ContentInfo> c)
Returns: void
Looks up content in the tenant's "assets" directory and passes it to the given callback, implementing the AssetContentStorageService contract used to read back asset binary content.
| Parameter | Description |
|---|---|
fileName | the name the content was stored under |
c | callback invoked with the content's ContentInfo, including its data as an InputStream |
detectContentTypeForInputStream(InputStream in)
Returns: Map<String,String>
Detects the content type and corresponding file extension of a stream, using Apache Tika.
| Parameter | Description |
|---|---|
in | a mark-supporting stream over the content to detect; its position is restored before returning |
findFileExtension(FileItem fileItem)
Returns: String
Finds the file extension of an uploaded file, taken from its file name.
| Parameter | Description |
|---|---|
fileItem | the uploaded file |
findFileExtension(String fileName)
Returns: String
Finds the extension of a file name.
| Parameter | Description |
|---|---|
fileName | the file name to extract the extension from |
cropImage(String dir, String contentName, String newContentName, Map<String,Integer> boundary, String formatName)
Returns: void
Crops a stored image and saves the result under a new name in the same directory.
| Parameter | Description |
|---|---|
dir | the directory the source image is stored in, or null for the tenant's root storage directory |
contentName | the name the source image is stored under |
newContentName | the name to save the cropped image under |
boundary | the crop boundary as a map with "left", "top", "width" and "height" entries, in pixels |
formatName | the image format to save the cropped image as, e.g. "png"; defaults to "png" if blank |
rotateImage(InputStream in, int angle, String formatName)
Returns: InputStream
Rotates an image by the given angle, first correcting for any EXIF orientation already recorded against the image so the result always ends up the right way up. The rotation angle must be 0, 90, 180 or 270 degrees. Equivalent to calling the four-argument overload with maximum compression quality.
| Parameter | Description |
|---|---|
in | the image to rotate |
angle | the rotation angle in degrees; must be 0, 90, 180 or 270 |
formatName | the output image format, e.g. "png" or "jpg" |
rotateImage(InputStream in, int angle, String formatName, Float compressionQuality)
Returns: InputStream
Rotates an image by the given angle, first correcting for any EXIF orientation already recorded against the image so the result always ends up the right way up. The rotation angle must be 0, 90, 180 or 270 degrees.
| Parameter | Description |
|---|---|
in | the image to rotate |
angle | the rotation angle in degrees; must be 0, 90, 180 or 270 |
formatName | the output image format, e.g. "png" or "jpg" |
compressionQuality | the compression quality to use when the output format supports it, from 0.0 to 1.0; defaults to 1.0 (maximum quality) if null |
getExifRotation(InputStream imageInputStream)
Returns: int
Reads the EXIF orientation tag and returns the corresponding rotation angle.
| Parameter | Description |
|---|---|
imageInputStream | The InputStream containing the image. |
getExifData(InputStream imageInputStream)
Returns: Map<String,Map<String,String>>
Reads all available metadata tags embedded in an image (EXIF, GPS, IPTC, etc).
| Parameter | Description |
|---|---|
imageInputStream | the InputStream containing the image |
getExifDataByHash(String hash)
Returns: Map<String,Map<String,String>>
Reads all available metadata tags embedded in an image stored under the given content hash.
| Parameter | Description |
|---|---|
hash | the content hash of the stored image, as used by {@link #readFile(String)} |
getExifDataByPath(String dir, String contentName)
Returns: Map<String,Map<String,String>>
Reads all available metadata tags embedded in an image stored at the given path.
| Parameter | Description |
|---|---|
dir | the parent directory name |
contentName | the name of the stored image content |
getContentStorageContentPrefix()
Returns: String
The prefix used to identify content stored in per-tenant storage. Only content ids carrying this prefix can be parsed by DefaultHashContentService.
generateContentStorageContentId(String directory, String contentName)
Returns: String
Builds the standard content id used to reference content stored in per-tenant storage, combining the content storage prefix with the directory and content name.
| Parameter | Description |
|---|---|
directory | the directory the content is stored in, or blank if it is in the tenant's root storage directory |
contentName | the name the content is stored under |
extractContentStorageFileName(String contentName)
Returns: Map<String,String>
Extracts the actual file name and directory name from a content storage content name previously built by generateContentStorageContentId.
| Parameter | Description |
|---|---|
contentName | the content storage content name to parse |
exportToZip(String outputDir, String outputName, Consumer<ZipBuilder> c)
Returns: void
Creates a zip file in per-tenant content storage and hands the caller a ZipBuilder to add entries to it. A typical script reads other stored content and adds each item to the zip via the builder's add method inside the callback.
| Parameter | Description |
|---|---|
outputDir | the directory to store the zip file in, or null for the tenant's root storage directory |
outputName | the name to store the zip file under |
c | callback invoked with a ZipBuilder to add entries to the zip |