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
| Property | Returns | Description |
|---|---|---|
| pdfManager | PdfManager | The PDF manager instance shared with this FileManager, used for reading, generating and manipulating PDF documents. |
| utils | FileUtils | The 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.
| Parameter | Description |
|---|---|
branch | the branch to save the file into |
dirPath | the path of the existing directory to save the file into |
filename | the name of the file to create or overwrite |
in | the file content to upload |
currentUser | the 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.
| Parameter | Description |
|---|---|
branch | the branch to create the directory in |
dirPath | the path of the existing parent directory |
folderName | the name of the directory to create |
currentUser | the profile to record as the editor of the change |
convertToInputStream(String content)
Returns: InputStream
Converts a string into an input stream over its bytes.
| Parameter | Description |
|---|---|
content | the 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.
| Parameter | Description |
|---|---|
files | the 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.
| Parameter | Description |
|---|---|
file | the 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.
| Parameter | Description |
|---|---|
in | the 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.
| Parameter | Description |
|---|---|
fileName | the 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.
| Parameter | Description |
|---|---|
bytes | the content to upload |
getAsInputStream(String hash)
Returns: ByteArrayInputStream
Reads the content for a content hash and wraps it in a new input stream.
| Parameter | Description |
|---|---|
hash | the 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.
| Parameter | Description |
|---|---|
hash | the 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.
| Parameter | Description |
|---|---|
hash | the 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.
| Parameter | Description |
|---|---|
fileItem | the file item to read |
getAsBase64(String hash)
Returns: String
Reads the full content for a content hash and base64-encodes it.
| Parameter | Description |
|---|---|
hash | the 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.
| Parameter | Description |
|---|---|
resource | the resource whose content should be encoded |
getAsBase64(InputStream in)
Returns: String
Reads a stream fully and base64-encodes its bytes.
| Parameter | Description |
|---|---|
in | the stream to read and encode |
getAsByteArray(InputStream in)
Returns: byte[]
Reads a stream fully into a byte array.
| Parameter | Description |
|---|---|
in | the stream to read |
getBranchFolder(String repoName)
Returns: BranchFolder
Finds the live branch folder of a repository in the current organisation, without creating anything.
| Parameter | Description |
|---|---|
repoName | the 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.
| Parameter | Description |
|---|---|
repoName | the name of the repository to look up |
autoCreate | if 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.
| Parameter | Description |
|---|---|
repoName | the name of the repository to look up |
branchName | the 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.
| Parameter | Description |
|---|---|
repoName | the name of the repository to look up |
branchName | the name of the branch to look up, or blank to skip creating a branch |
autoCreate | if 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.
| Parameter | Description |
|---|---|
o | the 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.
| Parameter | Description |
|---|---|
o | the CSV source, as an input stream, a byte array, or a value converted to a string |
seperator | the 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.
| Parameter | Description |
|---|---|
in | the workbook data to read |
useXml | if 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.
| Parameter | Description |
|---|---|
bytes | the workbook data to read |
useXml | if 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.
| Parameter | Description |
|---|---|
s | the XML content to parse |
parseDOM(InputStream in)
Returns: Document
Parses an XML stream into a normalized DOM document.
| Parameter | Description |
|---|---|
in | the XML content to parse |
parseXmlToJson(InputStream in)
Returns: String
Parses an XML stream and converts it to an equivalent JSON string.
| Parameter | Description |
|---|---|
in | the XML content to parse |
parseXmlToJson(String rawXml)
Returns: String
Parses an XML string and converts it to an equivalent JSON string.
| Parameter | Description |
|---|---|
rawXml | the 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.
| Parameter | Description |
|---|---|
hash | the content hash of the PDF to read |
password | the password protecting the PDF |
callback | receives 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.
| Parameter | Description |
|---|---|
hash | the content hash of the PDF to read |
callback | receives 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.
| Parameter | Description |
|---|---|
bytes | the PDF data to parse |
callback | receives 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.
| Parameter | Description |
|---|---|
bytes | the PDF data to parse |
password | the password protecting the PDF |
callback | receives 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.
| Parameter | Description |
|---|---|
out | the 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.
| Parameter | Description |
|---|---|
rootName | the 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.
| Parameter | Description |
|---|---|
list | the 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.
| Parameter | Description |
|---|---|
list | the rows to write, each a list of field values |
separator | the 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.
| Parameter | Description |
|---|---|
list | the rows to write |
separator | the field separator character to use |
quotechar | the 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.
| Parameter | Description |
|---|---|
fileURI | the 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.
| Parameter | Description |
|---|---|
newRemoteHash | the content hash to migrate from the remote server and commit |
adminUrl | the base URL of the remote server to migrate content from |
loginToken | the login token to authenticate to the remote server with |
branch | the 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.
| Parameter | Description |
|---|---|
newRemoteHash | the content hash to migrate from the remote server and commit |
adminUrl | the base URL of the remote server to migrate content from |
username | the user name to authenticate to the remote server with |
password | the password to authenticate to the remote server with |
branch | the branch to commit the migrated hash onto |
findFileExtension(FileItem fileItem)
Returns: String
Finds the file extension of an uploaded file item's name.
| Parameter | Description |
|---|---|
fileItem | the file item to inspect |
findFileExtension(String fileName)
Returns: String
Finds the file extension of a file name.
| Parameter | Description |
|---|---|
fileName | the file name to inspect |