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

PropertyReturnsDescription
contentStorageContentPrefixStringThe 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.

ParameterDescription
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.

ParameterDescription
dirthe 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.

ParameterDescription
filePrefixthe 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.

ParameterDescription
dirthe 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.

ParameterDescription
dirthe 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.

ParameterDescription
dirthe directory to list, or null for the tenant's root storage directory
maxFilesthe 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.

ParameterDescription
dirthe directory to list, or null for the tenant's root storage directory
filePrefixthe file name prefix to match

deleteFiles(List<String> fileNames)

Returns: void

Deletes the named files from the tenant's root storage directory.

ParameterDescription
fileNamesthe names of the files to delete

deleteFiles(String dir, List<String> fileNames)

Returns: void

Deletes the named files from the given directory.

ParameterDescription
dirthe directory the files are stored in, or null for the tenant's root storage directory
fileNamesthe names of the files to delete

deleteDirectory(String toRemoveDirectory)

Returns: void

Deletes the named directory and its contents from per-tenant storage.

ParameterDescription
toRemoveDirectorythe 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.

ParameterDescription
obthe 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.

ParameterDescription
inthe 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.

ParameterDescription
contentNamethe name to store the content under
binthe 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.

ParameterDescription
paththe directory path, within the tenant's storage, to store the content under
contentNamethe name to store the content under
binthe 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.

ParameterDescription
dirthe directory to store the content in, or null for the tenant's root storage directory
contentNamethe name to store the content under
binthe 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.

ParameterDescription
dirthe directory to store the content in, or null for the tenant's root storage directory
contentNamethe name to store the content under
filethe uploaded file to save
allowedMimeTypesthe 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.

ParameterDescription
filethe uploaded file to check
allowedMimeTypesthe 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.

ParameterDescription
dirthe directory to store the content in, or null for the tenant's root storage directory
fileNamethe name to store the content under
overwritecurrently has no effect; content is always saved with overwrite enabled
writercallback 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.

ParameterDescription
contentNamethe name the content was stored under
consumercallback 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.

ParameterDescription
dirthe directory the content was stored in, or null for the tenant's root storage directory
contentNamethe name the content was stored under
consumercallback 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.

ParameterDescription
directoryNamethe 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.

ParameterDescription
contentNamethe name to check

contentExists(String dir, String contentName)

Returns: boolean

Checks whether content with the given name exists in the given directory.

ParameterDescription
dirthe directory to check, or null for the tenant's root storage directory
contentNamethe name to check

storeFile(InputStream bin)

Returns: String

Saves the given content to the per-tenant blob store and returns its hash.

ParameterDescription
binthe 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.

ParameterDescription
inthe 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.

ParameterDescription
inputStreamthe zip stream to iterate over
consumercallback 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.

ParameterDescription
inputStreamthe InputStream containing GZIP-compressed data
consumerthe 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.

ParameterDescription
othe 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.

ParameterDescription
hashthe 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.

ParameterDescription
fileNamethe name to store the content under
inthe 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.

ParameterDescription
fileNamethe name the content was stored under
ccallback 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.

ParameterDescription
ina 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.

ParameterDescription
fileItemthe uploaded file

findFileExtension(String fileName)

Returns: String

Finds the extension of a file name.

ParameterDescription
fileNamethe 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.

ParameterDescription
dirthe directory the source image is stored in, or null for the tenant's root storage directory
contentNamethe name the source image is stored under
newContentNamethe name to save the cropped image under
boundarythe crop boundary as a map with "left", "top", "width" and "height" entries, in pixels
formatNamethe 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.

ParameterDescription
inthe image to rotate
anglethe rotation angle in degrees; must be 0, 90, 180 or 270
formatNamethe 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.

ParameterDescription
inthe image to rotate
anglethe rotation angle in degrees; must be 0, 90, 180 or 270
formatNamethe output image format, e.g. "png" or "jpg"
compressionQualitythe 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.

ParameterDescription
imageInputStreamThe 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).

ParameterDescription
imageInputStreamthe 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.

ParameterDescription
hashthe 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.

ParameterDescription
dirthe parent directory name
contentNamethe 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.

ParameterDescription
directorythe directory the content is stored in, or blank if it is in the tenant's root storage directory
contentNamethe 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.

ParameterDescription
contentNamethe 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.

ParameterDescription
outputDirthe directory to store the zip file in, or null for the tenant's root storage directory
outputNamethe name to store the zip file under
ccallback invoked with a ZipBuilder to add entries to the zip
To get full access to the Kademi Hub existing customers can login here, or new customers can register here.