In the Olympe platform files can be of three types:
- Files accessible directly via an URL, like an image residing on a web site.
- Files that are managed by the Olympe back-end (orchestrator) and can only be accessed with proper authorization and authentication.
- Files that are being uploaded by the user and that are temporarily stored in memory, usually in the form of
FileUploadItem
.
All these types are covered by classes that implement the olympe.df.File
interface. In most cases it is
preferable to use the methods declared in the interface because you are guaranteed it will work no matter
what kind of file you are manipulating.
This is a rather small interface and is pretty straightforward, however one aspect ot it requires a bit of
an explanation: Accessing the content of the file.
Because, as mentioned earlier, some of these files are behind a secure storage, accessing the content requires
authentication and permission checks, therefore the URL itself, as returned by getUrl()
, is not enough.
The API offers three asynchronous methods that will work in all cases:
getContentAsBinary(onSuccess, onFailure)
when successful will call theonSuccess
function and pass it anArrayBuffer
containing the actual binary content of the file. It will call theonFailure
function and pass it a string containing an error message in case of failure.getContentAsString(onSuccess, onFailure)
when successful will call theonSuccess
function and pass it a UTF8 string containing the content of the file. It will call theonFailure
function and pass it a string containing an error message in case of failure. This method should only be used to access text files like JSON or plain text files.getContentUrl(onSuccess, onFailure)
when successful will call theonSuccess
function and pass it a string containing a URL guaranteed to provide access to the content of the file. This will be a 'data:' URL in the case of managed files. It will call theonFailure
function and pass it a string containing an error message in case of failure.
The DC File Model
If you need to handle files in your data-model (e.g. as a property or through a relation), you will have to use
the File
class or one of its subclasses.
The simplest way to create a file is to use the helper static function olympe.dm.File#createFile
Here is a code example of how to 'persist' a File obtained via a FileUpload
UI components which gives
us a list of olympe.ui.std.FileUploadItem
.
function handleDroppedFiles(files) {
// Process uploaded files
files.forEach(uploadedFile => {
////// Create a File instance //////
const transaction = new olympe.dc.Transaction();
const mimeType = uploadedFile.getMimeType().valueOf();
const model = olympe.sc.getFileModel(mimeType);
olympe.dm.File.createFile(model, transaction, uploadedFile.getName().valueOf(), uploadedFile.getContent(), mimeType);
transaction.execute();
});
}
If you want to create an external File
(i.e. referenced as a URL like 'http://my.company.com/license.txt'), you need to use
the method where you pass the URL instead of an
ArrayBuffer
.
const transaction = new olympe.dc.Transaction();
olympe.dm.File.createFileFromURL(olympe.dm.File, transaction, 'License', 'http://my.company.com/license.txt', 'text/plain');
transaction.execute();
When either of these transactions are executed, and the files have to be persisted, the transaction engine will automatically use the File Service if necessary, so you don't have to do anything either way.