Utility for reading and writing content-addressed files, and for parsing CSV, Excel, XML and PDF documents. Registered in the templating context as "fileManager", and also instantiated directly by several controllers and background jobs that need direct access to the platform's BlobStore and HashStore content storage. Many of the upload and read helpers are deprecated in favour of the newer FileStorageManager and RepositoryManager APIs, and are retained only so older scripts that still call them keep working. Also provides CSV, Excel, XML and PDF parsing helpers, and support for locating a repository's branch folder and committing a synced remote hash into a branch's commit history.

Group: Managers


Properties

PropertyReturnsDescription
pdfManagerPdfManagerThe PDF manager instance shared with this FileManager, used for reading, generating and manipulating PDF documents.
utilsFileUtilsThe FileUtils instance shared with this FileManager, used for lower-level file and hash-store operations.

Methods

saveFile(Branch branch, String dirPath, String filename, InputStream in, Profile currentUser) · createFolder(Branch branch, String dirPath, String folderName, Profile currentUser) · convertToInputStream(String content) · uploadFiles(Map<String,FileItem> files) · uploadFile(FileItem file) · upload(InputStream in) · getFileType(String fileName) · upload(byte[] bytes) · getAsInputStream(String hash) · getAsByteArray(String hash) · getAsString(String hash) · getAsString(FileItem fileItem) · getAsBase64(String hash) · getAsBase64(Resource resource) · getAsBase64(InputStream in) · getAsByteArray(InputStream in) · getBranchFolder(String repoName) · getBranchFolder(String repoName, boolean autoCreate) · getBranchFolder(String repoName, String branchName) · getBranchFolder(String repoName, String branchName, boolean autoCreate) · fromCsv(Object o) · fromCsv(Object o, Object seperator) · parseExcel(InputStream in, boolean useXml) · parseExcel(byte[] bytes, boolean useXml) · parseDOM(String s) · parseDOM(InputStream in) · parseXmlToJson(InputStream in) · parseXmlToJson(String rawXml) · parsePDF(String hash, String password, Consumer<PdfDocument> callback) · parsePDF(String hash, Consumer<PdfDocument> callback) · parsePDF(byte[] bytes, Consumer<PdfDocument> callback) · parsePDF(byte[] bytes, String password, Consumer<PdfDocument> callback) · newXmlWriter(OutputStream out) · newXmlBuilder(String rootName) · toCsvAsString(List<List> list) · toCsvAsString(List<List> list, char separator) · toCsvAsString(List<List> list, char separator, char quotechar) · getPdfManager() · getUtils() · getFileFromURI(String fileURI) · commitRemoteHash(String newRemoteHash, String adminUrl, String loginToken, Branch branch) · commitRemoteHash(String newRemoteHash, String adminUrl, String username, String password, Branch branch) · findFileExtension(FileItem fileItem) · findFileExtension(String fileName)

saveFile(Branch branch, String dirPath, String filename, InputStream in, Profile currentUser)

Returns: void

Creates or overwrites a file at the given directory path within a branch, uploading the stream's content into the content store and saving the branch. Throws if the directory does not exist or does not identify a directory.

ParameterDescription
branchthe branch to save the file into
dirPaththe path of the existing directory to save the file into
filenamethe name of the file to create or overwrite
inthe file content to upload
currentUserthe profile to record as the editor of the change

createFolder(Branch branch, String dirPath, String folderName, Profile currentUser)

Returns: void

Creates a new sub-directory under the given directory path within a branch, and saves the branch. Throws if the parent directory does not exist or does not identify a directory.

ParameterDescription
branchthe branch to create the directory in
dirPaththe path of the existing parent directory
folderNamethe name of the directory to create
currentUserthe profile to record as the editor of the change

convertToInputStream(String content)

Returns: InputStream

Converts a string into an input stream over its bytes.

ParameterDescription
contentthe string content to wrap

uploadFiles(Map<String,FileItem> files)

Returns: Map<String,String>

Uploads each file in the map into content storage, returning a map from the original file name to its content hash. A file whose upload fails maps instead to a string starting with "failed-" followed by the error message.

ParameterDescription
filesthe file items to upload, keyed by a caller-supplied name

uploadFile(FileItem file)

Returns: String

Uploads a file item into content storage with no mime-type restriction.

ParameterDescription
filethe file item to upload

upload(InputStream in)

Returns: String

Splits and stores the given stream's content in the hash-split content store, yielding periodically so long uploads do not block other work.

