Skip to content

Latest commit

 

History

History
242 lines (178 loc) · 7.8 KB

README.md

File metadata and controls

242 lines (178 loc) · 7.8 KB

ember-filestack

npm version Build Status Ember Observer Score

Provides file picking, storing, and converting funtionality from Filestack using filestack-js.

Installation

ember install ember-filestack

ember-filestack supports all ember LTS versions (that means 2.18+ at the moment). With that being said, it may support versions even older than that.

To use it on ember versions prior to 2.5, you must include ember-assign-polyfill on your app.

Angle bracket invocation syntax is optional, but if you can use it in ember versions back to 2.12 if you install ember-angle-bracket-invocation-polyfill on your app.

Migration from 1.0.0

If you need to migrate from the previous 1.0.0 release, please read our migration guide.

Configuration

API Key

// config/environment.js
module.exports = function(environment) {
  let ENV = {
    //...
    'ember-filestack': {
      apiKey: 'AOkSBYOLvTqK3GzWzQMOuz'
    }
  };
  //...
};

Custom CDN

To minimize your quota usage (both bandwidth and process quotas), you can put filestack behind your own CDNs (e.g. AWS CloudFront). To do that you will want to proxy all urls with the host https://cdn.filestackcontent.com.

Then you can configure your custom CDN url like:

// config/environment.js
module.exports = function(environment) {
  let ENV = {
    //...
    'ember-filestack': {
      apiKey: '<your filestack api key>',
      customCDN: '<your-cdn.cloudfront.net>'
    }
  };
  //...
};

When customCDN is defined, ember-filestack will ensure that Filestack’s CDN is never accessed directly.

Global config for picker and preview

You can add global options for the picker and preview components (read more about them in the next sections). For that, you can use the pickerOptions and previewOptions keys in the ember-filestack config. Additionally, any necessary filestack client options must be defined here.

Example:

// config/environment.js
module.exports = function(environment) {
  let ENV = {
    //...
    'ember-filestack': {
      // ...
      clientOptions: {
        // add any client options here
      },
      pickerOptions: {
        // add any global picker options here
      },
      previewOptions: {
        // add any global preview options here
      }
    }
  };
  //...

Usage

File Picker

The file picker is rendered using the <FilestackPicker/> component. This component accepts all the available picker options that the filestack picker accepts. Complete documentation of all the available options can be found here.

Example:

<button onclick={{action (mut showFilePicker) true}}>Choose file</button>

{{#if showFilePicker}}
  <FilestackPicker
    @accept="image/*"
    @onUploadDone={{action "fileSelected"}}
    @onClose={{action (mut showFilePicker) false}}
  />
{{/if}}
export default Component.extend({
  actions: {
    fileSelected(result) {
      // `result` is an array of files you've just uploaded
      console.log(result.filesUploaded);
    }
  }
});

Generate file urls

When you need to generate a filestack url, you should use the {{filestack-url}} helper.

This helper accepts a filestack handle or any image url as the first (and only) positional parameter.

Additionally, you can pass in additional multiple named parameters that correspond to transforms. The helper accepts all the available transforms that filestack provides. You can check the available transform options here.

Examples:

{{filestack-url handleOrUrl}}

{{filestack-url handleOrUrl resize=(hash width=50 height=50 fit="scale")}}

{{filestack-url handleOrUrl output=(hash format="jpg")}}

{{filestack-url handleOrUrl output=(hash format="png") resize=(hash width=500 height=500 fit="max")}}

You can also use the included filestack service to generate image urls:

import Component from '@ember/component';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';

export default Component.extend({
  filestack: service(),

  scaledImage: computed('imageUrl', function() {
    let url = this.get('imageUrl');
    let transformations = {
      output: {
        format: 'png'
      },
      resize: {
        width: 500,
        height: 500,
        fit: 'max'
      }
    };

    return this.get('filestack').getUrl(url, transformations);
  })
});

All of these will respect the customCDN configuration, if present.

Notice that it is possible to write invalid transforms. For example, you can't specify a transform of resize=(hash fit="some-invalid-value"). Under the hook, filestack-js will validate the passed transform options, so expect errors if you do something wrong.

Filestack previewer

Filestack has a feature to preview documents in a nice file previewer. This renders an iframe with filestack's previewer and it supports multiple formats. Read more about this feature here.

ember-filestack provides a <FilestackPreview/> component that you can use to quickly insert this iframe anywhere in your DOM.

Example:

<FilestackPreview @handle={{fileHandle}}/>

This component supports all the preview options that filestack supports. Complete documentation of all the available options can be found here.

Direct filestack client Access

Sometimes you might need to use the filestack client manually. To do that, you can use the filestack service initClient method as follows:

import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Component.extend({
  // inject the filestack service
  filestack: service(),

  async someFunction() {
    // Use the promise in case you are not sure that your component will be initialized after filestack has been loaded

    let filestack = await this.filestack.initClient();

    // do something with filestack
  }
});

Notice that initClient is asynchronous (returns a Promise). This happens because we lazily initialize and import filestack client. Please read the next section for more information.

Lazy filestack initialization

All the filestack javascript and the respective filestack initialization is done only when needed. This means that, if a user never renders a filepicker or generates a filestack url, your app won't spend any resources or bandwidth with filestack related work.

With that being said, ember-filestack makes sure that it only initializes filestack once. This means that, for example, if you call this.filestack.initClient() twice, all the heavy work will be done once. In this example the second call would return the filestack client immediately.

Fastboot considerations

ember-filestack will no-op on fastboot environments since filestack-js requires access to document (which isn't available on node).