Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@filerobot/utils / lib / getFormattedPreviewUrl.js
Size: Mime:
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(e, r) { return new BabelRegExp(e, void 0, r); }; var e = RegExp.prototype, r = new WeakMap(); function BabelRegExp(e, t, p) { var o = new RegExp(e, t); return r.set(o, p || r.get(e)), _setPrototypeOf(o, BabelRegExp.prototype); } function buildGroups(e, t) { var p = r.get(t); return Object.keys(p).reduce(function (r, t) { var o = p[t]; if ("number" == typeof o) r[t] = e[o];else { for (var i = 0; void 0 === e[o[i]] && i + 1 < o.length;) i++; r[t] = e[o[i]]; } return r; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (r) { var t = e.exec.call(this, r); if (t) { t.groups = buildGroups(t, this); var p = t.indices; p && (p.groups = buildGroups(p, this)); } return t; }, BabelRegExp.prototype[Symbol.replace] = function (t, p) { if ("string" == typeof p) { var o = r.get(this); return e[Symbol.replace].call(this, t, p.replace(/\$<([^>]+)>/g, function (e, r) { var t = o[r]; return "$" + (Array.isArray(t) ? t.join("$") : t); })); } if ("function" == typeof p) { var i = this; return e[Symbol.replace].call(this, t, function () { var e = arguments; return "object" != _typeof(e[e.length - 1]) && (e = [].slice.call(e)).push(buildGroups(e, i)), p.apply(this, e); }); } return e[Symbol.replace].call(this, t, p); }, _wrapRegExp.apply(this, arguments); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var FILEROBOT_FILE_URL_REGEX = /*#__PURE__*/_wrapRegExp(/(https:\/\/)([\w-]*)\.(\w*)\.(\w*)\//, {
  protocol: 1,
  token: 2,
  secondLevelDomain: 3,
  topLevelDomain: 4
});
var FILEROBOT_SECOND_LEVEL_DOMAIN = 'filerobot';
var DEFAULT_FILEROBOT_SUBDOMAIN_PROD = 'assets';
var DEFAULT_FILEROBOT_SUBDOMAIN_DEV = 'assets-dev';
var FILEROBOT_V3_URL = 'assets.filerobot.com';
var TOP_LEVEL_DOMAIN = 'com';

/**
 * Ge preview url from filerobot url
 *
 * Based on documentation https://scaleflexhq.atlassian.net/wiki/spaces/DEVOPS/pages/166199297/Delivery+Filerobot+Hub+Thumbs+v3
 *
 * There are 2 cases:
 * 1) when there is v7 in the url
 * v2 https://fldytuwm.filerobot.com/v7/120049.jpg into v3 https://assets.filerobot.com/fldytuwm/120049.jpg
 * v2 https://cdn.scaleflex.com/cloudimage.io/checkerboard.svg?vh=938ef5 into v3 https://assets.filerobot.com/scaleflex/cloudimage.io/checkerboard.svg?vh=938ef5
 *
 * 2)when there is no v7 in the url
 * v2 https://fyjnhqim.filerobot.com/ganchev/test_files+doubled/01+-+StyleSense+-+Falsche+Darstellung.png?vh=2b64bd into https://assets.filerobot.com/fyjnhqim/ganchev/test_files+doubled/01+-+StyleSense+-+Falsche+Darstellung.png?vh=2b64bd
 * v2 https://fyjnhqim.filerobot.com/.internal/video_thumbnails/2oaTbKYAtv/20220818_112552.jpg into https://assets.filerobot.com/fyjnhqim/.internal/video_thumbnails/2oaTbKYAtv/20220818_112552.jpg
 *
 * @returns {string} -- New url based on the rules above
 *
 * @param {string} url -- Filerobot file url
 */
export default (function (_ref) {
  var _url$match;
  var _ref$url = _ref.url,
    url = _ref$url === void 0 ? '' : _ref$url,
    isDevEnv = _ref.isDevEnv,
    isHubMode = _ref.isHubMode,
    containerToken = _ref.containerToken,
    _ref$provideContainer = _ref.provideContainerToken,
    provideContainerToken = _ref$provideContainer === void 0 ? false : _ref$provideContainer;
  var _ref2 = ((_url$match = url.match(FILEROBOT_FILE_URL_REGEX)) === null || _url$match === void 0 ? void 0 : _url$match.groups) || {},
    protocol = _ref2.protocol,
    token = _ref2.token,
    secondLevelDomain = _ref2.secondLevelDomain,
    topLevelDomain = _ref2.topLevelDomain;
  if (url.includes('blob:')) return url;
  if (url.includes(FILEROBOT_V3_URL)) {
    if (isDevEnv) {
      return url.replace(DEFAULT_FILEROBOT_SUBDOMAIN_PROD, DEFAULT_FILEROBOT_SUBDOMAIN_DEV);
    }
    return url;
  }
  var debug = function debug() {
    var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    if (localStorage.getItem('debug_previews') !== '1') {
      return;
    }
    console.trace(_objectSpread(_objectSpread({}, data), {}, {
      url: url,
      isDevEnv: isDevEnv,
      isHubMode: isHubMode,
      containerToken: containerToken,
      provideContainerToken: provideContainerToken,
      protocol: protocol,
      token: token,
      secondLevelDomain: secondLevelDomain,
      topLevelDomain: topLevelDomain
    }));
  };

  // TODO: refactor to more sustainable parameter
  if (isHubMode) {
    var matchFilerobotCdnUrlRegExp = new RegExp("".concat(protocol).concat(token, "\\.").concat(secondLevelDomain, "\\.").concat(topLevelDomain, "\\/(v7\\/)?"));
    var previewToken = isDevEnv ? DEFAULT_FILEROBOT_SUBDOMAIN_DEV : DEFAULT_FILEROBOT_SUBDOMAIN_PROD;
    var previewUrl = "".concat(protocol).concat(previewToken, ".").concat(FILEROBOT_SECOND_LEVEL_DOMAIN, ".").concat(TOP_LEVEL_DOMAIN).concat(provideContainerToken ? '/' : "/".concat(containerToken, "/"));
    debug({
      previewUrl: previewUrl,
      finalUrl: url.replace(matchFilerobotCdnUrlRegExp, previewUrl)
    });
    return url.replace(matchFilerobotCdnUrlRegExp, previewUrl);
  }
  debug();
  return url;
});