ParameterDescription
inthe content to upload

getFileType(String fileName)

Returns: String

Determines the mime content type for a file name from its extension, without inspecting any file content.

ParameterDescription
fileNamethe file name to inspect, typically including its extension

upload(byte[] bytes)

Returns: String

Splits and stores the given byte array's content in the hash-split content store.

ParameterDescription
bytesthe content to upload

getAsInputStream(String hash)

Returns: ByteArrayInputStream

Reads the content for a content hash and wraps it in a new input stream.

ParameterDescription
hashthe content hash to read

getAsByteArray(String hash)

Returns: byte[]

Reads the full content for a content hash into a byte array, for the current tenant.

ParameterDescription
hashthe content hash to read

getAsString(String hash)

Returns: String

Reads the full content for a content hash and decodes it as a string using the platform default charset.

ParameterDescription
hashthe content hash to read

getAsString(FileItem fileItem)

Returns: String

Reads the full content of an uploaded file item and decodes it as a UTF-8 string.

ParameterDescription
fileItemthe file item to read

getAsBase64(String hash)

Returns: String

Reads the full content for a content hash and base64-encodes it.

ParameterDescription
hashthe content hash to read

getAsBase64(Resource resource)

Returns: String

Base64-encodes the content of a Milton resource. Reads directly from content storage for a hash resource, or otherwise streams the resource's rendered content.

ParameterDescription
resourcethe resource whose content should be encoded

getAsBase64(InputStream in)

Returns: String

Reads a stream fully and base64-encodes its bytes.

ParameterDescription
inthe stream to read and encode

getAsByteArray(InputStream in)

Returns: byte[]

Reads a stream fully into a byte array.

ParameterDescription
inthe stream to read

getBranchFolder(String repoName)

Returns: BranchFolder

Finds the live branch folder of a repository in the current organisation, without creating anything.

ParameterDescription
repoNamethe name of the repository to look up

getBranchFolder(String repoName, boolean autoCreate)

Returns: BranchFolder

Finds the live branch folder of a repository in the current organisation, optionally creating the repository if it does not already exist.

ParameterDescription
repoNamethe name of the repository to look up
autoCreateif true, create the repository when it does not already exist

getBranchFolder(String repoName, String branchName)

Returns: BranchFolder

Finds a named branch folder of a repository in the current organisation, without creating anything.

ParameterDescription
repoNamethe name of the repository to look up
branchNamethe name of the branch to look up

getBranchFolder(String repoName, String branchName, boolean autoCreate)

Returns: BranchFolder

Finds a named branch folder of a repository in the current organisation, optionally creating the repository and branch if they do not already exist.

ParameterDescription
repoNamethe name of the repository to look up
branchNamethe name of the branch to look up, or blank to skip creating a branch
autoCreateif true, create the repository and branch when they do not already exist

fromCsv(Object o)

Returns: List<String[]>

Parses a CSV file using the default comma separator and returns a list of rows, each row being a string array of fields.

ParameterDescription
othe CSV source, as an input stream, a file item, a byte array, or a value converted to a string

fromCsv(Object o, Object seperator)

Returns: List<String[]>

Parses a CSV file using an explicit field separator and returns a list of rows, each row being a string array of fields.

ParameterDescription
othe CSV source, as an input stream, a byte array, or a value converted to a string
seperatorthe field separator character to use, as a single-character value; blank defaults to a comma

parseExcel(InputStream in, boolean useXml)

Returns: Workbook

Parses an Excel workbook from a stream.

ParameterDescription
inthe workbook data to read
useXmlif true parse as the newer XML-based XSSF format (.xlsx), otherwise parse as the legacy binary HSSF format (.xls)

parseExcel(byte[] bytes, boolean useXml)

Returns: Workbook

Parses an Excel workbook from a byte array.

ParameterDescription
bytesthe workbook data to read
useXmlif true parse as the newer XML-based XSSF format (.xlsx), otherwise parse as the legacy binary HSSF format (.xls)

parseDOM(String s)

Returns: Document

Parses an XML string into a normalized DOM document.

ParameterDescription
sthe XML content to parse

parseDOM(InputStream in)

Returns: Document

Parses an XML stream into a normalized DOM document.

ParameterDescription
inthe XML content to parse

parseXmlToJson(InputStream in)

Returns: String

Parses an XML stream and converts it to an equivalent JSON string.

ParameterDescription
inthe XML content to parse

parseXmlToJson(String rawXml)

Returns: String

