files/server.js

import { JsonApiServer } from '../json_api'
import { isBrowserEnv } from '../utils/is_browser'
import { createFormData } from '../utils/form_data'

/**
 * Facilitates interaction with the wallet server.
 *
 * @class
 */
export class FileServer extends JsonApiServer {
  /**
   * Create a new wallet server instance.
   *
   * @constructor
   * @param {ShelfNetwork} sdk Parent SDK instance.
   * @param {string} serverUrl wallet server URL.
   * @param {Object} opts
   * @param {boolean} [opts.allowHttp] Allow connecting to http servers, default: `false`. This must be set to false in production deployments!
   * @param {Object} [opts.proxy] Proxy configuration. Look [axios docs](https://github.com/axios/axios#request-config) for more info
   * @param {Object} [opts.httpBasicAuth] HTTP basic auth credentials. Look [axios docs](https://github.com/axios/axios#request-config) for more info.
   * @param {Object} [opts.customHeaders] Custom headers for request.
   * @param {boolean} [opts.withCredentials] Indicates whether or not cross-site Access-Control requests should be made using credentials.
   * @param {string} [opts.responseType='json'] Indicates the type of data that the server will respond with options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'.
   */
  constructor (sdk, serverUrl, opts = {}) {
    opts.alias = 'storage'
    super(sdk, serverUrl, opts)
  }

  /**
   * Uploads a file to the storage server.
   *
   * @param {(File|Blob|ReadableStream)} file A file to upload.
   * @param {object} opts Document options.
   * @param {string} [opts.accessType='public'] Document access type. 'private' or 'public'
   * @param {string[]} [opts.owners=[]] Account IDs of the document owners
   * @param {string} [opts.expires] Document expiration time.
   * @return {Promise.<JsonApiResponse>} Payment server response.
   */
  upload (file, { accessType = 'public', owners = [], expires } = {}) {
    const formData = createFormData()
    const attributes = {
      document_type: accessType,
      owners,
      expires
    }

    formData.append('file', file)
    formData.append('file[attributes]', JSON.stringify(attributes))

    return this._makeDocumentsCallBuilder()
      .withCredentials()
      .withHeaders(isBrowserEnv() ? {} : formData.getHeaders())
      .post(formData)
  }

  /**
   * Get a file by ID.
   *
   * @param {string} fileId File ID.
   * @return {Promise.<JsonApiResponse>} Payment server response.
   */
  get (fileId) {
    return this._makeDocumentsCallBuilder()
      .appendUrlSegment(fileId)
      .withCredentials()
      .get()
  }

  /**
   * Get personal files.
   *
   * @param {object}   [query] Request options.
   * @param {string[]} [query.in] Array of document id's.
   * @param {Number}   [query.page.number] Page number.
   * @param {Number}   [query.page.size] Page size.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  getPage (query) {
    return this._makeDocumentsCallBuilder()
      .get(query)
  }

  _makeDocumentsCallBuilder () {
    return this._makeCallBuilder()
      .appendUrlSegment('documents')
      .withCredentials()
  }
}