Storage Usage in JavaScript

Save Text/Data

EmbarkJS.Storage.saveText("hello world")
  .then(function(hash) {})
  .catch(function(err) {
      if(err){
        console.log("IPFS saveText Error => " + err.message);
      }
  });

Retrieve Text/Data

EmbarkJS.Storage.get(hash)
  .then(function(content) {})
  .catch(function(err) {
      if(err){
        console.log("IPFS get Error => " + err.message);
      }
  });

Upload a file

<input type="file">

var input = $("input[type=file"]);
EmbarkJS.Storage.uploadFile(input)
  .then(function(hash) {})
  .catch(function(err) {
      if(err){
        console.log("IPFS uploadFile Error => " + err.message);
      }
  });

Display a file

EmbarkJS.Storage.getUrl(hash);

Check for storage provider availability

This will return true if the storage provider (IPFS or Swarm) is available and running.

EmbarkJS.Storage.isAvailable()
  .then(isAvailable => { alert(`The storage provider is: ${isAvailable ? 'available' : 'not available'}`) })
  .catch(function(err) {
      if(err){
        console.log("Error getting storage provider availability => " + err.message);
      }
  });

Setup

By default Embark will automatically initialize EmbarkJS with the provider configured at config/storage.js. However if you are using EmbarkJS directly or wish to change the provider configuration on the fly you can do:

EmbarkJS.Storage.setProvider('swarm', options);

Options are optional and if provided, will override the values in storage.js.

For example,

EmbarkJS.Storage.setProvider('ipfs', {server: 'localhost', port: '5001'});
// OR
EmbarkJS.Storage.setProvider('swarm', {server: 'swarm-gateways.net', port: '80'});

IPNS registration

You can register your IPFS hash using IPNS.

*It can take up to a minute to register.

IPNS - Register

EmbarkJS.Storage.register('IPFS_hash', (err, name) => {
if(err){
console.log("Error registering", err.message);
return;
}
console.log('Registred to the following hash:', name);
});

IPNS - Resolve

EmbarkJS.Storage.resolve('IPNS_hash', (err, path) => {
if(err){
console.log("Error resolving", err.message);
return;
}
console.log('Resolved to the following IPFS path:', name);
});