Parses an XML string and converts it to an equivalent JSON string.

ParameterDescription
rawXmlthe XML content to parse

parsePDF(String hash, String password, Consumer<PdfDocument> callback)

Returns: void

Reads the content for a hash and parses it as a password-protected PDF, passing the parsed document to the callback. The document is closed automatically once the callback returns.

ParameterDescription
hashthe content hash of the PDF to read
passwordthe password protecting the PDF
callbackreceives the parsed PDF document for the duration of the call

parsePDF(String hash, Consumer<PdfDocument> callback)

Returns: void

Reads the content for a hash and parses it as a PDF with no password, passing the parsed document to the callback. The document is closed automatically once the callback returns.

ParameterDescription
hashthe content hash of the PDF to read
callbackreceives the parsed PDF document for the duration of the call

parsePDF(byte[] bytes, Consumer<PdfDocument> callback)

Returns: void

Parses a PDF byte array with no password, passing the parsed document to the callback. Does nothing if bytes is null. The document is closed automatically once the callback returns.

ParameterDescription
bytesthe PDF data to parse
callbackreceives the parsed PDF document for the duration of the call

parsePDF(byte[] bytes, String password, Consumer<PdfDocument> callback)

Returns: void

Parses a password-protected PDF byte array, passing the parsed document to the callback. Does nothing if bytes is null. The document is closed automatically once the callback returns.

ParameterDescription
bytesthe PDF data to parse
passwordthe password protecting the PDF
callbackreceives the parsed PDF document for the duration of the call

newXmlWriter(OutputStream out)

Returns: XmlWriter

Creates a new Milton XmlWriter that writes elements to the given output stream.

ParameterDescription
outthe stream the writer will write XML to

newXmlBuilder(String rootName)

Returns: XmlBuilder

Creates a new XmlBuilder for programmatically assembling an XML document under the given root element name.

ParameterDescription
rootNamethe name of the document's root element

toCsvAsString(List<List> list)

Returns: String

Converts a list of rows (each a list of values) to CSV text, using the default comma separator and quote character.

ParameterDescription
listthe rows to write, each a list of field values

toCsvAsString(List<List> list, char separator)

Returns: String

Converts a list of rows (each a list of values) to CSV text, using an explicit field separator and the default quote character.

ParameterDescription
listthe rows to write, each a list of field values
separatorthe field separator character to use

toCsvAsString(List<List> list, char separator, char quotechar)

Returns: String

Converts a list of rows (each a list of values, a ScriptObjectMirror, a polyglot value, or a string array) to CSV text, using explicit field separator and quote characters. Each row's values are formatted to strings using the current Formatter before being written.

ParameterDescription
listthe rows to write
separatorthe field separator character to use
quotecharthe character used to quote fields

getPdfManager()

Returns: PdfManager

The PDF manager instance shared with this FileManager, used for reading, generating and manipulating PDF documents.

getUtils()

Returns: FileUtils

The FileUtils instance shared with this FileManager, used for lower-level file and hash-store operations.

getFileFromURI(String fileURI)

Returns: String

Downloads the content at a URL and stores it in the hash-split content store.

ParameterDescription
fileURIthe URL to download from

commitRemoteHash(String newRemoteHash, String adminUrl, String loginToken, Branch branch)

Returns: void

Walks a remote content hash into local storage and commits it as a new commit on the given branch, authenticating to the remote server with a login token.

ParameterDescription
newRemoteHashthe content hash to migrate from the remote server and commit
adminUrlthe base URL of the remote server to migrate content from
loginTokenthe login token to authenticate to the remote server with
branchthe branch to commit the migrated hash onto

commitRemoteHash(String newRemoteHash, String adminUrl, String username, String password, Branch branch)

Returns: void

Walks a remote content hash into local storage and commits it as a new commit on the given branch, authenticating to the remote server with a username and password.

ParameterDescription
newRemoteHashthe content hash to migrate from the remote server and commit
adminUrlthe base URL of the remote server to migrate content from
usernamethe user name to authenticate to the remote server with
passwordthe password to authenticate to the remote server with
branchthe branch to commit the migrated hash onto

findFileExtension(FileItem fileItem)

Returns: String

Finds the file extension of an uploaded file item's name.

ParameterDescription
fileItemthe file item to inspect

findFileExtension(String fileName)

Returns: String

Finds the file extension of a file name.

ParameterDescription
fileNamethe file name to inspect
To get full access to the Kademi Hub existing customers can login here, or new customers can register here.