All files / resizin/lib/upload upload.js

95.12% Statements 39/41
82.14% Branches 23/28
100% Functions 6/6
97.37% Lines 37/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85    1x     1x   1x   1x   1x   1x       4x   2x   1x       1x 1x         1x 11x 11x   11x       11x 1x     10x 1x     9x 1x     8x 8x           8x 8x         8x     8x 8x 8x 8x 8x 8x 8x 2x     6x           1x 1x
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
 
var _defaults = _interopRequireDefault(require("lodash/defaults"));
 
var _isomorphicFetch = _interopRequireDefault(require("isomorphic-fetch"));
 
var _promisePolyfill = _interopRequireDefault(require("promise-polyfill"));
 
var _v = _interopRequireDefault(require("uuid/v1"));
 
var _mimeByType;
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
 
function _defineProperty(obj, key, value) { Iif (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
var type = {
  IMAGE: 'image',
  FILE: 'file'
};
var mimeByType = (_mimeByType = {}, _defineProperty(_mimeByType, type.IMAGE, 'image/png'), _defineProperty(_mimeByType, type.FILE, 'application/octet-stream'), _mimeByType);
var defaultOptions = {
  fileType: type.IMAGE,
  autoId: false
};
 
var uploadImage = function uploadImage(serverUrl, apiKey, imageId, file, uploadOptions) {
  return _promisePolyfill["default"].resolve().then(function () {
    var options = (0, _defaults["default"])(uploadOptions, defaultOptions);
 
    Iif (!serverUrl) {
      throw new Error('Url is missing!');
    }
 
    if (!apiKey) {
      throw new Error('API KEY is missing!');
    }
 
    if (!imageId && !options.autoId) {
      throw new Error('Image id is missing!');
    }
 
    if (!file) {
      throw new Error('File is missing!');
    }
 
    var fileType = options.fileType === type.IMAGE ? options.fileType : type.FILE;
    var fetchOptions = {
      method: 'POST',
      headers: {
        Authorization: 'Key ' + apiKey
      }
    };
    var formData = new FormData();
    var id = options.autoId && !imageId ? (0, _v["default"])() : imageId; // TODO - how to deal with FormData having different interface in different environments.
    // At Browser app there is a native FormData which accepts only filename as a third parameter
    // whereas at Node.js there is commnunity implementiion of the FormData and has more complex
    // third parameter (including contentType).
 
    var fileMeta = {
      contentType: options.mime || mimeByType[fileType]
    };
    formData.append('id', id);
    formData.append('file', file, fileMeta);
    fetchOptions.body = formData;
    var url = "".concat(serverUrl, "/api/v1/").concat(fileType, "/upload");
    return (0, _isomorphicFetch["default"])(url, fetchOptions).then(function (response) {
      return response.json().then(function (res) {
        if (response.status >= 400) {
          throw new Error(res.message || 'Bad response from server!');
        }
 
        return res;
      });
    });
  });
};
 
var _default = uploadImage;
exports["default"] = _default;