Accessing Files

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 the onSuccess function and pass it an ArrayBuffer containing the actual binary content of the file. It will call the onFailure function and pass it a string containing an error message in case of failure.
  • getContentAsString(onSuccess, onFailure) when successful will call the onSuccess function and pass it a UTF8 string containing the content of the file. It will call the onFailure 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 the onSuccess 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 the onFailure 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.

]" ).each( function () { var $this = $( this ); $this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) ); ); $( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () { var $this = $( this ); var example = $this.find( "code" ); exampleText = example.html(); var lang = /{@lang (.*?)}/.exec( exampleText ); if ( lang && lang[1] ) { exampleText = exampleText.replace( lang[0], "" ); example.html( exampleText ); lang = lang[1]; } else { var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/); lang = langClassMatch ? langClassMatch[1] : "javascript"; } if ( lang ) { $this .addClass( "sunlight-highlight-" + lang ) .addClass( "linenums" ) .html( example.html() ); } } ); Sunlight.highlightAll( { lineNumbers : true, showMenu : true, enableDoclinks : true } ); $.catchAnchorLinks( { navbarOffset: 10 } ); $( "#toc" ).toc( { anchorName : function ( i, heading, prefix ) { return $( heading ).attr( "id" ) || ( prefix + i ); }, selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4", showAndHide : false, smoothScrolling: true } ); $( "#main span[id^='toc']" ).addClass( "toc-shim" ); $( '.dropdown-toggle' ).dropdown(); $( "table" ).each( function () { var $this = $( this ); $this.addClass('table'); } ); } );